forked from LupusNobilis/java-org.hwo
70 lines
1.2 KiB
Java
70 lines
1.2 KiB
Java
package org.hwo;
|
|
|
|
import java.lang.reflect.Array;
|
|
|
|
public class ArrayHelper {
|
|
|
|
|
|
public static Integer[] box(int[] a){
|
|
Integer[] r = new Integer[ a.length];
|
|
for (int i=0;i<a.length;i++){
|
|
r[i] = a[i];
|
|
}
|
|
return r;
|
|
}
|
|
public static int[] unbox(Integer[] a){
|
|
int[] r = new int[ a.length];
|
|
for (int i=0;i<a.length;i++){
|
|
r[i] = a[i];
|
|
}
|
|
return r;
|
|
}
|
|
|
|
public static byte[] unbox(Byte[] bytes){
|
|
byte[] t = new byte[ bytes.length ];
|
|
|
|
for (int i=0;i<t.length;i++)
|
|
t[i] = bytes[i];
|
|
|
|
return t;
|
|
}
|
|
|
|
public static Byte[] box(byte[] bytes){
|
|
Byte[] t = new Byte[ bytes.length ];
|
|
|
|
for (int i=0;i<t.length;i++)
|
|
t[i] = bytes[i];
|
|
|
|
return t;
|
|
}
|
|
|
|
public static <T> T[] cast(Object[] src,T[] dstType){
|
|
T[] dst;
|
|
|
|
try {
|
|
dst = (T[])Array.newInstance(dstType.getClass().getComponentType(), src.length);
|
|
for (int i=0;i<src.length;i++){
|
|
System.err.println(src[i].getClass().getCanonicalName());
|
|
dst[i] = (T)src[i];
|
|
}
|
|
return dst;
|
|
} catch (SecurityException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
|
|
|
|
return null;
|
|
}
|
|
|
|
public static <T> int indexOf(T[] array,T element){
|
|
for (int i=0;i<array.length;i++){
|
|
if (array[i] == element){
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
}
|