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;
}