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

5 Answers 5

4

You probably are looking for System.arrayCopy() method

Sign up to request clarification or add additional context in comments.

Comments

2

ArrayUtils.addAll(array1, array2)

Comments

1

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

How can I concatenate two arrays in Java?

Comments

0

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

Comments

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));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.