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}
GameDroids
  • 5,584
  • 6
  • 40
  • 59
shamy
  • 35
  • 1
  • 2
  • What did you try? Poste some code how you would to it. – Icewind Nov 04 '14 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 :) – GameDroids Nov 04 '14 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".) – Ted Hopp Nov 04 '14 at 16:49

4 Answers4

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
assylias
  • 321,522
  • 82
  • 660
  • 783
  • Nice solution. Just out of curiosity (since I'm not using Java 8 yet), would this properly handle an element of `multi` being `null`? – Ted Hopp Nov 04 '14 at 16:55
  • @TedHopp No that would throw a NPE - but you could add a `.filter(Objects.nonNull)` before `flatMapToDouble` to skip null entries. – assylias Nov 04 '14 at 17:30
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.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
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];
    }
}
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
0

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

ArrayUtils.addAll(array1,array2)

Community
  • 1
  • 1
sukotto1
  • 190
  • 9
  • 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. – Ted Hopp Nov 04 '14 at 16:45