关于任意数字类型的数组求最大值解决办法 (三)

2014-11-24 11:01:08 · 作者: · 浏览: 2
oat.class
|| componentType == long.class) {
for (int i = 0; i < length; i++) {
// 如果是基本类型的数字数组,需要手动添加到集合
list.add(Array.get(arr, i));
}
} else if (componentType == Integer.class
|| componentType == Float.class
|| componentType == Double.class
||componentType == Long.class) {
// 如果是包装类型,用Arrays的asList()方法
Object[] newArr = (Object[]) Array.newInstance(componentType,
length);
System.arraycopy(arr, 0, newArr, 0, length);
list = Arrays.asList(newArr);
} else {
throw new RuntimeException("请出入数字数组");
}

} else {
throw new RuntimeException("请输入数组");
}
return "数组中最大值为:"+Collections.max(list)+", 最小值为:"+Collections.min(list);



}
}