0


I am trying to create an array of stacks, in which each stack within the array is of type int.

If I create the array like this:  Stack<Integer>[] numbers = new Stack<Integer>[3]; , there is the compile error  "Cannot create a generic array of Stack<Integer>". So, I am trying to create the array of Stacks with the wildcard type instead of Integer,  and it then does not have this error.

However, if I then try to push an int into one of the stacks (of wildcard "?" type) like this:  this.numbers[stackIndex].push(i); , there is the compile error  "The method push(capture#1-of ?) in the type Stack<capture#1-of ?> is not applicable for the arguments (int)".

So, how can I properly instantiate an array of Stacks of type int? As of now I am not able to perform push/pop operations on these Stacks...


My reasoning in this is an attempt to program the Tower of Hanoi game. I wanted each of the three rods to be a Stack of type int, each ring to be represented as an int, and the three rods together to be contained as an array of the three Stacks.


Here is some example code:
import java.util.Stack;

public class StackTest {

    Stack<?>[] numbers;

    public StackTest(int stackLength) {
        this.numbers = new Stack<?>[stackLength];
    }

    public void fillStack(int stackIndex, int numRings) {
        for (int i = numRings; i >= 0; i--) {

            // this statement has a compile error!
            this.numbers[stackIndex].push(i);
        }
    }

    public static void main(String[] args) {
        int numberOfRods = 3;
        StackTest obj = new StackTest(numberOfRods);

        int rodNumber = 0, numberOfRings = 4;
        obj.fillStack(rodNumber, numberOfRings);
    }
} // end of StackTest


Ian Campbell
  • 2,678
  • 10
  • 56
  • 104

3 Answers3

2

It has to be a raw Stack[] or you can use List<Stack<YourClass>> lstStack = new ArrayList<Stack<YourClass>>().

In this case, I would prefer to use

List<Stack<Integer>> lstStack = new ArrayList<Stack<Integer>>(stackLength);
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thank you for the quick reply! However, when I change it to a raw `Stack[]` I get the warning "`Stack is a raw type. References to generic type Stack should be parameterized`". Is there a way to avoid this warning? – Ian Campbell Mar 20 '13 at 17:10
  • @IanCampbell yes, use the second way. – Luiggi Mendoza Mar 20 '13 at 17:12
  • Thanks again @Luiggi, however when I try this the second way I get the compile error "`The type List is not generic; it cannot be parameterized with arguments >"`. – Ian Campbell Mar 20 '13 at 17:33
  • 1
    @IanCampbell are you sure you're using `java.util.List`? – Luiggi Mendoza Mar 20 '13 at 17:34
  • Ah, thanks @Luiggi! Eclipse opted that I import `java.awt.List` before `java.util.List`, in which was *not* working. `java.util.List` *is* working though, thanks again! – Ian Campbell Mar 20 '13 at 17:38
  • @IanCampbell don't forget to mark this post as the answer if it helped you. – Luiggi Mendoza Mar 20 '13 at 17:53
  • Right right, thanks again @Luiggi. Hey why is polymorphism being used here, where it is of type `List` and using the `ArrayList` constructor? – Ian Campbell Mar 20 '13 at 17:56
  • 1
    @IanCampbell please refer to [What does it mean to “program to an interface”?](http://stackoverflow.com/q/383947/1065197) – Luiggi Mendoza Mar 20 '13 at 17:57
1

One solution could be:

public class StackInteger extends Stack<Integer> {
}

And then:

StackInteger[] numbers = new StackInteger[3];

Or even:

Stack<Integer>[] numbers = new StackInteger[3];
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • Thanks @sp00m, this is interesting, however when I do this I am getting the warning "`The serializable class StackInteger does not declare a static final serialVersionUID field of type long`" on the class declaration. – Ian Campbell Mar 20 '13 at 17:30
0

My guess is that you should push an Integer rather than an int:

this.numbers[stackIndex].push(Integer.valueOf(i));
Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
  • I like this solution, however when I change line 5 to `Stack numbers;` and line 8 to `this.numbers = new Stack[stackLength];`, I get the warning "`Type safety: The expression of type Stack[] needs unchecked conversion to conform to Stack[]`". – Ian Campbell Mar 20 '13 at 17:24