Results 1 to 2 of 2
  1. #1
    Masamune1059 is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Creating and Array from an Array

    Hi, im trying to create a new array from and existing one i have but without the duplicates, then i can use this new array to populate a dropdown menu.

    My problem is this, when i run the program i come across ""Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 33"" and i dont know how to get around it.

    I've included the problem area (i think) below, and the full netbeanz file is here MEGAUPLOAD - The leading online storage and file delivery service.

    Any help would be greatly appretiated

    Java Code:
       public Arrays()
       {
          // Finds amount of different Route Codes (a)
          int a = 0;
          for (int i = 0; i < routecode.length; i++)
          {
             if (routecode[i].equals(routecode[i + 1]))
             {
                System.out.println("Entered if");
             }
             else
             {
                System.out.println("Entered else");
                a++;
                System.out.println(a);
             }
          }
    
          //Puts route codes into a new string without duplicates
          String[] menu = new String[a];
          for (int i = 0; i < routecode.length; i++)
          {
             if (routecode[i].equals(routecode[i + 1]))
             {
                System.out.println("if");
             }
             else
             {
                System.out.println("else");
                menu[i] = routecode[i];
                System.out.println(menu[i]);
             }
          }
    
       }

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

    Default Re: Creating and Array from an Array

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 33
    Remember that the indexes for arrays go from 0 to the length-1. An index >= the length of the array is Out Of Bounds.

    Look at the statement with the error and see what index used in that statement is past the end of the array.
    If you can not tell, add printlns to print out ALL of the indexes values used on the line so you can see which one is out of bounds.
    The index values are what is inside of the []s

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 12:40 PM
  2. Need help creating a String/Array?
    By hla40 in forum New To Java
    Replies: 2
    Last Post: 06-09-2011, 05:03 AM
  3. Creating Array of Objects
    By chathurajeewaka in forum New To Java
    Replies: 4
    Last Post: 12-03-2009, 03:23 PM
  4. Creating an array of objects
    By geowizard in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 01:25 AM
  5. creating array at runtime
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 11-08-2007, 10:06 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
  •