Results 1 to 7 of 7
  1. #1
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default ArrayIndexOutOfBounds

    So i need a little help with arrays. Why doesnt this program work. I pretty much did what it said on the sun tutorial.

    All help appreciated:)

    Java Code:
    import java.io.*;
    
    public class Array
    {
     public static void main(String[] args)throws IOException
     {
      int[] num;
      num=new int[3];
      num[1]=4;
      num[2]=3;
      num[3]=2;
      System.out.println(num[1]);
     }
    }
    And my error is
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
        at Array.main(Array.java:11)
    Are you suggesting that Cocunuts migrate?!! -Monty Python

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

    Default

    arrays are 0 based. Thus your num int[3] array will go from:
    num[0]
    num[1]
    num[2]

    If you try to use
    num[3] you'll go beyond the array's bounds.

  3. #3
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    oh i didnt know that thanks man it works now:)
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    array 'indicies' are always zero based. at first this is confusing, because an array of three elements, and have indicies 0,1,2

    in your example, the array index out of bounds exception would be occuring on
    Java Code:
    num[3]=2;
    , because the array was defined to be three elements.

    the general rule of thumb is the inxex in the [ ] is always (-1) from the physical element.

    (because we as humans think of the first element in the array as the 1st element, not the 0th element).

    So this code to work would then be.
    Java Code:
    public class Array
    {
     public static void main(String[] args)throws IOException
     {
      int[] num;
      num=new int[3];
      num[0]=4;
      num[1]=3;
      num[2]=2;
      System.out.println(num[1]);
     }
    }

  5. #5
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    d.oh.. too wordy on my reply

  6. #6
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    thanks man thats really helpful:) i repped u for it:)
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  7. #7
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    no it was really helpful:)
    Are you suggesting that Cocunuts migrate?!! -Monty Python

Posting Permissions

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