【发布时间】:2016-08-26 18:07:07
【问题描述】:
我有一些元素的 ArrayList path = [1, 2, 5, 7]。现在我想从这些元素中获取值,例如:
1,2
2,5
5,7
我为此使用了列表迭代器,但我得到的是:
1,2
2,2
5,7
7,7
我看到了这个答案: how to get all combination of an arraylist? 但我不需要这样的所有组合。
我的代码是:
public class iterator {
public static void main(String[] args){
ArrayList<String> path=new ArrayList<>();
path.add("1");
path.add("2");
path.add("5");
path.add("7");
System.out.println(path);
ListIterator<String> itr = path.listIterator();
while(itr.hasNext())
{
System.out.println(itr.next()+ "," + itr.next());
System.out.println(itr.previous()+ "," + itr.next());
}
}
}
我的问题是如何获得所需的结果?
【问题讨论】:
-
您的一般要求是什么?如果您的数组是: [2, 3, 5, 6, 7, 8, 9] ,您想要什么组合?
-
那我想要:(2,3), (3,5), (5,6), (6,7), (7,8), (8,9)跨度>
标签: java arraylist listiterator