英文:
Printing Stack of Arrays
问题
以下是您要翻译的代码部分:
import java.util.Stack;
public class ArraysOnStack {
public static void main(String[] args) {
int sizeOfArray = 5;
Stack<int[]> stack = fillStackWithArray(5);
printStack(stack);
}
private static void printStack(Stack<int[]> stack) {
while (!stack.empty()) {
int[] arr = stack.pop();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
private static Stack<int[]> fillStackWithArray (int size) {
Stack<int[]> stack = new Stack<>();
int[] arr = new int[size];
// 填充栈中的数组的某些操作。
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
arr[j] = size - i;
}
// 将数组推入栈中,进行某些操作。
stack.push(arr);
}
return stack;
}
}
希望这对您有所帮助。如果您需要进一步的协助,请随时提出。
英文:
Some operation is performed on an array in function fillStackWithArray()
and after the operation the array is pushed into the stack and is repeated again.
But the issue occurs when I try to print the stack. It gives me wrong answer.
Output:
<pre>
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
</pre>
Expected Output:
<pre>
5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1
</pre>
Code:
import java.util.Stack;
public class ArraysOnStack {
public static void main(String[] args) {
int sizeOfArray = 5;
Stack<int[]> stack = fillStackWithArray(5);
printStack(stack);
}
private static void printStack(Stack<int[]> stack) {
while (!stack.empty()) {
int[] arr = stack.pop();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
private static Stack<int[]> fillStackWithArray (int size) {
Stack<int[]> stack = new Stack<>();
int[] arr = new int[size];
// Some Operation that fills Stack with Arrays.
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
arr[j] = size - i;
}
// Pushing the array into stack on which some operation
// is performed.
stack.push(arr);
}
return stack;
}
}
PS: The operation is random to just fill the array. But my question is related to such a situation.
答案1
得分: 1
在 fillStackWithArray 方法中,你在栈中多次推入相同的 int[]
。在Java中,int[]
数组是 Object 的子类,因此它是一个对象类型。请在循环内创建一个新的 int[]
数组。
for (int i = 0; i < size; i++) {
int[] arr = new int[size];
// ...
}
英文:
You are pushing same int[]
in stack in fillStackWithArray. An int array in Java is a subclass of Object, therefore it's an object type. Create int[]
array inside loop.
for (int i = 0; i < size; i++) {
int[] arr = new int[size];
...
}
答案2
得分: 1
尝试这个。
private static Stack<int[]> fillStackWithArray(int size) {
Stack<int[]> stack = new Stack<>();
int[] arr = new int[size];
// 填充堆栈与数组的某些操作。
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
arr[j] = i + 1; // 修改过
}
// 将数组推入堆栈,对其进行某些操作。
stack.push(arr.clone()); // 修改过
}
return stack;
}
输出
5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1
英文:
Try this.
private static Stack<int[]> fillStackWithArray (int size) {
Stack<int[]> stack = new Stack<>();
int[] arr = new int[size];
// Some Operation that fills Stack with Arrays.
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
arr[j] = i + 1; // changed
}
// Pushing the array into stack on which some operation
// is performed.
stack.push(arr.clone()); // changed
}
return stack;
}
output
5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1
答案3
得分: 0
-
被推送到堆栈上的
int[]
数组在循环外部初始化一次。在后续的迭代中更新相同的数组。您需要在循环内部声明int[]
数组。 -
分配给变量
i
的值应该从 1 开始,直到i <= size
,因为输出需要从 5 打印到 1,并且由于使用了Stack
,插入顺序需要被反转,因为第一个推送的元素将被最后打印。因此,分配给arr[j]
的值应该是i
。
for (int i = 1; i <= size; i++) {
int[] arr = new int[size];
for (int j = 0; j < size; j++) {
arr[j] = i;
}
// 将数组推送到堆栈上,在堆栈上执行某些操作。
stack.push(arr);
}
英文:
-
The
int[]
being pushed onto the stack is initialized once outside the loop. The same array is being updated in subsequent iterations. You need to declare theint[]
array inside the loop. -
The value assigned to the variable
i
should begin from 1 toi <= size
since the output needs to be printed from 5 to 1 and as aStack
is being used, the insertion order needs to be reversed as the first element that is pushed will be printed last. Hence, the value assigned toarr[j]
should bei
.
for (int i = 1; i <= size; i++) {
int[] arr = new int[size];
for (int j = 0; j < size; j++) {
arr[j] = i;
}
// Pushing the array into stack on which some operation
// is performed.
stack.push(arr);
}
答案4
得分: 0
在fillStackWithArray()
函数中,你创建了一个名为arr
的单一数组,并且在每次迭代中都修改了同一个arr
。
要解决这个问题,每次外部循环迭代时都要创建一个新的数组。
要理解这个问题,可以阅读关于深拷贝和按引用传递的相关信息。
英文:
In the fillStackWithArray()
function you have created a single array arr
and you are changing the same arr
in each iteration.
To fix this, create a new array for each iteration of the outer loop.
To understand the issue, read about deep-copy and passing by reference.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论