我有新的数组列表,1 个数组列表,其中已经插入了 10 个客户。我正在运行一个循环,它从数组列表中选择一个随机客户并将其添加到第二个数组列表中。但是,当我将客户插入第二个数组列表时,我得到了重复。因此,当循环在将客户添加到第二个数组列表后运行时,它会将其从第一个数组列表中删除。
但是,当它运行时出现错误: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.
}
}
}