我有新的數組列表,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