Io ne ho fatta una generica:
codice:
@SuppressWarnings({"unchecked"})
public static<E> java.util.List<E[]> split(E[] array, E sep) {
ArrayList<E[]> ret = new ArrayList<E[]>();
int numElements = 0;
int start = 0;
for(int pos=0; pos<array.length; pos++) {
if ( array[pos].equals(sep) ) {
E[] tmpArray = (E[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), numElements);
System.arraycopy(array, start, tmpArray, 0, numElements);
ret.add( tmpArray );
start = pos+1;
numElements = 0;
} else {
numElements++;
}
}
if ( numElements > 0 ) {
E[] tmpArray = (E[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), numElements);
System.arraycopy(array, start, tmpArray, 0, numElements);
ret.add( tmpArray );
}
return ret;
}
Chiaramente funziona solo con array di oggetti, non con array di tipi primitivi.
La funzione restituisce un List di array... così si va meglio ad usarli in un contesto di Collection Framework.
Ciao.