Results 1 to 4 of 4

Thread: Error - ArrayIndexOutOfBounds

  1. #1
    Join Date
    Nov 2009
    Posts
    330

    Error - ArrayIndexOutOfBounds

    Hi
    I am trying a program with arrays and I am getting an error. So, I need a little help with arrays. I have posted my code please check it
    Code:
    public class Array
    {
     public static void main(String[] args)throws IOException
     {
      int[] test;
      test=new int[3];
      test[1]=4;
      test[2]=3;
      test[3]=2;
      System.out.println(test[1]);
     }
    }
    I am getting an error
    Code:
     ArrayIndexOutOfBoundsException.
    Any help on this or suggestion.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Error - ArrayIndexOutOfBounds

    Hi
    Code:
    test[1]=4;
      test[2]=3;
      test[3]=2;
    Arrays start with zero, that is they are zero based. So, your test int[3] array will go from
    Code:
    test[0]
    test[1]
    test[2]
    If you try to use test[3] you'll go beyond the array's bounds. Please correct that.

  3. #3
    Join Date
    Nov 2009
    Posts
    330

    Re: Error - ArrayIndexOutOfBounds

    Hello
    test[0]
    test[1]
    test[2]
    thanks buddy, I did not knew that. After correcting the problem the program works correctly now.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Error - ArrayIndexOutOfBounds

    Hi
    Arrays ar always zero based. For beginners this concept is confusing, an example is array of three elements have indicies 0,1,2. In your example the array index out of bounds exception occurs because you have written
    test[3]=2;
    You modified and correct code is here
    Code:
    public class Array
    {
     public static void main(String[] args)throws IOException
     {
      int[] test;
      test=new int[3];
      test[0]=4;
      test[1]=3;
      test[2]=2;
      System.out.println(test[1]);
     }
    }

Similar Threads

  1. Replies: 5
    Last Post: 04-05-2011, 10:50 AM
  2. Replies: 6
    Last Post: 12-11-2010, 11:37 PM
  3. Server Error: 451, Socket Error: 10053, Error Number: 0x800CCC0F
    By Eigenberg in forum Windows XP Support
    Replies: 3
    Last Post: 03-06-2008, 04:13 PM
  4. Replies: 3
    Last Post: 21-07-2005, 01:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,626,747.45203 seconds with 16 queries