简体   繁体   English

删除数组元素时出错

[英]Error while removing array element

I have new arraylist, 1 arraylist which has 10 customer already inserted. 我有一个新的arraylist,其中1个arraylist已插入10个客户。 I'm running a loop which picks a random customer from the arraylist and adds it into the 2nd arraylist. 我正在运行一个循环,该循环从arraylist中选择一个随机客户并将其添加到第二个arraylist中。 However, I'm getting duplicates when I insert customer into the 2nd arraylist. 但是,当我将客户插入第二个arraylist时,会出现重复。 So when the loop runs after adding the customer into the 2nd arraylist it will remove it from the 1st arraylist. 因此,当在将客户添加到第二个数组列表后运行循环时,它将把它从第一个数组列表中删除。

However, when it runs I get an error: Intervals error: java.lang.IndexOutOfBoundsException: Index: 7, Size: 7 但是,当它运行时,我得到一个错误: Intervals error: java.lang.IndexOutOfBoundsException: Index: 7, Size: 7

ArrayList<String> customer = new ArrayList<String>(Arrays.asList(list));

int customerlist = customer.size();

while (line.isEmpty())
        {
            for (int x = 0; x < customerlist; x++ )
            {
                try
                {
                    Thread.sleep(intervals * 1000);   //Sleep method to hold the arrival time by 1-2 seconds. 
                    int cus = (int) (Math.random() * customerlist);   //Random customer is picked here. 
                    String new_cus = customer.get(cus);   //New customer object is created ere.
                    line.add(new_cus);   //Customer objects are added to the empty LinkedList queue.
                    customer.remove(cus);

                    //For loop statement to outputting the queue.
                    for (String s : line)
                    {
                        System.out.print("[" + s.toString() + " " + "]"); //Outputting each customer and using the ".name" method so customers are readable.
                    }
                    //Outputting the whole queue and stating who has joined the queue.
                    System.out.println("\n" + "The queue has " + line.size() + " customers so far" + "\n" + 
                    new_cus.toString() + " Has Joined the Queue " + " <=== WAITING" + "\n");
                }
                catch(Exception e)   //ERROR handler for sleep method.
                {
                    System.out.println("Intervals error: " + e);   //Outputting the ERROR message.
                    System.exit(0);   //If ERROR found exit system.
                }

            }
        }

You are removing from the array you are effectively iterating over, yet not updating the condition accordingly. 您正在从数组中进行有效的迭代,但是没有相应地更新条件。

Change: 更改:

for (int x = 0; x < customerlist; x++)

to

for (int x = 0; x < customer.size(); x++)

(Or better yet, use an iterator over the underlying ArrayList so that you can remove safely using the Iterator.remove() function.) (或者更好的方法是,在基础ArrayList使用迭代器,以便可以使用Iterator.remove()函数安全地删除。)

Also change the line: 同时更改行:

int cus = (int) (Math.random() * customerlist);

to

int cus = (int) (Math.random() * customer.size());

This is the problem: 这就是问题:

int cus = (int) (Math.random() * customerlist); 

That's fine (although not as clean as calling Random.nextInt ) for the first iteration - but afterwards, customer.size() has changed (as the element has been removed) but customerlist is still the same. 第一次迭代就可以了(虽然不像调用Random.nextInt那样干净)-但是之后, customer.size()已更改(因为元素已删除),但customerlist仍然相同。 So on the next iteration, you're picking an element in the wrong range. 因此,在下一次迭代中,您将选择错误范围内的元素。

To be honest, you'd be better off just using Collections.shuffle() to shuffle the original list - that's the result you want in the end, right? 老实说,您最好只使用Collections.shuffle()来整理原始列表-这就是您最终想要的结果,对吗?

add

customerlist--;

after

customer.remove(cus);

also, you can change 另外,你可以改变

for (int x = 0; x < customerlist; x++)

by 通过

for (int x = 0; x < customer.size(); x++)

but I think a call to the .size function at every loop uses more resources than a local variable. 但我认为在每个循环中对.size函数的调用都比局部变量使用更多的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM