除非我将我的数组声明为具有相同内容的新数组,否则该数组会在我的 while 循环中自行更改
Unless I declare my array as a new array with the same contents, the array changes itself in my while loop
What I'm trying to accomplish
所以我试图在数组中获得可能的翻转,同时还使用堆栈。
例如:
{3, 2, 1, 4} 会给我可能的翻转 [ {2, 3, 1, 4}, {1, 2, 3, 4}, {4, 1 , 3, 2}]
{3, 6, 1, 7} 会给我可能的翻转 [ {6, 3, 1, 7}, {1, 6, 3, 7}, {7, 1 , 6, 3}]
我有一个单独的循环来实现反转(粗体),所以这不是问题。
目前手头的问题是,当我得到其他可能的翻转时,我的 while 循环将我的测试数组的内容更改为我的临时数组的内容(希望代码片段突出显示这一点)。我试图复制测试数组,但它也改变了它的内容。
下面的代码有效,但正如您所想象的,我希望能够输入我自己的数字并让它起作用等等等等
public static void flipForSuccessors() {
int[] test = {3, 2, 1, 4};
int thePurplePointer = 1;
System.out.println("Test is " + Arrays.toString(test));
System.out.println("==================");
int[] tempList = test;
Stack<Integer> flipStack = new Stack<>();
while(thePurplePointer < test.length - 1) {
test = new int[]{3, 2, 1, 4}; // Whenever I remove this from the while loop, it doesn't work anymore
tempList = test;
for(int i = 0; i < test.length - thePurplePointer; i++) {
flipStack.add(test[i]);
}
for(int i = 0; i < tempList.length-thePurplePointer; i++) {
tempList[i] = flipStack.pop();
}
System.out.println("Temp is " + Arrays.toString(tempList));
thePurplePointer++;
}
for(int i = 0; i < tempList.length / 2; i++) { // This all works
int tempInt = tempList[i];
tempList[i] = tempList[tempList.length - i - 1];
tempList[tempList.length - i - 1] = tempInt;
}
System.out.println(Arrays.toString(tempList));
System.out.println("==================");
}
我可能很愚蠢。如果有人想出解决办法,谢谢!
tempList = test
复制对数组的引用,而不是数组,因此tempList 和test 都引用同一个数组。要复制数组,请使用
tempList=test.clone();
What I'm trying to accomplish
所以我试图在数组中获得可能的翻转,同时还使用堆栈。
例如:
{3, 2, 1, 4} 会给我可能的翻转 [ {2, 3, 1, 4}, {1, 2, 3, 4}, {4, 1 , 3, 2}]
{3, 6, 1, 7} 会给我可能的翻转 [ {6, 3, 1, 7}, {1, 6, 3, 7}, {7, 1 , 6, 3}]
我有一个单独的循环来实现反转(粗体),所以这不是问题。
目前手头的问题是,当我得到其他可能的翻转时,我的 while 循环将我的测试数组的内容更改为我的临时数组的内容(希望代码片段突出显示这一点)。我试图复制测试数组,但它也改变了它的内容。 下面的代码有效,但正如您所想象的,我希望能够输入我自己的数字并让它起作用等等等等
public static void flipForSuccessors() {
int[] test = {3, 2, 1, 4};
int thePurplePointer = 1;
System.out.println("Test is " + Arrays.toString(test));
System.out.println("==================");
int[] tempList = test;
Stack<Integer> flipStack = new Stack<>();
while(thePurplePointer < test.length - 1) {
test = new int[]{3, 2, 1, 4}; // Whenever I remove this from the while loop, it doesn't work anymore
tempList = test;
for(int i = 0; i < test.length - thePurplePointer; i++) {
flipStack.add(test[i]);
}
for(int i = 0; i < tempList.length-thePurplePointer; i++) {
tempList[i] = flipStack.pop();
}
System.out.println("Temp is " + Arrays.toString(tempList));
thePurplePointer++;
}
for(int i = 0; i < tempList.length / 2; i++) { // This all works
int tempInt = tempList[i];
tempList[i] = tempList[tempList.length - i - 1];
tempList[tempList.length - i - 1] = tempInt;
}
System.out.println(Arrays.toString(tempList));
System.out.println("==================");
}
我可能很愚蠢。如果有人想出解决办法,谢谢!
tempList = test
复制对数组的引用,而不是数组,因此tempList 和test 都引用同一个数组。要复制数组,请使用
tempList=test.clone();