java.util.ArrayList 循环遍历循环内的其余元素

标签 java

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

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);
    }
}

关于java.util.ArrayList 循环遍历循环内的其余元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29448652/

相关文章:

android - 方法调用 'toString' 可能会产生 'java.lang.NullPointerException' ?

java - 如何旋转SeekBar上的TextView?

用于将 TXT 读入 hashmap 的 java 类

java - 如何获取由特定字符集的一系列 ASCII 值表示的字符?

java - Android:onClickListener方法未到达结束,因为它先结束

java - Apache httpcomponents-client - CloseableHttpResponse 方法不起作用

java - 如何使用Java OpenCV

java - Set 的内部顺序怎么会被破坏呢?

java - 如何允许 "/api/**"通过我的基本身份验证配置并进入 Spring Security 中的 oauth 配置

java - 登录 https 网站并仅使用核心 Java API 下载页面