我怎样才能访问previous /下一个元素在一个ArrayList? [英] How can I access the previous/next element in an ArrayList?

查看:148
本文介绍了我怎样才能访问previous /下一个元素在一个ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个ArrayList迭代是这样的:

 为(T(T):名单){
  ...
}

当我这样做,我从来没有想过我不得不访问previous而这种元素的下一个元素。现在我的code是巨大的。它的费用很多,如果我把它改写:

 的for(int i = 0; I<则为list.size();我++){
  ...
}


解决方案

我想我找到了解决方案,它实际上是简单,但我已经不能删除这个帖子,因为它有答案..

我只是说 T T = list.get(我); 在第二个for循环,其他所有code保持不变。

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..

I just added T t = list.get(i); in the second for-loop, all other code remains unchanged.

这篇关于我怎样才能访问previous /下一个元素在一个ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆