Results 1 to 5 of 5
- 03-27-2012, 03:18 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Stack help. Why will my peekAtTop() not work??
I am new to Data Structures and am trying to write my own code for a Stack. Everything seems to be working but my peekAtTop() method. I keep getting errors. To test this i just threw in a few strings and displayed them as i popped them off. Let me know if you see anything obvious that I am missing.
import java.util.ArrayList;
public class Stack<T>
{
private ArrayList<T> elements;
private int size;
public Stack()
{
size = 0;
elements = new ArrayList();
}
public void push (T addElement)
{
elements.add(size, addElement);
size++;
}
public T pop()
{
size -=2;
return elements.get(++size);
}
public boolean isEmpty()
{
if(size == 0)
return true;
else
return false;
}
public T peekAtTop()
{
return elements.get(size);
}
}
-
Re: Stack help. Why will my peekAtTop() not work??
What errors do you get and which lines cause them?
- 03-27-2012, 10:31 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: Stack help. Why will my peekAtTop() not work??
import java.util.*;
public class StackTester {
public static void main(String[] args)
{
System.out.print("This is to test a stack");
System.out.println(" ");
Stack<String> stack = new Stack<String>();
System.out.print(stack.isEmpty());
//add to stack
stack.push("poop");
stack.push("Doop");
stack.push("loop");
stack.push("moop");
System.out.println(stack.peekAtTop());
int count=0;
while(stack.isEmpty() != true)
{
System.out.println(stack.pop());
count++;
System.out.println(count+" removed!");
}
}
}
that is my tester program.
Here is the error.
This is to test a stack
trueException in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at Stack.peekAtTop(Stack.java:39)
at StackTester.main(StackTester.java:20)
- 03-27-2012, 10:40 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
Re: Stack help. Why will my peekAtTop() not work??
If there are n elements on the stack, size == n but there is no element #n (that would be the n+1st element); use stack.get(n-1) instead.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-27-2012, 10:43 PM #5
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 -
applet call dll work in Win2000 but not work in WinXP
By manhcuongtin4 in forum Java AppletsReplies: 1Last Post: 07-14-2011, 01:45 PM -
Stack
By kayln in forum EclipseReplies: 0Last Post: 03-04-2011, 08:14 PM -
Stack Overflow work around?
By Coukapecker in forum New To JavaReplies: 2Last Post: 03-14-2010, 08:49 PM -
How to set a stack size. -Xss doesn't work
By protonus in forum New To JavaReplies: 4Last Post: 06-27-2008, 06:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks