Results 1 to 5 of 5
  1. #1
    MoozicFarm is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

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

    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    MoozicFarm is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Stack help. Why will my peekAtTop() not work??

    Quote Originally Posted by Fubarable View Post
    What errors do you get and which lines cause them?
    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)

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,372
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Stack help. Why will my peekAtTop() not work??

    In other words, arrays etc use zero based indexing: 0 to length-1.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 01-14-2012, 08:56 PM
  2. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  3. Stack
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 03-04-2011, 08:14 PM
  4. Stack Overflow work around?
    By Coukapecker in forum New To Java
    Replies: 2
    Last Post: 03-14-2010, 08:49 PM
  5. How to set a stack size. -Xss doesn't work
    By protonus in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 06:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •