Java 用于循环覆盖数组(一旦完成)
在下面的代码中,我尝试制作一个数组洗牌器。这将使字符数组(Java 用于循环覆盖数组(一旦完成),java,arrays,for-loop,shuffle,Java,Arrays,For Loop,Shuffle,在下面的代码中,我尝试制作一个数组洗牌器。这将使字符数组(charlarray)按运行时的次数洗牌,并将每个数组存储在二维数组中,shuffledArray。问题是当程序完成for循环时,出于某种原因,它会将所有数组设置为最后添加的数组 谢谢你的帮助 import java.util.Random; public class Testing { public static void main(String[] args) { char[] charArray = {
charlarray
)按运行时的次数洗牌,并将每个数组存储在二维数组中,shuffledArray
。问题是当程序完成for循环时,出于某种原因,它会将所有数组设置为最后添加的数组
谢谢你的帮助
import java.util.Random;
public class Testing {
public static void main(String[] args) {
char[] charArray = {'a', 'b', 'c', 'd', 'e', 'f'}; //Array to be shuffled
int runTimes = 3; //Amount of shuffled arrays to create
char[][] shuffledArrays = new char[runTimes][charArray.length];;
for (int x = 0; x != runTimes; x++) {
shuffledArrays[x] = shuffleArray(charArray);
System.out.print("IN FOR LOOP: shuffledArrays[" + x + "] = ");
System.out.println(shuffledArrays[x]);
}
System.out.print("OUT OF FOR LOOP: shuffledArrays[0] = ");
System.out.println(shuffledArrays[0]);
System.out.print("OUT OF FOR LOOP: shuffledArrays[1] = ");
System.out.println(shuffledArrays[1]);
System.out.print("OUT OF FOR LOOP: shuffledArrays[2] = ");
System.out.println(shuffledArrays[2]);
}
private static char[] shuffleArray(char[] array) {
Random rand = new Random();
for (int x = 0; x < array.length; x++) {
int randomIndexToSwap = rand.nextInt(array.length);
char temp = array[randomIndexToSwap];
array[randomIndexToSwap] = array[x];
array[x] = temp;
}
return array;
}
}
import java.util.Random;
公共类测试{
公共静态void main(字符串[]args){
char[]charArray={'a','b','c','d','e','f'};//要洗牌的数组
int runTimes=3;//要创建的无序数组的数量
char[][]shuffledarray=新的char[runTimes][charArray.length];;
for(int x=0;x!=运行时;x++){
shuffleArray[x]=shuffleArray(charArray);
System.out.print(“IN-FOR-LOOP:shuffledArrays[“+x+”]=”);
System.out.println(shuffledArray[x]);
}
System.out.print(“FOR-out-OF-FOR-LOOP:shuffledArrays[0]=”;
System.out.println(shuffledArray[0]);
System.out.print(“FOR-out-OF-FOR-LOOP:shuffledArrays[1]=”;
System.out.println(shuffledArrays[1]);
System.out.print(“FOR-out-OF-FOR-LOOP:shuffledArrays[2]=”;
System.out.println(shuffledArrays[2]);
}
私有静态字符[]Shufferray(字符[]数组){
Random rand=新的Random();
对于(int x=0;x
由于总是返回相同的数组,最后一个数组将影响所有数组。您需要返回副本或传递副本,如:
private static char[] shuffleArray(char[] array) {
Random rand = new Random();
for (int x = 0; x < array.length; x++) {
int randomIndexToSwap = rand.nextInt(array.length);
char temp = array[randomIndexToSwap];
array[randomIndexToSwap] = array[x];
array[x] = temp;
}
return Arrays.copyOf(array, array.length);
}
private static char[]shufflarray(char[]array){
Random rand=新的Random();
对于(int x=0;x
您需要更改阵列的副本,而不是更改原始阵列,例如
private static char[] shuffleArray(char[] inArray) {
Random rand = new Random();
char[] array = inArray.clone();
for (int x = 0; x < array.length; x++) {
int randomIndexToSwap = rand.nextInt(array.length);
char temp = array[randomIndexToSwap];
array[randomIndexToSwap] = array[x];
array[x] = temp;
}
return array;
}
您观察到同一阵列的三种状态。for循环的第一次迭代的System.out.println()
语句打印第一个状态,即此时的状态,例如f、b、c、e、a、d
。然后在第二次迭代中再次洗牌相同的数组,例如a、e、b、f、c、d
。第三次迭代idem dito
现在shuffledArray[0]
,shuffledArray[1]
和shuffledArray[2]
指向同一个数组。您的shuffledArray
不会返回新数组。它返回传入的相同数组。您总是传入相同的数组。因此,您将相同的char[]
数组添加到shuffledArray
多次。它确实返回相同的数组,但它在shufflerray()
中被更改。这就是为什么FOR循环中的:
行打印出三个不同的数组,都是原始数组的无序版本。我不明白的是,为什么当它离开for循环时,它刚打印出来的所有数组现在都变成了相同的值,因为你在一次又一次地洗牌相同的数组。但是在最后,你仍然只有一个数组。考虑使用CopysSchffle:同意这个解决方案,但是你也可以指出OP应该改变<代码> char [][SouffEdLeabs] =新char [RunTime][CARARAMOR.SULL];tochar[]shuffledArrays=new char[runTimes][]代码>。目前,从未使用分配的第二维度。
IN FOR LOOP: shuffledArrays[0] = dcfaeb
IN FOR LOOP: shuffledArrays[1] = edcbaf
IN FOR LOOP: shuffledArrays[2] = bfaced
OUT OF FOR LOOP: shuffledArrays[0] = dcfaeb
OUT OF FOR LOOP: shuffledArrays[1] = edcbaf
OUT OF FOR LOOP: shuffledArrays[2] = bfaced