Combine arrays in Java

Combine arrays in Java

Methods to merge (merge, concat) arrays in Java

  public static <T> T[] concat(final T[] array1, final T... array2) {
    final Class<?> type1 = array1.getClass().getComponentType();
    final T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
    System.arraycopy(array1, 0, joinedArray, 0, array1.length);
    System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
    return joinedArray;
  }

Joining String arrays

import java.lang.reflect.Array;
import java.util.Arrays;

public class ConcatArray {

  public static void main(String[] args) {

    String[] str1 = { "a", "b", "c" };
    String[] str2 = { "d", "e", "f" };

    String[] concatStr = concat(str1, str2);
    System.out.println(Arrays.toString(concatStr));

  }

  @SuppressWarnings("unchecked")
  public static <T> T[] concat(final T[] array1, final T... array2) {
    final Class<?> type1 = array1.getClass().getComponentType();
    final T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
    System.arraycopy(array1, 0, joinedArray, 0, array1.length);
    System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
    return joinedArray;
  }
}

result

[a, b, c, d, e, f]

Joining Integer arrays

import java.lang.reflect.Array;
import java.util.Arrays;

public class ConcatArray2 {

  public static void main(String[] args) {

    Integer[] int1 = { 1, 2, 3 };
    Integer[] concatInt = concat(int1, 4, 5, 6);
    System.out.println(Arrays.toString(concatInt));

  }

  @SuppressWarnings("unchecked")
  public static <T> T[] concat(final T[] array1, final T... array2) {
    final Class<?> type1 = array1.getClass().getComponentType();
    final T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
    System.arraycopy(array1, 0, joinedArray, 0, array1.length);
    System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
    return joinedArray;
  }
}

result

[1, 2, 3, 4, 5, 6]

Joining object arrays

import java.lang.reflect.Array;
import java.util.Arrays;

public class ConcatArray3 {

  public static void main(String[] args) {

    class Person {
      public String name;
      public int age;

      public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
      }

      @Override
      public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
      }

    }

    Person[] person1 = { new Person("Tom", 10), new Person("Jenny", 15) };
    Person[] person2 = { new Person("John", 18), new Person("Jack", 22) };

    Person[] concatPerson = concat(person1, person2);
    System.out.println(Arrays.toString(concatPerson));
  }

  @SuppressWarnings("unchecked")
  public static <T> T[] concat(final T[] array1, final T... array2) {
    final Class<?> type1 = array1.getClass().getComponentType();
    final T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
    System.arraycopy(array1, 0, joinedArray, 0, array1.length);
    System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
    return joinedArray;
  }
}

result

[Person [name=Tom, age=10], Person [name=Jenny, age=15], Person [name=John, age=18], Person [name=Jack, age=22]]

Recommended Posts

Combine arrays in Java
How arrays work in Java (illustration)
Combine Java8 lists
Partization in Java
[Java] About arrays
Changes in Java 11
Rock-paper-scissors in Java
About Java arrays
Pi in Java
FizzBuzz in Java
Interpreter implementation in Java
Make Blackjack in Java
Java, about 2D arrays
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Study java arrays, lists, maps
Save Java PDF in Excel
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
I made roulette in Java.
Create hyperlinks in Java PowerPoint
Implement two-step verification in Java
Refactoring: Make Blackjack in Java
Write flyway callbacks in Java