import java.util.ArrayList;
import java.util.Arrays;
public class MyData {
public static void main(String args[])
{
ArrayList<String> arrayList = new ArrayList<String>(
Arrays.asList("1","2","3","4","5","6","7","8")
);
System.out.println("Size of AyyayList= "+arrayList.size());
for(int i=0;i<arrayList.size()/2;i++)
{
System.out.println(i+" "+(i+1));
}
}
}
my Output
Size of AyyayList= 8
0 1
1 2
2 3
3 4
Need Output
Size of AyyayList= 8
0 1
2 3
4 5
6 7
答案 0 :(得分:2)
在这种情况下,你需要:
for(int i=0;i<arrayList.size()/2;i++)
{
System.out.println(2*i+" "+(2*i+1));
}
答案 1 :(得分:1)
,而不是i++
写i = i + 2.
问题是,你只增加了一个
答案 2 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
for(int i=0;i<arrayList.size();i+=2){
System.out.println(i+" "+arrayList.get(i));
}