// Setup dummy array
ArrayList<Integer> list = dateArray;
int counter = 1;
while (list.size() != 0) {
for (int j = 1; j < list.size(); j++)
{
//System.out.println(list.get(0) + " and " + list.get(j));
int difference = list.get(0) - list.get(j);
if (difference <6){
System.out.println(list.get(0) + " and " + list.get(j) + " and size is " +list.size());
counter= counter +1;
System.out.println ("Counter is " + counter);
if (counter >= 4){
System.out.println ("j = " + j + " Counter =" + counter);
if (j ==list.size()-1) {
System.out.println ("here " + counter);
break;
}
}
}
}
list.remove(0);
};
1 and 2 and size is 4
Counter is 2
1 and 3 and size is 4
Counter is 3
1 and 4 and size is 4
Counter is 4
here 4
2 and 3 and size is 3
Counter is 5
3 and 4 and size is 2
Counter is 6
here 6
理想的情況下,我希望它停止時將計數器4,不繼續執行「2和3的尺寸爲3」
非常感謝!
只是刪除了'while'循環,你想要做的只是執行一次'for'循環。你能否澄清一下,如果你的名單大小不是4,會發生什麼? – jhamon
您可以使用兩個'break'語句。首先把你帶出'for',其次將帶你離開'while'。當然,如果你通過達到條件終止'while循環會更好。 – PatNowak