Java ProgramsArraysConcatenate Arrays

Concatenate Arrays in Java

beginner·  Arrays  ·  Array Manipulation

Problem

Concatenating two arrays means placing every element of the first array followed by every element of the second, into one combined array.

Given two arrays of integers, combine them into a single array holding every element from both, in order.

Input
[2, 4, 6], [7, 9]
Output
Concatenated array: [2, 4, 6, 7, 9]

Java Program

Java
import java.util.Arrays; public class ConcatenateArrays { public static void main(String[] args) { int[] arr1 = {2, 4, 6}; int[] arr2 = {7, 9}; int[] result = new int[arr1.length + arr2.length]; for (int i = 0; i < arr1.length; i++) { result[i] = arr1[i]; } for (int i = 0; i < arr2.length; i++) { result[arr1.length + i] = arr2[i]; // offset past arr1's elements } System.out.println("Concatenated array: " + Arrays.toString(result)); } }

Output

Concatenated array: [2, 4, 6, 7, 9]

Core Logic

Allocating a new array sized to hold both inputs, then copying each one into its own section, joins them end to end.

How It Works
  1. 1int[] result = new int[arr1.length + arr2.length] allocates an array large enough for every element from both inputs.
  2. 2The first loop copies each element of arr1 into result, starting at index 0.
  3. 3The second loop copies each element of arr2 into result, starting right after where arr1's elements ended — at index arr1.length + i.
  4. 4The result holds every element from arr1 first, followed by every element from arr2.
For [2, 4, 6] and [7, 9], the result array is sized 5, with 2, 4, 6 copied to indices 0-2 and 7, 9 copied to indices 3-4.
💡

Key Point: The offset arr1.length + i is what prevents the second array's elements from overwriting the first — without it, both copies would start at index 0.

Complexity
Time Complexity: O(n + m)Space Complexity: O(n + m)

Why: Every element from both arrays is visited once, and the result array holds all of them combined.

Key Concepts

new array allocationfor loopindex offset

Approach 2: Java 8

Java
import java.util.Arrays; import java.util.stream.IntStream; public class ConcatenateArraysStream { public static void main(String[] args) { int[] arr1 = {2, 4, 6}; int[] arr2 = {7, 9}; // Joins both streams end to end, then collects the result into an array int[] result = IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)).toArray(); System.out.println("Concatenated array: " + Arrays.toString(result)); } }

Output

Concatenated array: [2, 4, 6, 7, 9]

Core Logic

IntStream.concat() joins two streams end to end directly, without manually tracking index offsets.

How It Works
  1. 1Arrays.stream(arr1) and Arrays.stream(arr2) each convert their int[] into an IntStream.
  2. 2IntStream.concat(...) joins the two streams together, with every element from the first stream before every element from the second.
  3. 3.toArray() collects the combined stream into a brand-new int[].
IntStream.concat(Arrays.stream({2, 4, 6}), Arrays.stream({7, 9})) produces a stream of 2, 4, 6, 7, 9, collected into the result array.
💡

Key Point: concat() handles the index bookkeeping internally — there's no equivalent of the manual version's arr1.length + i offset to get wrong.

Complexity
Time Complexity: O(n + m)Space Complexity: O(n + m)

Why: The concatenated stream still visits every element from both arrays once, and toArray() builds a result array holding all of them.

Key Concepts

StreamIntStream.concat()toArray()

Related Programs