Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7600401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:54:04+00:00 2026-05-30T22:54:04+00:00

I need to fill a stack from an array, then print out the elements

  • 0

I need to fill a stack from an array, then print out the elements in order and then again reversing the order. What I’m having problems with is I’m getting an ArrayIndexOutOfBoundsException and cant figure out where it is coming from. I have tried running through the debug and it appears as I’m popping off elements it never reaches the last element. Below is my code:

public class arrayStack {

    private int top;
    private String[] storage;

    public arrayStack(int capacity)
    {
        storage = new String[capacity];
        top = -1;
    }

    public boolean isEmpty() {
        return (top == 0);
    }

    String peek() {
        return storage[top];    
    }

    String pop() {
        top--;
        return storage[top];
    }

    public void push(String str) {
        top++;
        storage[top] = str;
    }

}

StackMain.java:

public class StackMain {

    public static void main(String[] args) {
        //int j = 5;
        String[] list = new String[5];

        list[0] = "Beware";
        list[1] = "The";
        list[2] = "Ides";
        list[3] = "Of";
        list[4] = "March";

        arrayStack stack = new arrayStack(5);

        for(int i = 0; i < list.length; i++)
        {
            stack.push(list[i]);
        }

        for(int j = 0; j < list.length; j++)
            System.out.println(stack.pop());

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:54:05+00:00Added an answer on May 30, 2026 at 10:54 pm

    In pop(), you need to return the item that was popped, which is the item at the index of the old value of top. Cleanest way is to change the function to

    String pop() {
        return storage[top--];
    }
    

    Edit
    You also need to change isEmpty() to return (top == -1). You could also change the implementation to use size (the number of elements) instead of top (the index of the highest element) like others mentioned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Nightly, I need to fill a SQL Server 2005 table from an ODBC source
I need to backup the original data from a table so I can fill
So, I need to fill a bi-dimensional array in JavaScript and I'm doing it
I need to fill textboxes depending on the item selected in a combobox. I
For example, I need to fill lots of DataTables with SQLDataAdapter's Fill() method: DataAdapter1.Fill(DataTable1);
I have two fields that need to multiply each other and fill a third
Need to locate the following pattern: The letter I followed by a space then
I really need some help... I detail my problem, I need an array of
I've a Tlistview with 3 columns, I need from Tcollection object as this follow
I want to be able to remove multiple elements from a set while I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.