user2579782
user2579782

Reputation: 23

ArrayList of 2 Dimensional arrays

I created a very simple program to create an ArrayList of 2 Dimensional arrays of floats. But adding new elements in the list seems to overwrite or corrupt previous elements.

What am i doing wrong and how should this functionality be implemented?

import java.util.ArrayList;

public class multiDArrayTest {
    
    public static void main(String[] args) {
        float[][] coeff = new float[3][6];
        ArrayList<float[][]> basisCoeffs;
        basisCoeffs = new ArrayList<float [][]>(2);
        
        coeff[0][0] = 0;
        coeff[0][1] = 100;
        coeff[0][2] = -50;
        basisCoeffs.add(coeff);
        
        
        coeff[0][0] = 50;
        coeff[0][1] = 200;
        coeff[0][2] = -400;
        
        basisCoeffs.add(coeff);
        
        System.out.println(basisCoeffs.get(0)[0][0]);
        System.out.println(basisCoeffs.get(0)[0][1]);
        System.out.println(basisCoeffs.get(0)[0][2]);
        
        //I should get 0 100 -50 50, but i don't? Where does it go ??
        
        System.out.println(basisCoeffs.get(1)[0][0]);
        System.out.println(basisCoeffs.get(1)[0][1]);
        System.out.println(basisCoeffs.get(1)[0][2]);
        
    }

}

Upvotes: 0

Views: 68

Answers (3)

JAGC
JAGC

Reputation: 68

What happens is that you have the coeff array with the first values, you add it to the list and everything is fine, but when you edit coeff again before adding it to the list, you also edit the one that is in position 0 of the list, since both coeff as the element in position 0 of the list they refer to the same object in Java. One option would be to create a copy and another to have the two arrays separately. Also, since I observe that your dimensions are static, you can directly add the values to the designated positions, for example:

import java.util.ArrayList;

public class multiDArrayTest {
   public static void main(String[] args) {
       ArrayList<float[][]> basisCoeffs = new ArrayList<float [][]>(2);

       basisCoeffs.add(new float[3][6]);
       basisCoeffs.add(new float[3][6]);
    
       // First values of coeffs
       basisCoeffs.get(0)[0][0] = 0;
       basisCoeffs.get(0)[0][1] = 100;
       basisCoeffs.get(0)[0][2] = -50;
       
       // Second values of coeffs
       basisCoeffs.get(1)[0][0] = 50;
       basisCoeffs.get(1)[0][1] = 200;
       basisCoeffs.get(1)[0][2] = -400;
        
       System.out.println(basisCoeffs.get(0)[0][0]);
       System.out.println(basisCoeffs.get(0)[0][1]);
       System.out.println(basisCoeffs.get(0)[0][2]);
        
       System.out.println(basisCoeffs.get(1)[0][0]);
       System.out.println(basisCoeffs.get(1)[0][1]);
       System.out.println(basisCoeffs.get(1)[0][2]);
    }
}

Upvotes: 1

MoRtEzA TM
MoRtEzA TM

Reputation: 106

Arrays in java are Mutable and pass by reference (well, pass by value of reference). this means is you change an element in an array, the reference is changed. So what do we have to do to avoid these side effects?

You can encapsulate Lists and arrays and just add a copy of objects into arrays.

if you're using Java 9 or later you can use List<float[][]> basisCoeffs = List.of(coeff) to add its Item as an immutable list.

you can read more about mutables and immutables here: Immutable class?

Upvotes: 0

Charlie Armstrong
Charlie Armstrong

Reputation: 2342

Here you add the array to the ArrayList, you modify that array, then you add it to the ArrayList a second time. So you have two copies of the same array in the ArrayList. I think you are confusing primitives and objects here. Arrays are objects, so they can be modified. When you get the elements out of the ArrayList, you see both elements point to that same array, which you modified, so you get the modified values back out. If you don't want that behavior, just clone the array when you add it to the ArrayList. Something like basicCoeffs.add(coeff.clone());.

Upvotes: 1

Related Questions