java.util.ArrayList通过循环中的rest元素循环

java.util.ArrayList通过循环中的rest元素循环,java,Java,给定一个非空的ArrayList,在迭代该列表时循环rest元素的最优雅方式是什么 Give an ArrayList instance 'exampleList' contains five strings: ["A", "B", "C", "D", "E"] 在循环过程中: for(String s : exampleList){ // when s is "A", I want to loop through "B"-"E", inside this loop // when s i

给定一个非空的ArrayList,在迭代该列表时循环rest元素的最优雅方式是什么

Give an ArrayList instance 'exampleList' contains five strings: ["A", "B", "C", "D", "E"]
在循环过程中:

for(String s : exampleList){
 // when s is "A", I want to loop through "B"-"E", inside this loop
 // when s is "B", I want to loop through "C"-"E", inside this loop
 // when s is "C", I want to loop through "D"-"E", inside this loop
}

最好的方法可能是使用传统的for循环:

for (int i=0; i<exampleList.size(); i++) {
    String s = exampleList.get(i);
    for (int j=i+1; j<exampleList.size(); j++) {
         String other = exampleList.get(j);
    }
}

我同意@Eran-answer-traditional for-loop,但我尝试使用迭代器

    List<String> exampleList = new ArrayList<String>(Arrays.asList("a", "b", "c"));
    Iterator<String> iterator = exampleList.iterator();
    while (iterator.hasNext()) {
        int start=exampleList.indexOf(iterator.next());
        List lst = exampleList.subList(start,exampleList.size());
        for(int i=0; i< lst.size() ; i++)
            System.out.println(lst.get(i));
    }
 }
您也可以使用stream的skip,这使得代码看起来很好看

List<String> coreModules = new ArrayList<String>(Arrays.asList("A","B","C","D"));
    for(int a=0;a<coreModules.size();a++){
        coreModules.stream().skip(a).forEach(item -> System.out.println(item));
    }
虽然需要Java1.8,但看起来很优雅

是流的文档,它有许多这样有用的过滤器