Java/Java Stack


Hi! I'm trying to create a Space class will represent the (explored) set of states as a stack. The Methods are used to add (push) a state and to remove (pop) a state. This class only adds and removes State objects, and has no knowledge about a State’s data structures or methods. So far, I have this code written but it doesn't seem to be working. This is anything wrong with this code? Thank You

class Space{
  Object [] Stack;
  int top = -1;
  void adds(int a)
     Stack[top]= a ;

   Object remove()
     return Stack[top+1];

You need to initialize your Stack to an array. Right now it's null so it won't work.

If you want to have a stack of size 10, you can:
Object[] Stack = new Object[10];

Also, I see that you have adds take an int, while remove gives an Object. You should either change adds to take an Object, or change remove and Stack to be int.


All Answers

Answers by Expert:

Ask Experts


Artemus Harper


I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.


I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Washington State University

MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2017 All rights reserved.