Results 1 to 7 of 7
Thread: Stack ADT using array
- 03-02-2013, 02:09 AM #1
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Stack ADT using array
I'm looking to make my own version of the stack ADT using arrays (or arraylist).
I have created my own isEmpty() method. Here is part of my code:
Java Code:public interface StackADT<T> { void push(T element); // adds an element to the stack T pop(); // removes an element from the stack and returns it boolean isEmpty(); // returns true if the stack is empty and false otherwise T peek(); // returns top element from the stack without removing it void truncate(); // truncates the stack to the exact number of elements void setExpansionRule(char rule); // sets expansion to either doubling or increasing by 10 elements }I'm trying to use my isEmpty() method (also part of the stack class) inside other methods like pop(). Problem is it says: "T cannot be resolved to a variable, Stack cannot be resolved to a variable". I also tried Stack.isEmpty(), but it doesn't work. I'm really confused. Any suggestions?Java Code:public class Stack<T> implements StackADT<T> { private T[] array; public Stack(){ this.array = (T[]) new Object[50]; } public boolean isEmpty(){ for(T elem: array){ if(elem != null){ return false; } } return true; } public T pop(){ if(Stack<T>.isEmpty()){ //ERROR throw new EmptyStackException("Stack is empty"); } } }Last edited by Rom 88; 03-02-2013 at 03:39 AM.
- 03-02-2013, 02:38 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 636
- Rep Power
- 1
Re: Stack ADT using array
There are a number of potential problems here. I assume you are implementing your own stack backed up by an array.
You're trying to use the class name of Stack to reference a non-existent method isEmpty(). And your doing it in a static way so you would not be able to access and instance of the array. What you really want to do is define an isEmpty() method that checks to see if the array has any elements in it. This is usually done maintaining an index to the current element in the array and adjusting it during push and pop operations.
Also, you can't use Generic type parameters (T in your case) during execution as they are erased during class generation. Generic types and the type parameters are only meaningful to the compiler. You might also want to include your interface declaration of StackADT.
Regards,
JimLast edited by jim829; 03-02-2013 at 02:48 AM.
The Java™ Tutorial
YAT -- Yet Another Typo
- 03-02-2013, 02:52 AM #3
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Stack ADT using array
Sorry, I guess I should've posted my entire code, I did make an isEmpty() method, I just didn't paste it here. I'll edit the code. I'm not sure what you mean about the generics though...I was asked to implement the stack with generics using either an array or arraylist (but controlling all its operations). I read around and saw that in order to use an array with generics, I have to first make an array of objects and then cast it to T[] when inserting elements into the array.
And I just understood what you meant with having an index to keep the locations with the push/pop operations. I'll definitely be implementing that instead of my isEmpty method. But for the time being, how am I supposed to even call the isEmpty method..? Do I make an instance of the class stack inside its own class?Last edited by Rom 88; 03-02-2013 at 02:59 AM.
- 03-02-2013, 03:01 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 636
- Rep Power
- 1
Re: Stack ADT using array
Check out The Java™ Tutorials. They have a lot of good information there. And within the last year they greatly improved the section on generics.
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-02-2013, 03:10 AM #5
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Stack ADT using array
Just re-read and noticed you asked to show my implementation of stackADT, so I re-edited...and my problem isn't really generics (although I'm sure my code/knowledge on generics isn't perfect), but it's really how to be able to call the isEmpty method from my other methods. Do I make an instance of the stack class inside itself?
- 03-02-2013, 03:35 AM #6
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Stack ADT using array
I think I got it...is the answer simply to write if (isEmpty())? without any class preceding it?
- 03-02-2013, 03:58 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 636
- Rep Power
- 1
Re: Stack ADT using array
Not quite. You don't need to provide the isEmpty() method so someone who creates a stack can do a test. It might look something like this if a user were using your class:
This assumes the stack holds ints. However, internally to your Stack class the isEmpty method would check where the current element is in the array and return true or false if the stack is empty or not.Java Code:Stack stk = new Stack(10); // stack size of 10. .. stk.push(1); stk.push(2); if (stk.isEmpty() == false) { int a = stk.pop(); // do something with a; }
JimThe Java™ Tutorial
YAT -- Yet Another Typo
Similar Threads
-
Stack problem. Object array pushed into stack came out different.
By LoViNgHeArTy in forum New To JavaReplies: 2Last Post: 01-14-2012, 08:56 PM -
Implementing a stack as an array
By fam2315 in forum New To JavaReplies: 2Last Post: 06-22-2011, 03:55 PM -
trouble creating stack using array
By shashankc in forum New To JavaReplies: 5Last Post: 01-20-2011, 12:49 PM -
Traversing through a stack of objects, and puttin them info in an array
By szimme101 in forum New To JavaReplies: 1Last Post: 03-25-2008, 05:06 AM -
how to convert a Java array to a java stack?
By pompeez in forum New To JavaReplies: 2Last Post: 08-13-2007, 02:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks