public static Object slice ( Object arr , int from , int to , int step ) { Class < ? > toType = Types . arrayElementType ( arr . getClass ( ) ) ; int length = Array . getLength ( arr ) ; if ( to > length ) to = length ; if ( 0 > from ) from = 0 ; length = to - from ; if ( 0 >= length ) return Array . newInstance ( toType , 0 ) ; if ( step == 0 || step == 1 ) { Object toArray = Array . newInstance ( toType , length ) ; System . arraycopy ( arr , from , toArray , 0 , length ) ; return toArray ; } Object [ ] tmp = new Object [ ( int ) Math . ceil ( ( 0.0 + length ) / Math . abs ( step ) ) ] ; for ( int i = 0 , j = 0 ; i < length ; i ++ ) if ( i % step == 0 ) tmp [ j ++ ] = Array . get ( arr , step < 0 ? length - 1 - i : i + from ) ; Object toArray = Array . newInstance ( toType , tmp . length ) ; copy ( toType , toArray , ( Object ) tmp ) ; return toArray ; }
Slice the supplied array for range and step .