/**
* 1,数组的类型不定
* 2,个数不定
**/
import java.lang.reflect.Array;
public class ArrayUtils{
public static void main(String[] args) {
int[] myarray = {1, 2, 3};
int[] newarray = invertArray(myarray);
for (int i = 0; i < newarray.length; i++) {
System.out.println(newarray[i]);
}
}
/**
* 反转数组
*
*/
public static <T> T invertArray(T array) {
int len = Array.getLength(array);
Class<?> classz = array.getClass().getComponentType();
Object dest = Array.newInstance(classz, len);
System.arraycopy(array, 0, dest, 0, len);
Object temp;
for (int i = 0; i < (len / 2); i++) {
temp = Array.get(dest, i);
Array.set(dest, i, Array.get(dest, len - i - 1));
Array.set(dest, len - i - 1, temp);
}
return (T)dest;
}
}
* 1,数组的类型不定
* 2,个数不定
**/
import java.lang.reflect.Array;
public class ArrayUtils{
public static void main(String[] args) {
int[] myarray = {1, 2, 3};
int[] newarray = invertArray(myarray);
for (int i = 0; i < newarray.length; i++) {
System.out.println(newarray[i]);
}
}
/**
* 反转数组
*
*/
public static <T> T invertArray(T array) {
int len = Array.getLength(array);
Class<?> classz = array.getClass().getComponentType();
Object dest = Array.newInstance(classz, len);
System.arraycopy(array, 0, dest, 0, len);
Object temp;
for (int i = 0; i < (len / 2); i++) {
temp = Array.get(dest, i);
Array.set(dest, i, Array.get(dest, len - i - 1));
Array.set(dest, len - i - 1, temp);
}
return (T)dest;
}
}