AskHandle Blog
How Do You Concatenate Two Arrays in Programming?

How Do You Concatenate Two Arrays in Programming?
Concatenating two arrays is a common task in programming. It allows you to join two sequences of data into a single array, making it easier to process or analyze combined data sets. This operation is useful in many situations, such as merging search results, combining lists of items, or preparing data for further processing.
Let’s explore what concatenation means in practical terms. Given two arrays, say array1 and array2, concatenation creates a new array that contains all elements of array1 followed by all elements of array2. The original arrays remain unchanged, and a new array is returned.
Concatenation in Different Programming Languages
Different programming languages have their own ways of performing array concatenation. Here are some common examples for popular languages:
JavaScript
In JavaScript, arrays have a built-in method called .concat() that makes concatenation straightforward.
1const array1 = [1, 2, 3];
2const array2 = [4, 5, 6];
3
4const combinedArray = array1.concat(array2);
5
6console.log(combinedArray); // Output: [1, 2, 3, 4, 5, 6]This code creates two arrays and then merges them into a new array combinedArray. The original arrays array1 and array2 stay the same.
In Python, concatenating lists (which are similar to arrays) can be done using the + operator.
1array1 = [1, 2, 3]
2array2 = [4, 5, 6]
3
4combined_array = array1 + array2
5
6print(combined_array) # Output: [1, 2, 3, 4, 5, 6]Alternatively, you can use extend() if you don’t want to create a new list:
1array1 = [1, 2, 3]
2array2 = [4, 5, 6]
3
4array1.extend(array2)
5
6print(array1) # Output: [1, 2, 3, 4, 5, 6]Java
In Java, arrays are of fixed size, so to concatenate them, you typically create a new array with enough space to hold both arrays, then copy elements into it.
1int[] array1 = {1, 2, 3};
2int[] array2 = {4, 5, 6};
3
4// Create a new array with size equal to the sum of both arrays
5int[] combinedArray = new int[array1.length + array2.length];
6
7// Copy elements from array1
8System.arraycopy(array1, 0, combinedArray, 0, array1.length);
9
10// Copy elements from array2
11System.arraycopy(array2, 0, combinedArray, array1.length, array2.length);
12
13System.out.println(Arrays.toString(combinedArray)); // Output: [1, 2, 3, 4, 5, 6]C++
In C++, arrays are fixed size, so concatenation is often done using vectors, which are dynamic arrays.
1#include <vector>
2#include <iostream>
3
4int main() {
5 std::vector<int> array1 = {1, 2, 3};
6 std::vector<int> array2 = {4, 5, 6};
7
8 std::vector<int> combinedArray = array1; // start with array1
9 combinedArray.insert(combinedArray.end(), array2.begin(), array2.end()); // append array2
10
11 for(int num : combinedArray) {
12 std::cout << num << " ";
13 }
14 // Output: 1 2 3 4 5 6
15 return 0;
16}Important Notes
- Concatenation creates a new array or list; it does not modify the original arrays unless specifically designed to do so (like
extend()in Python). - When dealing with large datasets, be mindful of the memory implications, as concatenation can increase memory usage.
- Some languages and data structures have specific methods for concatenation, while others require manual copying, especially with fixed-size arrays.
Understanding how to concatenate arrays efficiently helps in writing cleaner, more effective code for a wide range of applications.