Need serious help here...
What's your question?
Well, I need to know how to add an array list to this import java.util.*; public class StackHW { public static void main(String args[]) { // Create a new, empty stack Stack<String> stack1 = new Stack<String>(); stack1.push("Frist (bottom)"); printStack(stack1); stack1.push("Second (middle)"); printStack(stack1); stack1.push("Third (top)"); printStack(stack1); stack1.pop(); printStack(stack1); stack1.pop(); printStack(stack1); stack1.pop(); printStack(stack1); } public static void printStack(Stack<String> s) { if (s.isEmpty()) System.out.println("You have nothing in your stack"); else System.out.printf("%s TOP\n", s); } }
This is an example of how to declare an arraylist: ArrayList<String> arrayList = new ArrayList<String>();
Okay, got that.
okay, now you have to make the arraylist behave like a stack. In a stack, everything is First In First Out, so anytime you "push" something to it, it goes behind everything that was there before. basically like how you form a line to see a movie or something. make sense?
ArrayList<String> aList = new ArrayList<String>(5); aList.add("One(bottom)"); aList.add("Two"); aList.add("Third"); aList.add("Fourth"); aList.add("First(Top)"); System.out.println(aList);
Stack<String> stack1 = new Stack<String>(); stack1.push(?);
I described stack wrong -- I was thinking of a queue. A stack is Last In First Out.
but what is your question now?
how can I add an array value to the statement > stack1.push(?);
aList[1]?
stack1.push(aList[1]); ?
stack1.push(aList[1]); will grab the value at aList[1] ("Two" in this case), and push that value to the stack Is that what you want?
Yes, but I'm getting an error.
what does it say
Error(39, 26) java: array required, but java.util.ArrayList<java.lang.String> Found
when you declared your arraylist, you put ArrayList<String> aList = new ArrayList<String>(5); The (5) is not necessary, just try ()
the arraylist will grow dynamically and you dont need to set its size
ah, i see the problem
stack1.push(aList.get(1)); is the command you need
aList.get(2)
Yes, that seems to work.
cool
Nice, thanks! Now all I need is to add top() to retrieve element...
top will retrieve the last element inserted into the stack
so, this is the last element in the arraylist. any idea how to get this index?
Nope. stack1.top();?
aList.size() will give you the total number of elements in the arraylist. how can we use this number to get the index of the last element in the arraylist?
aList.size()-1
?
perfect
But now I gotta create my own Stack class :/ > Do NOT use the predefined java Stack class, and create your own Stack.java instead.
That's what it sounds like to me..
can you still use an arraylist?
Yeah
Do you know about private instance variables?
Somewhat
You will need one to keep track of the arraylist as you modify it (add, remove, etc) import java.util.*; public class StackHW { private ArrayList<String> aList = ArrayList<String>(); public void push(String element) { //use the arraylist to add the element (you did this already in the previous example) } public static void main(String[] args) { StackHW myStack = new StackHW(); mystack.push("Hello"); } } Can you make push work? It's just one line of code.
you might need to put static after private, but I'm not sure.
Okay, I'm not sure if I got the top() method to work for me. public Object top() throws StackEmptyException { if(stack1.isEmpty()) throw new StackEmptyException("Stack is empty.") ; return ? ; }
without using an arraylist?
Join our real-time social learning platform and learn together with your friends!