Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hello ,
I have a little issue to get my second iteration in the list below
My list : [1, 2 ,3, 4, 5, 6 , 7 ,8, 9, 10, 11, 12]
Here is my code to get the first 3 items :
int endOf = (context.offset)+3;
ArrayList<String> ids2 = new ArrayList<String>(ids.subList(context.offset, endOf));
System.out.println(ids2) ;
With my initialisation of context.offset = 0 .
*** Here is the result :
[1, 2, 3]
Now i would like to get the second iteration :
[4, 5, 6] and the third iteration : [7, 8, 9] and the rest of the iteration until my list is done .
How to do so ?
Thanks in advance .
Hi,
You can do it like this:
Here as text:
//initialize list
List<String> ids = new ArrayList<>();
for (int i=1;i<=12;i++){
ids.add(String.valueOf(i));
}
//inititalize vars
int split_every = 3;
int count = 0;
//logic
for (String s: ids){
count++;
if (count % split_every == 0){
List<String> ids2 = new ArrayList<>(ids.subList(count-split_every, count));
System.out.println(ids2);
}
}
output:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10, 11, 12]
Hi,
You can do it like this:
Here as text:
//initialize list
List<String> ids = new ArrayList<>();
for (int i=1;i<=12;i++){
ids.add(String.valueOf(i));
}
//inititalize vars
int split_every = 3;
int count = 0;
//logic
for (String s: ids){
count++;
if (count % split_every == 0){
List<String> ids2 = new ArrayList<>(ids.subList(count-split_every, count));
System.out.println(ids2);
}
}
output:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10, 11, 12]