for my project, I need an ArrayList, that saves a value (if the if-statement turns true) of the counting index in a do-while-loop . After the loop, the list should print out the wanted and various values. However, all values are equal to the last value of the counting index.
During searching for a solution (without success), I fount out, that the ArrayList should only be used to store objects. That could somehow explain, that the ArrayList has saved the object, not the wanted values. Nevertheless, I need a dynamic list for my loop with nested loops, which is processed more than 500 times, and multiple if-statements, therefore the ArrayList.
How to fix this problem? Any alternatives?
An easy script to describe my problem:
import java.util.ArrayList;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i1=0;
int i2=0;
ArrayList<int[]> iArray = new ArrayList<int[]>();
int[] x = new int[2];
do{
i1++;
do{
i2++;
x[0] = i1;
x[1] = i2;
iArray.add(x);
}while(i2<15);
}while(i1<15);
for(int[] i : iArray){
System.out.println("line 1: " + i[0] + " line 2: " + i[1]);
}
}
}
console output:
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
line 1: 15 line 2: 29
Thanks in advance.