Results 1 to 6 of 6
- 10-08-2010, 05:56 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Why is this producing ArrayIndexOutOfBoundsException?
Hi!
Since you kindly helped me yesterday I thought returning here for some more of your advice surely would be a good idea.
I cant quite make out why there is an "ArrayIndexOutOfBoundsException" when I run this:
Thats what the error is:Java Code:public class labledLoops { public static void main (String[] args){ int[] array1={10,8,6,3}; double[] array2=new double[array1.length]; int count=0; while(count++<array1.length){ if (array2[count]==6){ continue; } array2[count]=(double)array1[count]; System.out.println(array2[count]); } } }
Having as of now a better understanding of "for loops" I put the whole thing in such a loop:Java Code:8.0 Exception in thread "main" 6.0 3.0 java.lang.ArrayIndexOutOfBoundsException: 4 at labledLoops.main(labledLoops.java:7)
Interestingly enough if I dont add a break statement it seems the loop continues even above "count<=array1.length" despite the fact that it shouldn't given by that very statement right?Java Code:public class testLoops2 { public static void main(String[] args){ int array1[]={10,8,7,3}; float[] array2=new float[array1.length]; for (int count=0;count<=array1.length;count++){ if(count==array1.length){ break; } array2[count]=(float)array1[count]; System.out.println("array2 elements:"+array2[count]); System.out.println("count content:"+count); } } }
[EDIT]
Never mind on that last part. Had to change it to "count<array1.length".
And with some more close looking I solved the while loop like this:
Apparently the test inside the while loop is executed on runtime once which makes count start at 1 and not 0. So I put it to -1 and made the loop stop at count==array1.length. But why is it not doing it on itself?Java Code:public class labledLoops { public static void main (String[] args){ int[] array1={10,8,6,3}; double[] array2=new double[array1.length]; int count=-1; while(count++<array1.length){ if (count==array1.length){ break; } array2[count]=(double)array1[count]; System.out.println(array2[count]); } } }
[/EDIT]Last edited by BentByBogus; 10-08-2010 at 06:14 PM. Reason: added some more code
- 10-08-2010, 06:09 PM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Hi,
Very simply it is saying that the application is attempting to access the element at position 4 within the array which is outside the bounds of the array (Position 0 to 3).
What is happening in the line "while(count++<array1.length)" is that the condition is being validated and count is being incremeted before moving to the next line of code.
On the first pass the effect of this is the system validates count (count = 0) is less than array1.length (length = 4) which is true. It then increments count by one (count = 1) then drops through to the next line of code. count needs to be incremeted after all other operations as currently you are referencing the position after the one you are intending to manipulate.
Regards.
- 10-08-2010, 06:10 PM #3
With this code:
count++<array1.length
What is the value of count that is tested?
What is the value of count after the test?
- 10-08-2010, 06:21 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Thanks for helping me out.
I now too realized that count is incremented before the body of the while loop is being executed which makes count=0 start with 1 meaning that array1[0] is ignored and count goes as far as 4 when array1 only has four elements ending at array1[3].
Hope that gets it right.
- 10-08-2010, 06:26 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
You're overcomplicating things; if you want to iterate over all elements of array1 the idiom is simply:
Remember: array indexes are zero based, so if you have an array with n elements the valid index values are 0, 1, 2 ... n-1Java Code:for (int count= 0; count < array1.length; count++) // do something with array1[count] ...
kind regards,
Jos
- 10-08-2010, 09:32 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
ArrayIndexOutOfBoundsException
By er1c550n20 in forum New To JavaReplies: 2Last Post: 04-07-2010, 06:50 PM -
ArrayIndexOutOfBoundsException
By flaskvacuum in forum New To JavaReplies: 6Last Post: 07-14-2009, 05:36 PM -
Problem producing XML
By ashleyh in forum Java ServletReplies: 0Last Post: 04-13-2008, 04:29 PM -
ArrayIndexOutOfBoundsException
By daredavil82 in forum New To JavaReplies: 2Last Post: 12-14-2007, 09:29 PM -
producing DOCs
By javaplus in forum New To JavaReplies: 0Last Post: 11-18-2007, 06:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks