ArrayDeque size and contents
I've got the following code.
int clock;
ArrayDeque<Integer> q = new ArrayDeque<Integer>();
int customer = 1;
Random r = new Random();
for (clock = 0; clock <= 99; clock++) {
q.add(customer);
customer++;
}
Based on what I know so far it should give me a list, or in this case a deque, of size 100 with 100 customers.
When I print the size it validates my theory, however when I print the contents it only returns index 0-49.
开发者_JS百科 for (int i = 0; i < q.size() ; i++) {
System.out.println("index "+ i + " "+ q.remove());
}
Is printing arraydeque different from printing other arraylists?
The problem is that you compare with q.size(), which decreases on each iteration. This is because remove() actually modified the queue.
You can print the elements without modifying the queue using the iterator:
int i = 0;
for (int elem : q) {
System.out.println("index "+ i++ + " "+ elem);
}
精彩评论