gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:01:55 25 4
gpt4 key购买 nike

给定一个非空 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com