打印数组堆栈

huangapple go评论68阅读模式
英文:

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&lt;int[]&gt; stack = fillStackWithArray(5);
        printStack(stack);
    }

    private static void printStack(Stack&lt;int[]&gt; stack) {
        while (!stack.empty()) {

            int[] arr = stack.pop();
            for (int i = 0; i &lt; arr.length; i++) {
                System.out.print(arr[i] + &quot; &quot;);
            }
            System.out.println();
        }
    }

    private static Stack&lt;int[]&gt; fillStackWithArray (int size) {
        Stack&lt;int[]&gt; stack = new Stack&lt;&gt;();
        int[] arr = new int[size];

        // Some Operation that fills Stack with Arrays.
        for (int i = 0; i &lt; size; i++) {
            for (int j = 0; j &lt; 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 &lt; 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&lt;int[]&gt; fillStackWithArray (int size) {
    Stack&lt;int[]&gt; stack = new Stack&lt;&gt;();
    int[] arr = new int[size];

    // Some Operation that fills Stack with Arrays.
    for (int i = 0; i &lt; size; i++) {
        for (int j = 0; j &lt; 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

  1. 被推送到堆栈上的 int[] 数组在循环外部初始化一次。在后续的迭代中更新相同的数组。您需要在循环内部声明 int[] 数组。

  2. 分配给变量 i 的值应该从 1 开始,直到 i &lt;= size,因为输出需要从 5 打印到 1,并且由于使用了 Stack,插入顺序需要被反转,因为第一个推送的元素将被最后打印。因此,分配给 arr[j] 的值应该是 i

for (int i = 1; i &lt;= size; i++) {
    int[] arr = new int[size];
    for (int j = 0; j &lt; size; j++) {
        arr[j] = i;
    }
    // 将数组推送到堆栈上,在堆栈上执行某些操作。
    stack.push(arr);
}
英文:
  1. 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 the int[] array inside the loop.

  2. The value assigned to the variable i should begin from 1 to i &lt;= size since the output needs to be printed from 5 to 1 and as a Stack 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 to arr[j] should be i.

for (int i = 1; i &lt;= size; i++) {
    int[] arr = new int[size];
    for (int j = 0; j &lt; 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.

huangapple
  • 本文由 发表于 2020年8月8日 16:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63313232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定