Array Combine Example
This page was last reviewed on 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.
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.
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.
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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 7, 2023 (edit).
© 2007-2024 Sam Allen.