Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (anonymous):

Need serious help here...

OpenStudy (rsmith6559):

What's your question?

OpenStudy (anonymous):

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); } }

OpenStudy (blurbendy):

This is an example of how to declare an arraylist: ArrayList<String> arrayList = new ArrayList<String>();

OpenStudy (anonymous):

Okay, got that.

OpenStudy (blurbendy):

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?

OpenStudy (anonymous):

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);

OpenStudy (anonymous):

Stack<String> stack1 = new Stack<String>(); stack1.push(?);

OpenStudy (blurbendy):

I described stack wrong -- I was thinking of a queue. A stack is Last In First Out.

OpenStudy (blurbendy):

but what is your question now?

OpenStudy (anonymous):

how can I add an array value to the statement > stack1.push(?);

OpenStudy (anonymous):

aList[1]?

OpenStudy (anonymous):

stack1.push(aList[1]); ?

OpenStudy (blurbendy):

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?

OpenStudy (anonymous):

Yes, but I'm getting an error.

OpenStudy (blurbendy):

what does it say

OpenStudy (anonymous):

Error(39, 26) java: array required, but java.util.ArrayList<java.lang.String> Found

OpenStudy (blurbendy):

when you declared your arraylist, you put ArrayList<String> aList = new ArrayList<String>(5); The (5) is not necessary, just try ()

OpenStudy (blurbendy):

the arraylist will grow dynamically and you dont need to set its size

OpenStudy (blurbendy):

ah, i see the problem

OpenStudy (blurbendy):

stack1.push(aList.get(1)); is the command you need

OpenStudy (anonymous):

aList.get(2)

OpenStudy (anonymous):

Yes, that seems to work.

OpenStudy (blurbendy):

cool

OpenStudy (anonymous):

Nice, thanks! Now all I need is to add top() to retrieve element...

OpenStudy (blurbendy):

top will retrieve the last element inserted into the stack

OpenStudy (blurbendy):

so, this is the last element in the arraylist. any idea how to get this index?

OpenStudy (anonymous):

Nope. stack1.top();?

OpenStudy (blurbendy):

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?

OpenStudy (anonymous):

aList.size()-1

OpenStudy (anonymous):

?

OpenStudy (blurbendy):

perfect

OpenStudy (anonymous):

But now I gotta create my own Stack class :/ > Do NOT use the predefined java Stack class, and create your own Stack.java instead.

OpenStudy (anonymous):

That's what it sounds like to me..

OpenStudy (blurbendy):

can you still use an arraylist?

OpenStudy (anonymous):

Yeah

OpenStudy (blurbendy):

Do you know about private instance variables?

OpenStudy (anonymous):

Somewhat

OpenStudy (blurbendy):

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.

OpenStudy (blurbendy):

you might need to put static after private, but I'm not sure.

OpenStudy (anonymous):

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 ? ; }

OpenStudy (blurbendy):

without using an arraylist?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!