ArrayList的二维数组

huangapple go评论60阅读模式
英文:

ArrayList of 2 Dimensional arrays

问题

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

Note: The code provided was already in Java, so there wasn't much translation needed. The code snippet demonstrates an issue with overwriting or corrupting previous elements in an ArrayList of 2D arrays. The issue is due to the fact that the same 'coeff' array is being added twice to the ArrayList, so any changes made to 'coeff' after the first addition affect both instances in the list. To fix this, you need to create a new 'coeff' array for each addition to the ArrayList.

英文:

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&lt;float[][]&gt; basisCoeffs;
		basisCoeffs = new ArrayList&lt;float [][]&gt;(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&#39;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]);
		
	}

}

答案1

得分: 1

在这里,您将数组添加到ArrayList中,然后修改该数组,然后再次将其添加到ArrayList中。因此,在ArrayList中有两个相同数组的副本。我认为您在这里混淆了基本类型和对象。数组是对象,因此它们可以被修改。当您从ArrayList中获取元素时,您会看到两个元素都指向同一个数组,您已经对其进行了修改,因此您会得到修改后的值。如果您不希望出现这种行为,只需在将其添加到ArrayList时克隆该数组。类似于 basicCoeffs.add(coeff.clone());

英文:

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

答案2

得分: 1

以下是翻译好的内容:

发生的情况是,您有一个具有第一组值的 coeff 数组,您将其添加到列表中,一切正常,但是当您在将 coeff 添加到列表之前再次编辑它时,您也会编辑列表中位置 0 处的元素,因为 coeff 和列表中位置 0 处的元素都引用 Java 中的同一对象。一个选项是创建副本,另一个选项是将这两个数组分开。此外,由于我观察到您的维度是静态的,您可以直接将值添加到指定的位置,例如:

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]);
    
       // coeffs 的第一个值
       basisCoeffs.get(0)[0][0] = 0;
       basisCoeffs.get(0)[0][1] = 100;
       basisCoeffs.get(0)[0][2] = -50;
       
       // 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]);
    }
}
英文:

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&lt;float[][]&gt; basisCoeffs = new ArrayList&lt;float [][]&gt;(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]);
    }
}

答案3

得分: 0

在Java中,数组是可变的并且按引用传递(或者更确切地说,按引用值传递)。这意味着如果你改变了数组中的一个元素,引用也会发生改变。那么,为了避免这些副作用,我们需要做些什么呢?

你可以封装列表和数组,然后将对象的副本添加到数组中。

如果你使用的是Java 9或更高版本,你可以使用List<float[][]> basisCoeffs = List.of(coeff)将其项添加为不可变列表。

你可以在这里阅读更多关于可变和不可变的信息:https://stackoverflow.com/questions/3162665/immutable-class

英文:

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&lt;float[][]&gt; basisCoeffs = List.of(coeff) to add its Item as an immutable list.

you can read more about mutables and immutables here: https://stackoverflow.com/questions/3162665/immutable-class

huangapple
  • 本文由 发表于 2020年9月15日 00:27:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63888313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定