Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道是否有一种方法可以比较 arrayList 中的连续元素。我有这个
for (int j=0; j< Index.size(); j++) { if(Index.get(j) < Index.get(j -1) { System.out.println("Total number of shapes is " + sizer); } }
问题是我的代码在达到这一点时崩溃了,我不知道如何修复它。
提前致谢
您的索引从 0 开始,您试图获取第 -1 个元素,而不是尝试初始化j为 1
j
更改 for 循环的开始:
for (int j=1; j< Index.size(); j++) { if(Index.get(j) < Index.get(j -1) { System.out.println("Total number of shapes is " + sizer); } }