Results 1 to 10 of 10
  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Array Index Out of Bound

    Hi,

    Please help me with the below one:-

    Java Code:
    class Stack
    {
    	int stck[] = new int[10];
    	int tos;
    
    /*Constructor*/
    
    Stack()
    	{
    	tos = 0;
    	}
    
    /*push routine to enter data into the LIFO(stack) structure,*/
    
    void push(int item)
    	{
    		if(tos > 9)
    		{
    			System.out.println("Stack is Full");
    		}
    		
    		else
    			stck[tos++] = item;
    
    	}
    
    /*pop routine to pull values back from stack*/
    
    int pop()
    	{
    		if(tos < 0)
    		{
    			System.out.println("Stack is already empty");
    			return 0;
    		}
    
    		else
    			return stck[tos--];
    	 }
    
    }
    
    
    class StackDemo
    {
    	public static void main(String[] args)
    	{
    		Stack s1 = new Stack();
    		Stack s2 = new Stack();
    
    	/*pushing integers onto stack*/
    		for(int i = 0; i<=9; i++)
    			s1.push(i);
    		for(int j = 10; j<=19; j++)
    			s2.push(j);
    //pop integers from stack
    	for(int i = 0; i<=9; i++)
    			System.out.println(s1.pop());
    			System.out.println();
    		for(int j = 10; j<=19; j++)
    			System.out.println(s2.pop());
    	
    	}
    	
    
    }
    Array Index Out of Bound-stack.png
    Last edited by ankiit; 01-02-2012 at 03:47 AM. Reason: with the output

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

    Default Re: Array Index Out of Bound

    If you have an error, it's usually wise to show us the actual error message, and also indicate with a comment which line causes the error.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default Re: Array Index Out of Bound

    b.t.w. there are four different stacks: ascending and descending stacks and stacks where the stack pointer refers to the first free element or to the most recently pushed element. Your push and pop methods implement different stack strategies.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Array Index Out of Bound

    Hi Fubarable,

    Apologies for the inconvenience.

    I have attached the screen shot of the error with my post.

    Thanks
    Ankit.

  5. #5
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Array Index Out of Bound

    Hi All,

    I have figured out the issue with my code, I found that instead of the following I should use:-

    Java Code:
    int pop()                      //causes error
        {
            if(tos < 0)
            {
                System.out.println("Stack is already empty");
                return 0;
            }
     
            else
                return stck[tos--];
    Java Code:
    int pop()                                            //to be used
        {
            if(tos < 0)
            {
                System.out.println("Stack is already empty");
                return 0;
            }
     
            else
                return stck[--tos];


    This solved my problem, actually I was of the view that the final value for tos is 9 which caused the problem, but since the final value is 10 , i corrected my code to work fine.

    But now I want to initialize tos to the upper limit of the stack i.e 9, looking forward for your esteemed help in the matter.

    Thanks
    Ankit

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default Re: Array Index Out of Bound

    Quote Originally Posted by ankiit View Post
    But now I want to initialize tos to the upper limit of the stack i.e 9, looking forward for your esteemed help in the matter.
    Reread my reply #3 again ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Array Index Out of Bound

    Hi Jos,

    Thanks a lot for the information.

    Please share in which of the 4 categories my push and pop routine falls.

    Request you to please provide a brief reference to 4 different strategies of implementing stacks.

    Thanks in Advance
    Best Regards,
    Ankit

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default Re: Array Index Out of Bound

    Quote Originally Posted by ankiit View Post
    Please share in which of the 4 categories my push and pop routine falls.

    Request you to please provide a brief reference to 4 different strategies of implementing stacks.
    You stack is an ascending stack (in your original example) because the elements are pushed from low to high, but your push operation assumed an empty stack (the stack pointer refers to the first available slot), while your pop operation assumes a full stack (the stack pointer refers to the last pushed element). Both operations should assume either an empty stack or a full stack, i.e. for an empty, ascending stack:

    push: stack[top++]= element;
    pop: return stack[--top];

    A full stack uses the following operations:

    push: stack[++top]= element;
    pop: return stack[top--];

    Note that I left out the stack full/empty tests.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Array Index Out of Bound

    Hi Jos,

    Thanks a lot for the information.

    I have understood and implemented both ascending and descending stacks, thanks to you :).

    But still I am unable to understand 2 of the following stacks:- "pointer points to first free element or to the most recently pushed element"

    Seeking your help in this regard.
    Thanks
    Ankit

  10. #10
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Array Index Out of Bound

    Hi Jos,

    For implementing the descending stack i have used the following code:-

    Java Code:
    class Stack
    {
        int stck[] = new int[10];
        int tos;
     
    /*Constructor*/
     
    Stack()
        {
        tos = 9;
        }
     
    /*push routine to enter data into the LIFO(stack) structure,*/
     
    void push(int item)
        {
            if(tos < 0)
            {
                System.out.println("Stack is Full");
            }
             
            else
                stck[tos--] = item;
     
        }
     
    /*pop routine to pull values back from stack*/
     
    int pop()
        {
            if(tos > 9)
            {
                System.out.println("Stack is already empty");
                return 0;
            }
     
            else
                return stck[++tos];
         }
     
    }
     
     
    class StackImpl
    {
        public static void main(String[] args)
        {
            Stack s1 = new Stack();
            Stack s2 = new Stack();
     
        /*pushing integers onto stack*/
            for(int i = 0; i<=9; i++)
                s1.push(i);
            for(int j = 10; j<=19; j++)
                s2.push(j);
    //pop integers from stack
        for(int k = 0; k<=9; k++)
                System.out.println(s1.pop());
                System.out.println("empty line");
    
            for(int l = 10; l<=19; l++)
                System.out.println(s2.pop());
         
        }
         
    }
    I think it just gave me the correct output but it ruined the stack concept.

    Please also find the output

    Array Index Out of Bound-stackimpl.png

    Please give your valuable suggestions.

    Thanks in advance

    Best Regards,
    Ankit
    Last edited by ankiit; 01-04-2012 at 06:07 AM.

Similar Threads

  1. Index out of bound exception
    By sh4rif in forum New To Java
    Replies: 7
    Last Post: 12-07-2011, 12:32 PM
  2. Replies: 5
    Last Post: 11-22-2011, 12:42 PM
  3. Array index out of bound exception error
    By rahulkrishnanr in forum Threads and Synchronization
    Replies: 7
    Last Post: 10-12-2010, 05:57 PM
  4. Array index Out of bound Exception
    By nitin_daviet88 in forum New To Java
    Replies: 9
    Last Post: 07-28-2010, 05:32 AM
  5. Array Index Out of bound exception
    By abhijit in forum Networking
    Replies: 7
    Last Post: 09-25-2009, 07:25 PM

Posting Permissions

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