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}
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
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.
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];
}
}
According to: https://stackoverflow.com/questions/20686499/java-how-to-convert-a-multidimensional-array-to-single-array-easily
ArrayUtils.addAll(array1,array2)
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.
double[][] multiinstead ofdouble multi[][]. Since you have an array of arrays of double (double[][]) and not an array of arrays of multi :)double multi[][], single[];declaresmultito be a two-dimensional array andsingleto be a one-dimensional array; if the line starts withdouble[][] ...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".)