我有新的数组列表,1个数组列表,其中有10个客户已插入。我正在运行一个循环,从arraylist中挑选一个随机客户,并将其添加到第二个数组列表中。但是,当我将客户插入第二个数组列表时,我会得到重复的数据。所以当循环运行后,将客户添加到第二个数组列表中时,它将从第一个数组列表中移除它。删除数组元素时出错
但是,当它运行时我得到一个错误: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.
}
}
}
试着铺设'cus'的价值。 – Bucket 2013-04-24 16:22:39
你只是试图把给定的客户按随机顺序放入列表中? – 2013-04-24 16:25:17
基本上我试图删除重复的客户被添加到我的第二个arraylist,但我需要客户被随机挑选出来。 – user1898552 2013-04-24 16:30:33