简体   繁体   中英

How to assign values of a counting index to an ArrayList? NOT the object of the counting index

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.

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});

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 .

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM