ArrayDeque 大小和内容
我有以下代码。
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++;
}
根据我目前所知,它应该给我一个列表,或者在本例中是一个双端队列,大小为 100,有 100 个客户。
当我打印大小时,它验证了我的理论,但是当我打印内容时,它只返回索引 0-49。
for (int i = 0; i < q.size() ; i++) {
System.out.println("index "+ i + " "+ q.remove());
}
打印 arraydeque 与打印其他 arraylist 不同吗?
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.
for (int i = 0; i < q.size() ; i++) {
System.out.println("index "+ i + " "+ q.remove());
}
Is printing arraydeque different from printing other arraylists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您与 q.size() 进行比较,它在每次迭代时都会减小。这是因为remove()实际上修改了队列。
您可以使用迭代器打印元素而不修改队列:
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: