Results 1 to 6 of 6
  1. #1
    BentByBogus is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default 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:
    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]);
    		}
    	}
    }
    Thats what the error is:
    Java Code:
    8.0
    Exception in thread "main" 6.0
    3.0
    java.lang.ArrayIndexOutOfBoundsException: 4
    	at labledLoops.main(labledLoops.java:7)
    Having as of now a better understanding of "for loops" I put the whole thing in such a loop:
    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);
    		}
    	}
    }
    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?
    [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:
    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]);
    		}
    	}
    }
    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?
    [/EDIT]
    Last edited by BentByBogus; 10-08-2010 at 06:14 PM. Reason: added some more code

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    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.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    With this code:
    count++<array1.length
    What is the value of count that is tested?
    What is the value of count after the test?

  4. #4
    BentByBogus is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    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.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by BentByBogus View Post
    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.
    You're overcomplicating things; if you want to iterate over all elements of array1 the idiom is simply:

    Java Code:
    for (int count= 0; count < array1.length; count++)
       // do something with array1[count] ...
    Remember: array indexes are zero based, so if you have an array with n elements the valid index values are 0, 1, 2 ... n-1

    kind regards,

    Jos

  6. #6
    BentByBogus is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You're overcomplicating things;
    Ah well since I am German I think I am supposed to. :)
    But joke aside I guess you are right.
    And thanks for explaining.

Similar Threads

  1. ArrayIndexOutOfBoundsException
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 04-07-2010, 06:50 PM
  2. ArrayIndexOutOfBoundsException
    By flaskvacuum in forum New To Java
    Replies: 6
    Last Post: 07-14-2009, 05:36 PM
  3. Problem producing XML
    By ashleyh in forum Java Servlet
    Replies: 0
    Last Post: 04-13-2008, 04:29 PM
  4. ArrayIndexOutOfBoundsException
    By daredavil82 in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 09:29 PM
  5. producing DOCs
    By javaplus in forum New To Java
    Replies: 0
    Last Post: 11-18-2007, 06:08 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
  •