0

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.

1
  • Note that you are not initialising i2 inside your i1 loop so the inner loop will only run once. Commented Nov 24, 2015 at 23:26

3 Answers 3

1

You only create one array, and then you add that same array multiple times. Delete your early declaration, and declare (and initialize) it in your loop. Something like,

int[] x = {i1, i2};
// x[0] = i1;
// x[1] = i2;
iArray.add(x);

Which you could do without x on one line like,

iArray.add(new int[] {i1, i2});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, I could fix this.
0

You are reusing the same array. You create it once with int[] x = new int[2]; and then you insert the reference to the same object into iArray when you do iArray.add(x);

You can solve your issue by moving the creation of the array just before you assign its value. That way you'll always have a new instance to work on and to add to iArray.

Comments

0

You may be interested in a much simpler way of achieving this with Java 8 streams:

IntStream.range(0, 15).boxed().flatMap(i1 ->
    IntStream.range(0, 15).map(i2 -> new int[]{i1, i2}))
    .collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.