1

Is there a simple way like one line of code to combine two arrays of double type into one array?

If there isn't, what would be the easiest way to do it?

Thanks

Armand
  • 2,611
  • 2
  • 24
  • 39

5 Answers5

4

You probably are looking for System.arrayCopy() method

PermGenError
  • 45,977
  • 8
  • 87
  • 106
2

ArrayUtils.addAll(array1, array2)

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
1

If you have liberty of using Apace commons this here is the solution.

How can I concatenate two arrays in Java?

Community
  • 1
  • 1
Jaydeep Patel
  • 2,394
  • 1
  • 21
  • 27
0

Or you could write some like this:

public static double[] unite(double[]... arrays)
{
    int length = 0;
    for(double[] array: arrays)
        length += array.length;


    double[] united = new double[length];

    int pos = 0;
    for(double[] array: arrays) {
        System.arraycopy(array, 0, united, pos, array.length);
        pos += array.length;
    }

    return united;
}


public static void main(String... args) {

    double[] d1 = {0.1, 0.2};
    double[] d2 = {0.3, 0.4, 0.5};
    double[] d3 = {0.6, 0.7, 0.8, 0.9};
    double[] d4 = {};
    double[] d5 = {1.0};

    double[] united = unite(d1, d2, d3, d4, d5);

    System.out.println(Arrays.toString(united));
}
0

Can't recommend Google Guava library for this kind of thing

imrichardcole
  • 4,633
  • 3
  • 23
  • 45