如何制作动态二维数组并在其中存储随机排列的数组
How to make a dynamic 2D array and store a shuffled array in it
我创建了一个二维数组列表,它具有固定的数字或行,以及一个包含数字 1-4 的数组。我应该打乱数组,然后将该数组存储在数组列表中。但是,当我之后去打印整个 arraylist 时,它不匹配,而且看起来,它正在进行我的最后一次洗牌并为所有行打印它。
例如,我的输出之一是:
3、2、1、4
1, 2, 4, 3
2、1、3、4
2、3、4、1
2、3、4、1
2、3、4、1
2、3、4、1
2、3、4、1
有人可以帮我理解我的错误吗?
package practice;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
public class Practice {
public static void main(String[] args) {
//Make arraylist for teams
List < Integer[] > teamMatches = new ArrayList < > ();
//Array for team numbers
Integer[] teamNums = new Integer[] {
1,
2,
3,
4
};
for (int i = 0; i < 4; i++) {
//shuffle array
Collections.shuffle(Arrays.asList(teamNums));
//add array to arraylist
teamMatches.add(teamNums);
//print out
System.out.println(teamMatches.get(i)[0] + ", " + teamMatches.get(i)[1] + ", " +
teamMatches.get(i)[2] + ", " + teamMatches.get(i)[3]);
}
System.out.println("_____________________________");
//print out entire match array
for (int n = 0; n < 4; n++) {
System.out.println(teamMatches.get(n)[0] + ", " + teamMatches.get(n)[1] + ", " +
teamMatches.get(n)[2] + ", " + teamMatches.get(n)[3]);
}
}
当您将 teamNums 添加到 teamMatches 时,您将引用(指针)传递给相同的数组(相同的内存位置)。因此,当您在 for 循环之后打印时,您只会得到最后已知的洗牌,因为这就是数组的样子。
您必须为 for 循环的每次迭代声明一个新的数组变量。
尝试:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
public class Practice {
public static void main(String[] args) {
//Make arraylist for teams
List < Integer[] > teamMatches = new ArrayList < > ();
for (int i = 0; i < 4; i++) {
// *create new Array for team numbers
Integer[] teamNums = new Integer[] {1, 2, 3, 4};
//shuffle array
Collections.shuffle(Arrays.asList(teamNums));
//add array to arraylist
teamMatches.add(teamNums);
//print out
System.out.println(
teamMatches.get(i)[0] + ", "
+ teamMatches.get(i)[1] + ", "
+ teamMatches.get(i)[2] + ", "
+ teamMatches.get(i)[3]
);
}
System.out.println("_____________________________");
//print out entire match array
for (int n = 0; n < 4; n++) {
System.out.println(
teamMatches.get(n)[0] + ", "
+ teamMatches.get(n)[1] + ", "
+ teamMatches.get(n)[2] + ", "
+ teamMatches.get(n)[3]);
}
}
}
我创建了一个二维数组列表,它具有固定的数字或行,以及一个包含数字 1-4 的数组。我应该打乱数组,然后将该数组存储在数组列表中。但是,当我之后去打印整个 arraylist 时,它不匹配,而且看起来,它正在进行我的最后一次洗牌并为所有行打印它。
例如,我的输出之一是:
3、2、1、4
1, 2, 4, 3
2、1、3、4
2、3、4、1
2、3、4、1
2、3、4、1
2、3、4、1
2、3、4、1
有人可以帮我理解我的错误吗?
package practice;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
public class Practice {
public static void main(String[] args) {
//Make arraylist for teams
List < Integer[] > teamMatches = new ArrayList < > ();
//Array for team numbers
Integer[] teamNums = new Integer[] {
1,
2,
3,
4
};
for (int i = 0; i < 4; i++) {
//shuffle array
Collections.shuffle(Arrays.asList(teamNums));
//add array to arraylist
teamMatches.add(teamNums);
//print out
System.out.println(teamMatches.get(i)[0] + ", " + teamMatches.get(i)[1] + ", " +
teamMatches.get(i)[2] + ", " + teamMatches.get(i)[3]);
}
System.out.println("_____________________________");
//print out entire match array
for (int n = 0; n < 4; n++) {
System.out.println(teamMatches.get(n)[0] + ", " + teamMatches.get(n)[1] + ", " +
teamMatches.get(n)[2] + ", " + teamMatches.get(n)[3]);
}
}
当您将 teamNums 添加到 teamMatches 时,您将引用(指针)传递给相同的数组(相同的内存位置)。因此,当您在 for 循环之后打印时,您只会得到最后已知的洗牌,因为这就是数组的样子。
您必须为 for 循环的每次迭代声明一个新的数组变量。 尝试:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
public class Practice {
public static void main(String[] args) {
//Make arraylist for teams
List < Integer[] > teamMatches = new ArrayList < > ();
for (int i = 0; i < 4; i++) {
// *create new Array for team numbers
Integer[] teamNums = new Integer[] {1, 2, 3, 4};
//shuffle array
Collections.shuffle(Arrays.asList(teamNums));
//add array to arraylist
teamMatches.add(teamNums);
//print out
System.out.println(
teamMatches.get(i)[0] + ", "
+ teamMatches.get(i)[1] + ", "
+ teamMatches.get(i)[2] + ", "
+ teamMatches.get(i)[3]
);
}
System.out.println("_____________________________");
//print out entire match array
for (int n = 0; n < 4; n++) {
System.out.println(
teamMatches.get(n)[0] + ", "
+ teamMatches.get(n)[1] + ", "
+ teamMatches.get(n)[2] + ", "
+ teamMatches.get(n)[3]);
}
}
}