• java泛型


    1.泛型类与泛型方法

    package Generic;
    
    import java.io.Serializable;
    import java.time.LocalDate;
    import Generic.ArrayAlg.Pair;
    
    public class ArrayDemo{
        public static void main(String[] args) {
            LocalDate[] birthdays = {
                LocalDate.of(1906, 12, 9),
                LocalDate.of(1815, 12, 10),
                LocalDate.of(1903, 12, 3),
                LocalDate.of(1910, 6, 22)
            };
            Pair<LocalDate> mm = ArrayAlg.minmax(birthdays);
            System.out.println("min = " + mm.getFirst());
            System.out.println("max = " + mm.getSecond());
        }
    }
    
    class ArrayAlg{    
        public class Pair<T>{
            private T first;
            private T second;
            
            public Pair() {
                first = null;
                second = null;
            }
            
            public Pair(T first, T second) {
                this.first = first;
                this.second = second;
            }
            
            public T getFirst() {
                return this.first;
            }
            
            public T getSecond() {
                return this.second;
            }
            
            public void setFirst(T newValue){
                first = newValue;
            }
            
            public void setSecond(T newValue) {
                second = newValue;
            }
        }
        
        public class Interval<T extends Comparable & Serializable> implements Serializable{
            private T lower;
            private T upper;
    
            public Interval(T first, T second) {
                if(first.compareTo(second)<=0) {
                    lower = first;
                    upper = second;
                }else {
                    lower = second;
                    upper = first;
                }
            }
        }
        
        public static <T extends Comparable> Pair<T> minmax(T[] array) {
            if (array == null || array.length == 0) {
                return null;
            }
            
            T min = array[0];
            T max = array[0];
            for (int i = 1; i < array.length; i++) {
                if (min.compareTo(array[i]) > 0) {
                    min = array[i];
                }
                if (max.compareTo(array[i]) < 0) {
                    max = array[i];
                }
            }
            
    //        ArrayAlg arrayAlg = new ArrayAlg();
    //        return arrayAlg.new Pair<>(min, max);
            return (new ArrayAlg()).new Pair<>(min, max);//这条语句等同于上面两行
        }
    }

    运行结果:

    min = 1815-12-10
    max = 1910-06-22
  • 相关阅读:
    layui 动态设置radio选中
    C# ling 查询 in 用法
    sql 去除小数点后面无效的0
    VUE 全局变量申明和取值
    SQL 逗号分隔将一行拆成多行
    devexpress 延长试用期 licenses.licx
    BugkuCTF-WEB4
    一招破解网页复制+网页上如何实现禁止复制粘贴
    JS的函数
    JS的数组
  • 原文地址:https://www.cnblogs.com/sunzhongyu008/p/11397215.html
Copyright © 2020-2023  润新知