zoukankan      html  css  js  c++  java
  • java 泛型类2

    简介

    泛型类 确保某个类一定包含某个接口函数

    code

    package cn;
    import cn.Pair;
    import java.time.*;
    
    class ArrayAlg2{
    	public static <T extends Comparable> Pair<T> minmax(T[] a){
    		if(a == null || a.length == 0){
    			return null;
    		}
    		T min = a[0];
    		T max = a[0];
    		for(int i = 1; i < a.length; i++){
    			if(min.compareTo(a[i]) > 0) min = a[i];
    			if(max.compareTo(a[i]) < 0) max = a[i];
    		}
    		return new Pair<>(min, max);
    	}
    }
    
    public class PairTest2 {
    	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 = ArrayAlg2.minmax(birthdays);
    		System.out.println("min = " + mm.getFirst());
    		System.out.println("max = " + mm.getSecond());
    	}
    }
    
    

    Q&A

    1. 其中 T extends Comparable
      表示T类型一定要有Comparable 接口。
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    SQL_50题
    Java多线程之冰山一角
    概览
    Selector
    集群配置
    redis config
    分布式、集群
    redis相关技术总结
    redis scan扫描
    redis 单线程 多路io复用
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13658567.html
Copyright © 2011-2022 走看看