如何访问 ArrayList 中的上一个/下一个元素? [英] How can I access the previous/next element in an ArrayList?
本文介绍了如何访问 ArrayList 中的上一个/下一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以这种方式遍历 ArrayList:
I iterate through an ArrayList this way:
for (T t : list){
...
}
当我这样做的时候,我从没想过我必须访问这个元素的前一个和下一个元素.现在我的代码很大.如果我用以下方式重写它会花费很多:
When I did this, I never thought I had to access the previous and next elements of this element. Now my code is huge. It costs a lot if I rewrite it with:
for (int i = 0; i < list.size(); i++){
...
}
推荐答案
我觉得我找到的解决方案其实很简单,但是我不能删除这个帖子,因为它有答案..
I think I found the solution it is simple in fact, but I cannot delete this post already because it has answers..
我刚刚在第二个 for 循环中添加了 T t = list.get(i);
,所有其他代码保持不变.
I just added T t = list.get(i);
in the second for-loop, all other code remains unchanged.
这篇关于如何访问 ArrayList 中的上一个/下一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文