Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: array stack

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default array stack

     
     
    public class ThreeStackArray 
    {
    	static int row = 10;
    	static int col = 2;
    	int[][] stack = new int[row][col];
     
    	//place numbers into the first row of array
    	public void push(int num)
    	{
     
    		try
    		{
    			for(int i=0; i<row;i++)
    			{
    				if(stack[i][col-2]==0)
    				{
    					stack[i][col-2]=num;
    					break;
    				}
    				//checks if first stack is empty and if second stack
    				//is empty
    				else if(stack[i][col-1]==0 && stack[i][col-2] > 0)
    				{
    					stack[i][col-1]=num;
    					break;	
    				}
    				//checks if first stack is empty and if second stack
    				//is empty then check third stack
    				else if(stack[i][col]==0 && stack[i][col-2] > 0
    						&& stack[i][col-1]>0)
    				{
    					stack[i][col]=num;
    					break;
    				}
    				else
    				{
    					System.out.println("ERROR");
    				}
    			}
    		}
    		catch(Exception ex)
    		{
    			System.out.println("An error has occured "+ex);
    		}
     }
     
    	public void pop()
    	{
    		int count=1;
     
    		for(int i=0;i<row;i++)
    		{
    			for(int j=0;j<col;j++)
    			{
    				if(count==3)
    				{
    					//makes a line every third iteration
    					System.out.println("");
    					count=1;//setting count back to 0
    				}
    				System.out.print(stack[i][j]);
    			}
    			count++;
    		}	
    	}
     
    	public static void main(String[] args) 
    	{
    		ThreeStackArray a = new ThreeStackArray();
     
    		for(int i=0;i<30;i++)
    		{
    		a.push(i);
    		}
    		a.pop();
    	}
     
    }

    I found this program idea online. Make a array with three stacks. Either I jump out of bounds if I use "continue" instead of "break" in my loop in the push method. If I use break the output looks like:

    12900
    0000
    0000
    0000
    0000

    if I use continue the output looks like:

    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    1212
    1212
    1212
    1212
    1210
    Last edited by jocdrew21; May 17th, 2014 at 02:12 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: array stack

    Make a array with three stacks.
    Please describe what the program is supposed to do.

    Let's start with the error in the code you've posted. This is what I get when I run your code:
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
    1200
    0000
    0000
    0000
    0000
    You must analyze your code to determine why the error is occurring and fix it if you can. You might trying improving the error message to determine the source of the exception. If you can't find the source and a solution, come back.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: array stack

    One of the first things to do is to print out the exception stack trace:
    catch(Exception ex)
    {
        System.out.println("An error has occured "+ex);
        ex.printStackTrace();
    }
    This will tell you the exact location of the exception. Check the line of code indicated in the stack trace for errors. You may find it helpful to put in some System.out.println()s in that area to further understand the reason for the exception.

    Apart from that, go through Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics). Pay extra attention to the way an array is indexed in relation to its size/length.

  4. #4
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: array stack

    Here is a different approach. I want to make a 3 stacked array.

    import java.util.*;
     
    public class ArrayStack 
    {
    	private int SIZE =10;
    	private int MAXSIZE= 30;
    	private int location =0;
    	private int number;
    	ArrayStack[] arrayStack = new ArrayStack[MAXSIZE];
     
    	static Stack<Integer> st = new Stack<Integer>();
     
    	ArrayStack()
    	{
    		for(int i=0;i<MAXSIZE;i++)
    		{
    			arrayStack[i] = new ArrayStack(); 
    		}
    	}
     
    	boolean checkSize()
    	{
    		return st.size()==SIZE;
    	}
     
    	public void pushNumbersOntoStack(int num)
    	{
    		if(checkSize()) //if stack is full create a new one
    		{
    			for(int i=0;i<st.size();i++)//add stack to array
    			{
    				arrayStack[location++].number=st.pop();
    			}
    			st = new Stack<Integer>();//creating new stack
    		}
    		st.push(num);
    	}
     
    	public void getNumberfromArray()
    	{
    		for(int i=0;i<MAXSIZE;i++)
    		{
    			System.out.println(arrayStack[i].number);	
    		}
    	}
     
    	public static void main(String[] args)
    	{
    		ArrayStack myStack = new ArrayStack();
     
    		for(int i=0;i<30;i++)
    		{
    		myStack.pushNumbersOntoStack(i);
    		}
     
    		myStack.getNumberfromArray();
    	}
     
    }

    errors
    Exception in thread "main" java.lang.StackOverflowError
    	at ArrayStack.<init>(ArrayStack.java:13)
    	at ArrayStack.<init>(ArrayStack.java:17)
    	at ArrayStack.<init>(ArrayStack.java:17)
    	at ArrayStack.<init>(ArrayStack.java:17)
    	at ArrayStack.<init>(ArrayStack.java:17)
    	at ArrayStack.<init>(ArrayStack.java:17)

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: array stack

    Take a look at StackOverflowError (Java Platform SE 7 ) to see why might this error occur. (Note, "stack" in the API doc refers to JVM internals.)

    The StackOverflowError is many ways is similar to the problem discussed in another thread. My reply there, http://www.javaprogrammingforums.com...tml#post146642, is also applicable to your case, especially this part:
    Quote Originally Posted by jashburn View Post
    Note that when a class is instantiated, the new object is initialised by going through the following procedure (on a high-level):
    1. invoke another constructor in the same class (if such a call exists)
    2. invoke a superclass constructor
    3. execute instance initialisers and instance variable initialisers
    4. execute the rest of the body of the constructor

    (See Object initialization in Java | JavaWorld and Chapter*12.*Execution for elaboration on the above.)
    In your case, it is at step #4 above. Consider the following, based on the code in your post but significantly simplified:
    public class ArrayStack {
     
    	ArrayStack arrayStack;
     
    	ArrayStack() {
    		arrayStack = new ArrayStack();
    	}
     
    	public static void main(String[] args) {
    		ArrayStack myStack = new ArrayStack();
    	}
     
    }
    When "ArrayStack myStack = new ArrayStack()" is called in the main method, it goes through the object initialisation procedure. On reaching step #4, it executes the constructor, ArrayStack(). The body of the constructor contains "arrayStack = new ArrayStack()," i.e., code to instantiate and initialise another ArrayStack object. This again, goes through the object initialisation procedure, execute the constructor, etc., ad infinitum.

    In short, you cannot instantiate the same object in the object's class constructor as it causes infinite recursion.

Similar Threads

  1. what is difference between call stack and stack tace?
    By me_shankara in forum Exceptions
    Replies: 6
    Last Post: October 27th, 2018, 03:23 AM
  2. Implementation Stack Using Array
    By rainbow9 in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 20th, 2011, 09:48 AM
  3. stack
    By ridg18 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 12:45 PM
  4. Stack
    By AmyH in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 7th, 2010, 04:04 PM
  5. Stack Array..
    By qaromi in forum Collections and Generics
    Replies: 2
    Last Post: December 26th, 2009, 12:54 PM