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

每日一贴,今天的内容关键字为数组方法

    

    有两种思绪:

    

    一种是重载方式(就是有几种数字数组就写几个重载方法,因为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);


		
	 }
 }

    

文章结束给大家分享下程序员的一些笑话语录: 祝大家在以后的日子里. 男生象Oracle般健壮; 女生象win7般漂亮; 桃花运象IE中毒般频繁; 钱包如Gmail容量般壮大, 升职速度赶上微软打补丁 , 追女朋友像木马一样猖獗, 生活像重装电脑后一样幸福, 写程序敲代码和聊天一样有**。

--------------------------------- 原创文章 By 数组和方法 ---------------------------------

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3095570.html