Reputation: 6391
I haven't done much by way of writing my own generic classes, but I'm trying to make my own ArrayStack class and having trouble understanding how to correctly write the constructor.
public class ArrayStack<T> implements List<T> {
private T[] a;
private int n;
public ArrayStack(T[] a) {
this.a = a;
}
}
And my main class that uses it:
public class ArrayStackTester {
public static void main(String[] args) {
ArrayStack<Integer> numbers = new ArrayStack<Integer>();
}
}
This produces a compilation error which says that The ArrayStack<Integer> is undefined
, so I obviously suspect a problem with the constructor in the ArrayStack
class.
I didn't include all the overriden List
methods for brevity.
Upvotes: 2
Views: 107
Reputation: 70267
You defined one constructor, which takes a T[]
. Then you tried to call a constructor which has no arguments. You need to define the constructor for new ArrayStack<Integer>()
to do what you need it to do.
public ArrayStack() {
// Initialize a (and possibly n) here as appropriate)
}
Upvotes: 2
Reputation: 235994
Try defining this no-args constructor first:
public ArrayStack() {
}
Or alternatively, pass along an array of the right type:
Integer[] array = new Integer[100];
ArrayStack<Integer> numbers = new ArrayStack<Integer>(array);
Upvotes: 3
Reputation: 3084
Is the ArrayStack class compiled?
There is a generic array, which causes troubles
How to create a generic array in Java?
Upvotes: 1