Results 1 to 10 of 10
Thread: Array Index Out of Bound
- 01-01-2012, 04:59 PM #1
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
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()); } }
Last edited by ankiit; 01-02-2012 at 03:47 AM. Reason: with the output
-
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.
- 01-01-2012, 05:59 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-02-2012, 03:50 AM #4
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
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.
- 01-02-2012, 04:48 AM #5
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
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
- 01-02-2012, 07:19 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 01-02-2012, 08:54 AM #7
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
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
- 01-02-2012, 04:02 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: Array Index Out of Bound
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-04-2012, 05:59 AM #9
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
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
- 01-04-2012, 06:03 AM #10
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
Re: Array Index Out of Bound
Hi Jos,
For implementing the descending stack i have used the following code:-
I think it just gave me the correct output but it ruined the stack concept.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()); } }
Please also find the output

Please give your valuable suggestions.
Thanks in advance
Best Regards,
AnkitLast edited by ankiit; 01-04-2012 at 06:07 AM.
Similar Threads
-
Index out of bound exception
By sh4rif in forum New To JavaReplies: 7Last Post: 12-07-2011, 12:32 PM -
This is a pgm for text detection.This shows array Index Out of Bound Exception..
By gopika in forum Advanced JavaReplies: 5Last Post: 11-22-2011, 12:42 PM -
Array index out of bound exception error
By rahulkrishnanr in forum Threads and SynchronizationReplies: 7Last Post: 10-12-2010, 05:57 PM -
Array index Out of bound Exception
By nitin_daviet88 in forum New To JavaReplies: 9Last Post: 07-28-2010, 05:32 AM -
Array Index Out of bound exception
By abhijit in forum NetworkingReplies: 7Last Post: 09-25-2009, 07:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks