0

is it possible to copy the data from multi[][] to single[]?!?

double multi[][] = { {1.0, 2.0}, {2.11, 204.00, 11.00, 34.00},{66.5,43.3,189.6}};

to

double single [] = {1.0, 2.0, 2.11, 204.00, 11.0, 66.5,43.3,189.6}
3
  • What did you try? Poste some code how you would to it. Commented Nov 4, 2014 at 16:35
  • FYI: in Java its common to write double[][] multi instead of double multi[][]. Since you have an array of arrays of double (double[][]) and not an array of arrays of multi :) Commented Nov 4, 2014 at 16:42
  • @GameDroids - Actually, the two forms are identical in effect when only one variable is being declared. The difference comes when more than one variable is declared on a line: double multi[][], single[]; declares multi to be a two-dimensional array and single to be a one-dimensional array; if the line starts with double[][] ... then everything is a two-dimensional array (or an array of two-dimensional arrays). You could, however, write: double[] multi[], single; to get the desired declarations. (I don't understand the part of your comment about "array of arrays of multi".) Commented Nov 4, 2014 at 16:49

4 Answers 4

4

With Java 8 you can write:

double[] single = Arrays.stream(multi) //Creates a Stream<double[]>
            .flatMapToDouble(Arrays::stream) //merges the arrays into a DoubleStream
            .toArray(); //collects everything into a double[] array
Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution. Just out of curiosity (since I'm not using Java 8 yet), would this properly handle an element of multi being null?
@TedHopp No that would throw a NPE - but you could add a .filter(Objects.nonNull) before flatMapToDouble to skip null entries.
2

It's entirely possible. Per @assylias's answer, Java 8 has a very nice solution. If you're not using Java 8, you'll have to do some work by hand. Since the multi elements are different length, The most efficient thing to do would be to use two passes: the first to count how many elements you need in the result and the second to actually copy the elements once you've allocated the array:

int n = 0;
for (int[] elt : multi) {
    n += elt.length;
}
double[] single = new double[n];
n = 0;
for (int[] elt : multi) {
    System.arraycopy(elt, 0, single, n, elt.length);
    n += elt.length;
}

If it is at all possible that an element of multi is null, you'd want to add an appropriate check inside each of the loops.

Comments

1

if you want a logical way..first you have to find the length of multy array content .and then make single [] with that length and add values

double multy[][] = { {1.0, 2.0}, {2.11, 204.00, 11.00, 34.00},{66.5,43.3,189.6}};
int y=0;
for(int x=0;x<multy.length,x++){
    for(int i=0;i<multy[x].length,i++){
       y++;
    }
}
double single [] =new double[y];
y=0;

for(int x=0;x<multy.length,x++){
    for(int i=0;i<multy[x].length,i++){
       y++;
       single[y]==multy[x][i];
    }
}

Comments

0

According to: https://stackoverflow.com/questions/20686499/java-how-to-convert-a-multidimensional-array-to-single-array-easily

ArrayUtils.addAll(array1,array2)

1 Comment

This is an incorrect (or at least incomplete) solution. ArrayUtils.addAll will return a new array that is the concatenation of array1 and array2. OP wants to concatenate all elements of multi. One can build on addAll to concatenate elements one at a time, but that is wildly inefficient. Furthermore, ArrayUtils is not part of the standard API; it require using the third-party Apache Commons library.

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.