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

本文介绍了两种方法来查找 Java 数组中的最大值和最小值:一种是重载方法,另一种是使用反射和集合工具。详细步骤包括基本数据类型数组的重载方法实现和泛型的局限性,以及使用反射获取数组类型并利用 Collections.max() 和 Collections.min() 方法。通过示例代码展示了如何应用这些方法,并提供了不同数据类型的数组实例。

有两种思路:

一种是重载方式(就是有几种数字数组就写几个重载方法,因为Arrays中的toString()方法就是这么干的(黄玉昆给的解释,很好),毕竟基本数据类型没封装类好操作)。

public class GetMaxAndMin {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = new int[]{1,21,2,24,4,64,6,86,8,98,9};
		String max_min = getMax_Min(arr);
		System.out.println(max_min);    //打印结果:数组中,最大值为:98 , 最小值为 :1
		
	}
	public static String getMax_Min(int[] arr){
		int max = arr[0];
		int min = arr[0];
		for(int x = 1;x<arr.length;x++){
			if(max<arr[x])
				max = arr[x];
			if(min>arr[x])
				min = arr[x];
			}
		return "数组中,最大值为:"+max+" , 最小值为 :"+min;
	}
    public static String getMax_Min(double[] arr){
    	double max = arr[0];
    	double min = arr[0];
		for(int x = 1;x<arr.length;x++){
			if(max<arr[x])
				max = arr[x];
			if(min>arr[x])
				min = arr[x];
			}
		return "数组中,最大值为:"+max+" , 最小值为 :"+min;
	}
    public static String getMax_Min(long[] arr){
	
    	long max = arr[0];
		long min = arr[0];
		for(int x = 1;x<arr.length;x++){
			if(max<arr[x])
				max = arr[x];
			if(min>arr[x])
				min = arr[x];
			}
		return "数组中,最大值为:"+max+" , 最小值为 :"+min;
    }

}


另一种是利用反射(开始我想的也是这种,可是没有想到结合集合去解决在数组类型未知时怎么比较大小,问了下老师得到了满意的答复)
不能像这样用泛型getMax(T[]),因为T[]不接收基本类型的数组。
代码如下:

//这是关键:因为不确定数组的数据类型,无法用>比较大小,可以考虑是用集合工具了Collections的max()方法


/*
             思路:
*           1、传入一个数组引用获取其字节码文件
*           2、用Class的静态方法,isArray判断是不是数组
*           3、是数组的话通过componentType()方法获取其数组类型
*           4、对类型进行判断,是基本数据类型就一个个添加进list集合(自动装箱)
*           5、不是的话就用Object中的arrayCopy(),直接添加进list集合。
*           6、因为jvm不知道数组类型,所以不能用<、>比较符号,用Collections中的max,min方法获取集合中的最大值最小值
* */


 class  GetMaxAndMin{
	 public static void main(String[] args){
		
              int[] ins = {1,21,2,24,4,64,6,86,8,98,9};
              double[] ds = { 123.323, 123.54, 328.0 };
              float[] fs = { 123.5f, 32.4f };
              Integer[] in = { 1, 2, 3, 4, 17 };
              Double[] d = { 123.323, 123.54, 328.0 };
              System.out.println(getMax_Min(ins)); //数组中最大值为:98, 最小值为:1
              System.out.println(getMax_Min(ds));  // 数组中最大值为:328.0, 最小值为:123.323
              System.out.println(getMax_Min(fs));  //数组中最大值为:123.5, 最小值为:32.4
              System.out.println(getMax_Min(in));  //数组中最大值为:17, 最小值为:1
              System.out.println(getMax_Min(d));   // 数组中最大值为:328.0, 最小值为:123.323
	 }
    
	 @SuppressWarnings({ "unchecked", "rawtypes" })
	public static String getMax_Min(Object arr){
		 Class clazz = arr.getClass();
         List list = new ArrayList();
         int length = 0;
         if (clazz.isArray()) { // 判断是不是数组
                 length = Array.getLength(arr);
                 Class componentType = clazz.getComponentType(); // 获得数组的类型
                 if (componentType == int.class
                	    || componentType == double.class
                             || componentType == float.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);


		
	 }
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值