public static Object from2DArrayToArray ( final Object array2D ) { // final Profiler profilerPeriod = new Profiler("from2DArrayToArray"); // profilerPeriod.start("from2DArrayToArray"); Object array = null ; if ( array2D . getClass ( ) . isArray ( ) ) { final int lengthY = Array . getLength ( array2D ) ; if ( Array . getLength ( array2D ) > 0 ) { final Object firstLine = Array . get ( array2D , 0 ) ; int lengthLineX ; if ( firstLine . getClass ( ) . isArray ( ) ) { lengthLineX = Array . getLength ( firstLine ) ; final Class < ? > compType = firstLine . getClass ( ) . getComponentType ( ) ; array = Array . newInstance ( compType , lengthY * lengthLineX ) ; if ( lengthLineX > 0 && lengthY > 0 ) { for ( int y = 0 ; y < lengthY ; y ++ ) { final Object line = Array . get ( array2D , y ) ; System . arraycopy ( line , 0 , array , lengthLineX * y , lengthLineX ) ; } } } else { // is not a 2D array array = Array . newInstance ( array2D . getClass ( ) . getComponentType ( ) , Array . getLength ( array2D ) ) ; System . arraycopy ( array2D , 0 , array , 0 , Array . getLength ( array2D ) ) ; } } else { // empty array if ( array2D . getClass ( ) . getComponentType ( ) . isArray ( ) ) { array = Array . newInstance ( array2D . getClass ( ) . getComponentType ( ) . getComponentType ( ) , Array . getLength ( array2D ) ) ; } else { array = Array . newInstance ( array2D . getClass ( ) . getComponentType ( ) , Array . getLength ( array2D ) ) ; } } } else { // is not an array array = array2D ; } // profilerPeriod.stop().print(); return array ; }
Convert a 2D array to a 1D array