简体   繁体   English

二维数组的 ArrayList

[英]ArrayList of 2 Dimensional arrays

I created a very simple program to create an ArrayList of 2 Dimensional arrays of floats.我创建了一个非常简单的程序来创建一个二维浮点数组的 ArrayList。 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]);
        
    }

}

Here you add the array to the ArrayList, you modify that array, then you add it to the ArrayList a second time.在这里,您将数组添加到 ArrayList,修改该数组,然后再次将其添加到 ArrayList。 So you have two copies of the same array in the ArrayList.因此,您在 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.当您从 ArrayList 中取出元素时,您会看到两个元素都指向您修改过的同一个数组,因此您可以将修改后的值取出。 If you don't want that behavior, just clone the array when you add it to the ArrayList.如果您不想要这种行为,只需在将数组添加到 ArrayList 时克隆它即可。 Something like basicCoeffs.add(coeff.clone());类似basicCoeffs.add(coeff.clone()); . .

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.发生的情况是,您拥有带有第一个值的coeff数组,将其添加到列表中,一切正常,但是当您在将其添加到列表之前再次编辑coeff时,您还编辑了位于列表中位置 0 的那个列表,因为coeff作为列表中位置 0 的元素,它们引用 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]);
    }
}

Arrays in java are Mutable and pass by reference (well, pass by value of reference). java 中的数组是可变的并且是通过引用传递的(好吧,是通过引用的值传递)。 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.如果您使用的是 Java 9 或更高版本,则可以使用List<float[][]> basisCoeffs = List.of(coeff)将其 Item 添加为不可变列表。

you can read more about mutables and immutables here: Immutable class?你可以在这里阅读更多关于可变和不可变的信息:不可变类?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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