Home
Java
Array Combine Example
Updated Jun 7, 2023
Dot Net Perls
Combine arrays. Two arrays exist. We can combine them into a third array. We can do this in different ways. Some are more efficient, and others have simpler syntax.
By creating another array, we can copy all elements into a single region of storage. For-loops can be used to quickly copy these elements.
For-loop example. Let us begin with this example. We have two arrays—they can have any number of elements, but in this example they both have 6 elements.
for
Info We create a new array called "merged." This must have the total number of elements required.
Then In the first for-loop we copy the first elements into the merged array. The second for-loop must use an offset.
Tip The offset for the second for-loop ensures we do not overwrite the elements from the first array.
import java.util.Arrays; public class Program { public static void main(String[] args) { int[] array1 = { 1, 2, 3, 4, 5 }; int[] array2 = { 5, 4, 3, 2, 1 }; // Create empty array of required length. int[] merged = new int[array1.length + array2.length]; // Copy first array into new array. for (int i = 0; i < array1.length; i++) { merged[i] = array1[i]; } // Copy second array into new array. // ... Use offset to assign elements. for (int i = 0; i < array2.length; i++) { merged[array1.length + i] = array2[i]; } // Print results. System.out.println(Arrays.toString(array1)); System.out.println(Arrays.toString(array2)); System.out.println(Arrays.toString(merged)); } }
[1, 2, 3, 4, 5] [5, 4, 3, 2, 1] [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
Collections.addAll. This approach is a little simpler. We create an ArrayList of the required element type. Then we use Collections.addAll to add both arrays to it.
ArrayList
Finally We can convert our ArrayList back into a single array with the toArray method.
Note We must pass an array reference to the toArray method. This is where our new array will be placed.
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; public class Program { public static void main(String[] args) { // Two string arrays we want to combine. String[] array1 = { "cat", "bird", "fish" }; String[] array2 = { "ant", "bee" }; System.out.println(Arrays.toString(array1)); System.out.println(Arrays.toString(array2)); // Create an ArrayList. // ... Add all string arrays with addAll. ArrayList<String> list = new ArrayList<>(); Collections.addAll(list, array1); Collections.addAll(list, array2); // Display ArrayList contents. System.out.println(list.toString()); // Convert ArrayList to String array. // ... This is the final merged array. String[] merged = new String[list.size()]; list.toArray(merged); System.out.println(Arrays.toString(merged)); } }
[cat, bird, fish] [ant, bee] [cat, bird, fish, ant, bee] [cat, bird, fish, ant, bee]
Notes, Collections.addAll. With Collections.addAll, we cannot add int arrays to an ArrayList of Integers. For int arrays, using for-loops is a better solution.
Collections.addAll
ArrayList int
With for-loops or an ArrayList and Collections.addAll we can combine arrays. For more than two arrays, Collections.addAll may be simpler—fewer offsets will be needed.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jun 7, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen