|
-
January 24th, 2010, 05:57 PM
#1
Array stack generic class, non-static/static compile error
Ok so my assignment is to make an ArrayStack generic class, without a separate interface, and also a TestArrayStack with a "static main method" to test functionality with a sequence of integers. I have never worked with generic class until this moment and have never learned anything about it. Also, I have only been studying stacks/data structures for a week and so far I am terrible! I (think) that I created the generic class with no problems, here it is:
Code:
public class ArrayStack<T> {
private final int DEFAULT_CAPACITY = 500;
private T[] stack;
private int top;
public ArrayStack() {
top = 0;
stack = (T[])(new Object[DEFAULT_CAPACITY]);
}
public void push(T element) {
stack[top] = element;
top++;
}
public T pop() {
top--;
T result = stack[top];
stack[top] = null;
return result;
}
public boolean isEmpty(){
return top == -1;
}
public int size() {
return top+1;
}
}
class EmptyCollectionException extends Exception {
public EmptyCollectionException(String message)
{
super(message);
}
}
However, when I try to use ArrayStack.pop(); and ArrayStack.push(); in my static main method, obviously I get the error "non-static method cannot be referenced from a static context". How do I get around this?? Also, did I create my generic class for an array stack correctly? Thank you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|