Stack Collection Doubt

Stack is a class in java . And we are allowed to create array of objects.

Stack[] B =new Stack[5];

B[0].push(“1”);

B[1].push(1);

System.out.println(B[0].peek());

System.out.println(B[1].peek());

But on compilation I get this.

So How to create Array of Stacks?

to get rid of this use
ArrayList<Stack<Integer>> a = new ArrayList<>();
instead of
Stack<Integer> A[]=new Stack[q+1];

1 Like