0

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:

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

Expected 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

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.

Serum
  • 307
  • 1
  • 3
  • 14

4 Answers4

1

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];
   ...
}
Eklavya
  • 17,618
  • 4
  • 28
  • 57
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
0
  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 <= 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 <= 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);
}
Ameya Pandilwar
  • 2,638
  • 28
  • 28
0

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.

Anuneet Anand
  • 331
  • 1
  • 9