Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: IndexOutOfBound

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default IndexOutOfBound

    Hi all,

    I'm getting the mentioned exception while trying to merge two int arrays.

    Here's the code..

     
    public static int[] MergeArrays(int[] ArrayA, int[] ArrayB)
      {
          int ArrayAindex = ArrayA.length;
          int[] MergedArray = new int[ArrayA.length + ArrayB.length];
          int MergedIndex = MergedArray.length;
     
          for (int x = 0; x < ArrayAindex; x++)
          {
              MergedArray[x] = ArrayA[x];
          }
     
          int y;
          int z;
     
          for (y = ArrayAindex; y < MergedIndex; ArrayAindex++)
             for (z = 0; z < ArrayB.length; z++)
             {
                MergedArray[ArrayAindex] = ArrayB[z];
     
             }
     
          for (int m = 0; m < MergedArray.length; m++)
          {
              System.out.print("MergedValues: " +  MergedArray[m]);
              System.out.println("");
              System.out.print(MergedArray[m]);
          }
          return MergedArray;
      }

    Just cant seem to find out whats wrong...


  2. #2
    Moderators helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: IndexOutOfBound

    You're algorithm is putting all the values of ArrayB into every spot in MergedArray... Use a counter that is updated in the main loop.

    // merging the second half of the array
    int counter = ArrayA.length;
    for (int i = 0; i < ArrayB.length; i++)
    {
         MergedArray[counter] = ArrayB[i];
         counter++;
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Kumarrrr (February 26th, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up Re: IndexOutOfBound

    And now why didn't I think of that.. haha

    Thanks bro.

  5. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: IndexOutOfBound

    It works fine when dealing with integers < 10;
    But I get java.lang.ArrayIndexOutOfBoundsException when the integers get huge;