如何将数组的值添加到 ArrayList 而不是该数组的位置?
How to add the value of a array into a ArrayList in stead of the location of that array?
如果你给一个列表添加一个int[][],它不会添加数组的值,而是添加数组的位置。如何将数组的值添加到列表中。这是代码(这只是一个例子,可能有小错误):
public class Main {
int[][] matrix = new int[2][2];
List<int[2][2]> matrixList = new ArrayList<>();
public static void main(String[] args) {
for(int i = 0; i < 4; i++){
matrixList.add(matrix);
matrix = matrixCalc(matrix);
}
for(int i = 0; i < 4; i++) {
System.out.println(Arrays.deepToString(matrixList.get(i)));
}
public int[][] matrixCalc(int[][] m){
//do various calculations with m
...
return m;
}
}
}
我希望输出具有通过计算方法计算的不同矩阵。
我想要得到的输出示例:
{{0,1}{5,7}}
{{2,0}{2,4}}
{{8,1}{4,8}}
{{3,3}{7,9}}
我会这样得到输出(错误!)
{{3,3}{7,9}}
{{3,3}{7,9}}
{{3,3}{7,9}}
{{3,3}{7,9}}
试试:
public class Main {
int[][] matrix = new int[2][2];
List<int[2][2]> matrixList = new ArrayList<>();
public static void main(String[] args) {
for(int i = 0; i < 4; i++){
matrixList.add(matrixCalc(matrix));
}
for(int i = 0; i < 4; i++) {
System.out.println(Arrays.deepToString(matrixList.get(i)));
}
public int[][] matrixCalc(int[][] m){
int[][] newMatrix = new int[2][2];
... do calc then put results on newMatrix
return newMatrix;
}
}
}
编辑: 在 OP 发表评论后,我删除了我的整个答案(you can still find a screenshot of it here;或查看编辑历史)。
If I'm not mistaken, you send an empty Array to the method matrixCalc? For the calculations method I need a matrix with the same value as I just put on top of the stack.
在那种情况下有点不同,尽管问题仍然是对 matrix
的相同引用,所以我们必须更改它。要将数据从一个二维数组复制到一个新数组(具有新引用),我们应该制作一个单独的复制方法。在 Java 中有用于一维数组的 Arrays.copyOf
方法(或者 System.arraycopy
),但是对于二维数组,您应该自己创建一个方法。所以你的代码变成这样:
public class Main {
int[][] previousMatrix = new int[2][2];
List<int[2][2]> matrixList = new ArrayList<>();
public static void main(String[] args) {
for(int i = 0; i < 4; i++){
int[][] matrix = matrixCalc(previousMatrix);
matrixList.add(matrix);
previousMatrix = copy2dArray(matrix);
}
for(int i = 0; i < 4; i++) {
System.out.println(Arrays.deepToString(matrixList.get(i)));
}
}
public int[][] matrixCalc(int[][] m){
//do various calculations with m
...
return m;
}
private int[][] copy2dArray(int[][] original){
int[][] copy = new int[original.length][];
for(int i = 0; i < original.length; i++){
copy[i] = Arrays.copyOf(original[i], original[i].length);
}
return copy;
}
}
或者,您可以在 matrixCalc
方法的开头创建一个新的二维数组并填充 return ,而不是给定的参数。但是,由于我不知道您在 matrixCalc
方法中进行了什么样的计算,所以我无法真正举出一个例子。它看起来像这样:
public int[][] matrixCalc(int[][] m){
int[][] n = new int[2][2];
//do various calculations with m,
//but save the result in n
...
return n;
}
一些背景信息:
我正在编写的程序使用回溯。我需要将 boardstates 保存到堆栈中,还需要向板上添加东西。这里 [matrix] 代表棋盘, [matrixList] 代表保存的棋盘状态。当我进行计算时,我需要在更改某些内容后拥有当前的电路板。我需要在堆栈顶部添加新板并继续更改新板。
如果你给一个列表添加一个int[][],它不会添加数组的值,而是添加数组的位置。如何将数组的值添加到列表中。这是代码(这只是一个例子,可能有小错误):
public class Main {
int[][] matrix = new int[2][2];
List<int[2][2]> matrixList = new ArrayList<>();
public static void main(String[] args) {
for(int i = 0; i < 4; i++){
matrixList.add(matrix);
matrix = matrixCalc(matrix);
}
for(int i = 0; i < 4; i++) {
System.out.println(Arrays.deepToString(matrixList.get(i)));
}
public int[][] matrixCalc(int[][] m){
//do various calculations with m
...
return m;
}
}
}
我希望输出具有通过计算方法计算的不同矩阵。
我想要得到的输出示例:
{{0,1}{5,7}}
{{2,0}{2,4}}
{{8,1}{4,8}}
{{3,3}{7,9}}
我会这样得到输出(错误!)
{{3,3}{7,9}}
{{3,3}{7,9}}
{{3,3}{7,9}}
{{3,3}{7,9}}
试试:
public class Main {
int[][] matrix = new int[2][2];
List<int[2][2]> matrixList = new ArrayList<>();
public static void main(String[] args) {
for(int i = 0; i < 4; i++){
matrixList.add(matrixCalc(matrix));
}
for(int i = 0; i < 4; i++) {
System.out.println(Arrays.deepToString(matrixList.get(i)));
}
public int[][] matrixCalc(int[][] m){
int[][] newMatrix = new int[2][2];
... do calc then put results on newMatrix
return newMatrix;
}
}
}
编辑: 在 OP 发表评论后,我删除了我的整个答案(you can still find a screenshot of it here;或查看编辑历史)。
If I'm not mistaken, you send an empty Array to the method matrixCalc? For the calculations method I need a matrix with the same value as I just put on top of the stack.
在那种情况下有点不同,尽管问题仍然是对 matrix
的相同引用,所以我们必须更改它。要将数据从一个二维数组复制到一个新数组(具有新引用),我们应该制作一个单独的复制方法。在 Java 中有用于一维数组的 Arrays.copyOf
方法(或者 System.arraycopy
),但是对于二维数组,您应该自己创建一个方法。所以你的代码变成这样:
public class Main {
int[][] previousMatrix = new int[2][2];
List<int[2][2]> matrixList = new ArrayList<>();
public static void main(String[] args) {
for(int i = 0; i < 4; i++){
int[][] matrix = matrixCalc(previousMatrix);
matrixList.add(matrix);
previousMatrix = copy2dArray(matrix);
}
for(int i = 0; i < 4; i++) {
System.out.println(Arrays.deepToString(matrixList.get(i)));
}
}
public int[][] matrixCalc(int[][] m){
//do various calculations with m
...
return m;
}
private int[][] copy2dArray(int[][] original){
int[][] copy = new int[original.length][];
for(int i = 0; i < original.length; i++){
copy[i] = Arrays.copyOf(original[i], original[i].length);
}
return copy;
}
}
或者,您可以在 matrixCalc
方法的开头创建一个新的二维数组并填充 return ,而不是给定的参数。但是,由于我不知道您在 matrixCalc
方法中进行了什么样的计算,所以我无法真正举出一个例子。它看起来像这样:
public int[][] matrixCalc(int[][] m){
int[][] n = new int[2][2];
//do various calculations with m,
//but save the result in n
...
return n;
}
一些背景信息: 我正在编写的程序使用回溯。我需要将 boardstates 保存到堆栈中,还需要向板上添加东西。这里 [matrix] 代表棋盘, [matrixList] 代表保存的棋盘状态。当我进行计算时,我需要在更改某些内容后拥有当前的电路板。我需要在堆栈顶部添加新板并继续更改新板。