An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

Example:

public class ConcatenateArrays
{
 public static void main(String[] args)
 {
 int[] t1 = { 3, 2, -1, 4 };
 int[] t2 = { 9, -7 };
 int[] t = concatenateArrays(t1, t2);
 printArray(t);

 int[] v1 = { -4, -4, 0, -4, 2 };
 int[] v2 = { -4, -4, 0, -4, 2, 9, 15 };
 int[] v = concatenateArrays(v1, v2);
 printArray(v);

 // If you coded this correctly, your output should be: 
 // 3 2 -1 4 9 -7 
 // -4 -4 0 -4 2 -4 -4 0 -4 2 9 15
 }

 public static int[] concatenateArrays(int[] a, int[] b)
 {
 int arrLen = a.length + b.length;
 //adds up the number of elements in the first and the second array into one which can hold them both
 int doorStop = 0; 
 int[] storeArray = new int[arrLen];//creates new array of the same size as our two arrays
 
 for (int i = 0; i < a.length; i++)
 {
 storeArray[i] = a[i];
 doorStop = i+1;
 //doorStop tells us where the first array finished off writing so we can start there with the next array
 }
 for (int i = 0; i < b.length; i++)
 {
 storeArray[i + doorStop] = b[i];
 }
 
 return storeArray;
 }

 public static void printArray(int[] array)
 {
 for (int i = 0; i < array.length; i++)
 System.out.print(array[i] + " ");
 System.out.println();
 }
}

 

The above program takes two different arrays and smushes them into one array. Why not just tell the compiler to concatenate the two arrays like we would a String?

 

Because once an array’s size is defined, that’s it. You can’t shrink it or make it bigger. Think of it like having a box which can hold, lets say, 4 items. Its not like you can merge the box with another box, because both boxes have already been created.

 

What we can do is take both boxes, measure them, and create a new box. The box will be able to fit all the elements from each box into one.