打印数组堆栈

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

Printing Stack of Arrays

问题

以下是您要翻译的代码部分:

  1. import java.util.Stack;
  2. public class ArraysOnStack {
  3. public static void main(String[] args) {
  4. int sizeOfArray = 5;
  5. Stack<int[]> stack = fillStackWithArray(5);
  6. printStack(stack);
  7. }
  8. private static void printStack(Stack<int[]> stack) {
  9. while (!stack.empty()) {
  10. int[] arr = stack.pop();
  11. for (int i = 0; i < arr.length; i++) {
  12. System.out.print(arr[i] + " ");
  13. }
  14. System.out.println();
  15. }
  16. }
  17. private static Stack<int[]> fillStackWithArray (int size) {
  18. Stack<int[]> stack = new Stack<>();
  19. int[] arr = new int[size];
  20. // 填充栈中的数组的某些操作。
  21. for (int i = 0; i < size; i++) {
  22. for (int j = 0; j < size; j++) {
  23. arr[j] = size - i;
  24. }
  25. // 将数组推入栈中,进行某些操作。
  26. stack.push(arr);
  27. }
  28. return stack;
  29. }
  30. }

希望这对您有所帮助。如果您需要进一步的协助,请随时提出。

英文:

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:

  1. import java.util.Stack;
  2. public class ArraysOnStack {
  3. public static void main(String[] args) {
  4. int sizeOfArray = 5;
  5. Stack&lt;int[]&gt; stack = fillStackWithArray(5);
  6. printStack(stack);
  7. }
  8. private static void printStack(Stack&lt;int[]&gt; stack) {
  9. while (!stack.empty()) {
  10. int[] arr = stack.pop();
  11. for (int i = 0; i &lt; arr.length; i++) {
  12. System.out.print(arr[i] + &quot; &quot;);
  13. }
  14. System.out.println();
  15. }
  16. }
  17. private static Stack&lt;int[]&gt; fillStackWithArray (int size) {
  18. Stack&lt;int[]&gt; stack = new Stack&lt;&gt;();
  19. int[] arr = new int[size];
  20. // Some Operation that fills Stack with Arrays.
  21. for (int i = 0; i &lt; size; i++) {
  22. for (int j = 0; j &lt; size; j++) {
  23. arr[j] = size - i;
  24. }
  25. // Pushing the array into stack on which some operation
  26. // is performed.
  27. stack.push(arr);
  28. }
  29. return stack;
  30. }
  31. }

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[] 数组。

  1. for (int i = 0; i < size; i++) {
  2. int[] arr = new int[size];
  3. // ...
  4. }
英文:

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.

  1. for (int i = 0; i &lt; size; i++) {
  2. int[] arr = new int[size];
  3. ...
  4. }

答案2

得分: 1

尝试这个。

  1. private static Stack<int[]> fillStackWithArray(int size) {
  2. Stack<int[]> stack = new Stack<>();
  3. int[] arr = new int[size];
  4. // 填充堆栈与数组的某些操作。
  5. for (int i = 0; i < size; i++) {
  6. for (int j = 0; j < size; j++) {
  7. arr[j] = i + 1; // 修改过
  8. }
  9. // 将数组推入堆栈,对其进行某些操作。
  10. stack.push(arr.clone()); // 修改过
  11. }
  12. return stack;
  13. }

输出

  1. 5 5 5 5 5
  2. 4 4 4 4 4
  3. 3 3 3 3 3
  4. 2 2 2 2 2
  5. 1 1 1 1 1
英文:

Try this.

  1. private static Stack&lt;int[]&gt; fillStackWithArray (int size) {
  2. Stack&lt;int[]&gt; stack = new Stack&lt;&gt;();
  3. int[] arr = new int[size];
  4. // Some Operation that fills Stack with Arrays.
  5. for (int i = 0; i &lt; size; i++) {
  6. for (int j = 0; j &lt; size; j++) {
  7. arr[j] = i + 1; // changed
  8. }
  9. // Pushing the array into stack on which some operation
  10. // is performed.
  11. stack.push(arr.clone()); // changed
  12. }
  13. return stack;
  14. }

output

  1. 5 5 5 5 5
  2. 4 4 4 4 4
  3. 3 3 3 3 3
  4. 2 2 2 2 2
  5. 1 1 1 1 1

答案3

得分: 0

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

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

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

  1. for (int i = 1; i &lt;= size; i++) {
  2. int[] arr = new int[size];
  3. for (int j = 0; j &lt; size; j++) {
  4. arr[j] = i;
  5. }
  6. // Pushing the array into stack on which some operation
  7. // is performed.
  8. stack.push(arr);
  9. }

答案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:

确定