0

I have new arraylist, 1 arraylist which has 10 customer already inserted. I'm running a loop which picks a random customer from the arraylist and adds it into the 2nd arraylist. However, I'm getting duplicates when I insert customer into the 2nd 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

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.
                }

            }
        }
4
  • Try flooring the value of cus. Commented Apr 24, 2013 at 16:22
  • Are you just trying to put the given customers into a list in a random order? Commented Apr 24, 2013 at 16:25
  • Basically im trying to remove duplicate customer being added into my 2nd arraylist, but I need the customer to be randomly picked in the first place. Commented Apr 24, 2013 at 16:30
  • Give us an example of what customer could be beforehand and what you would like line to look like afterward Commented Apr 24, 2013 at 16:36

3 Answers 3

1

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. 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?

Sign up to request clarification or add additional context in comments.

Comments

1

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.)

Also change the line:

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

to

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

2 Comments

I dont understand can you explain it please.
Oki im not getting a error however I need 10 customer to be added to the 2nd arraylist with the code you given me its only adding 5 customers. It worked to get 10 customer i just changed the loop value i < 10; Thank you
1

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.

3 Comments

This made it work aswell but I dont understand what does this mean customerlist--;
this means customerlist = customerlist - 1;
Thanks every I understand what I did wrong it was basically the customer.size() every time loop runs I need record the length of the arraylist with my pervious code this value was 10 even doe I kept removing customers from it. My bad !!! lol got ti working thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.