java - Converting this line of code to a for loop -
so in java, i'm trying execute:
num [0]=list.get(2); num [1]=list.get(4); num [2]=list.get(6); num [3]=list.get(8);
i have arraylist of integers called list , want put values in number indices starting @ 2 array of integers called num in indices 0,1,2,3,etc. problem is, i'm trying within loop i'm not sure how go it. here have:
for (int i=0; i<list.size()-2; i++){ num[i] = list.get(i+2); }
my problem here is, after incrementing i, arraylist goes next index instead of every other index. i've tried multiple variations of loop keep coming same problem.
this should it:
int max = list.size()/2; if(list.size()%2==0) max-=1; // prevents indexoutofbounds list lengths (int i=0; i<max; i++){ num[i] = list.get(i*2 + 2); }
Comments
Post a Comment