0

I am trying to write a method that receives two arrays and concatenates them. Right now I am getting the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2." I do not understand why this is happening. Can someone explain why I am getting this error?

public static int [ ] concat (int [ ] nums1, int [ ] nums2)

    {
        int length = nums1.length+nums2.length;
        int nums3 [] = new int [length];
        for (int i=0; i<nums1.length; i++)
        {
            int value = nums1 [i];
            nums3 [i]=value;
        }
        for (int i=0; i<(nums1.length+nums2.length); i++)
        {
            int value=nums2 [i]; //It says I have an error on this line but I do not understand why.
         length = nums1.length+1;
            nums3 [length]= value;
        }
        return nums3;

    }
Bill
  • 25
  • 2
  • 9
  • 1
    Please formulate a question. Are you asking why you're getting an exception? Have you debugged the code? Have you walked through it with pen and paper? – Sotirios Delimanolis Oct 31 '14 at 00:58
  • It would be really helpful if you looked at the exception you get. It has a line number with it. Look at the line in your code and figure out what's wrong. (I think I just spotted the error by inspection, but looking at the line number in the exception would help confirm my guess). – markspace Oct 31 '14 at 01:00

3 Answers3

1

Your second loop is spanning the concatenated length, when you want it to only span the length of nums2.

Try this:

    for (int i=nums1.length; i<nums2.length; i++)
    {
        int value=nums2 [i - num1.length];
        nums3 [i]= value;
    }
CharlieS
  • 1,432
  • 1
  • 9
  • 10
  • Your code does not include any printing, or assigning to num2 array. Post the rest of the code so we can see why this is happening – CharlieS Oct 31 '14 at 01:11
1

Use the Apache Commons Lang library.

String[] concat = ArrayUtils.addAll(nums1, nums2);

API

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
0

This is a working example:

import java.util.Arrays;

public class test{
        public static int [] concat (int [] nums1, int [] nums2)
        {
                int length = nums1.length+nums2.length;
                int nums3 [] = new int [length];
                for (int i=0; i<nums1.length; i++)
                {
                        nums3[i] = nums1[i];
                }
                // You can start adding to nums3 where you had finished adding
                // in the previous loop. 
                for (int i=nums1.length; i< nums3.length; i++)
                {
                        // (i - nums1.length) will give you zero initially and 
                        // ends with nums2.length by the time this loop finishes
                        nums3[i]= nums2[i - nums1.length];
                }
                return nums3;

        }

        public static void main(String[] args) {

                int[] temp = {1,2,3,4};
                int[] temp2 = {1,2,3,4};
                System.out.println(Arrays.toString(concat(temp,temp2)));


        }

}

You can also use the method addAll in ArrayUtils to add them together as noted in this thread Arrayutils thread , but if you want to write out the method explicitly then this should work.

Community
  • 1
  • 1
kolonel
  • 1,412
  • 2
  • 16
  • 33