---
title: Concatenate two arrays
url: https://docs.vultr.com/java/examples/concatenate-two-arrays
publish_date: 2024-12-16T16:20:00.078200Z
last_updated: 2024-12-16T16:20:00.078200Z
header_image: https://docs.vultr.com/public/doc-assets/collection-items/1449/37e4e041-4a82-469b-a971-7128714812b8.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Java Program to Concatenate Two Arrays

## Introduction

Java, known for its robust handling of objects and classes, offers various ways to manage and manipulate arrays. One common task in Java programming is concatenating two arrays - combining them into a single array. This operation is useful in numerous situations, from data processing to everyday programming tasks where you need to merge data stored in separate arrays.

In this article, you will learn how to concatenate two arrays in Java using different methods. Explore straightforward loops and advanced methods involving system functions to achieve your goal. By the end of this guide, you will be able to implement these techniques in your own Java projects, reinforcing your understanding of array handling and manipulation.

## Basic Concatenation Using Loops

Concatenating two arrays can be approached in the simplest form by using loops to manually iterate through each array and add their elements to a new, larger array. This method gives you a high level of control over the process and is easy to understand for beginners.

### Example: Concatenating Integer Arrays
1. Define two arrays of integers that you wish to concatenate.
1. Create a new array that has the combined length of both arrays.
1. Use a loop to copy elements from the first array into the new array.
1. Use another loop to add elements from the second array to the new array, starting from where the first left off.

    ```java
    int[] array1 = {1, 2, 3};
    int[] array2 = {4, 5, 6};
    int[] concatenatedArray = new int[array1.length + array2.length];

    int i = 0;
    for (; i < array1.length; i++) {
        concatenatedArray[i] = array1[i];
    }
    for (int j = 0; j < array2.length; i++, j++) {
        concatenatedArray[i] = array2[j];
    }
    ```

    This code first iterates over `array1`, assigning its elements to the new `concatenatedArray`. After finishing with `array1`, it continues with `array2`, starting from where it left off. This ensures that all elements from both arrays are included in `concatenatedArray`.

## Advanced Concatenation Using System Functions

For a more efficient approach when performance or readability is a concern, you can use Java's built-in system functions. The `System.arraycopy()` method is particularly useful for concatenating arrays because it is designed to efficiently copy arrays.

### Example: Concatenating String Arrays
1. Define two string arrays.
1. Initialize a new string array that can hold all elements from both arrays.
1. Use `System.arraycopy()` to copy the first array into the new array.
1. Use `System.arraycopy()` again to copy the second array into the new array, starting right after the last element of the first array.

    ```java
    String[] array1 = {"Hello", "World"};
    String[] array2 = {"Concatenation", "Example"};
    String[] concatenatedArray = new String[array1.length + array2.length];

    System.arraycopy(array1, 0, concatenatedArray, 0, array1.length);
    System.arraycopy(array2, 0, concatenatedArray, array1.length, array2.length);
    ```

    Here, `System.arraycopy()` first copies all elements from `array1` into `concatenatedArray`. Then it continues to copy elements from `array2`, starting at the index immediately following the last index filled by `array1`. This method is faster than a loop, especially for larger arrays, and keeps the code concise.

## Conclusion

Concatenating arrays in Java is a fundamental skill that enhances flexibility and efficiency in data handling tasks. Whether using basic loops for greater control or advanced system functions for better performance and readability, mastering array concatenation allows you to seamlessly integrate separate data sets in your applications. Implement these methods in your array manipulation toolkit to ensure your applications are robust and efficient.