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

    简介

    类似C++模板类

    code

    package cn;
    
    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 first;
    	}
    	public T getSecond() {
    		return second;
    	}
    	
    	public void setFirst(T newValue) {
    		first = newValue;
    	}
    	public void setSecond(T newValue) {
    		second = newValue;
    	}
    }
    
    
    package cn;
    
    import cn.Pair;
    
    
    class ArrayAlg1{
    	/**
    	 *  get the min max
    	 */
    	public static Pair<String> minmax(String[] a){
    		if(a == null || a.length ==0) return null;
    		String min = a[0];
    		String 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 PairTest1 {
    	public static void main(String[] args){
    		String[] words = {"Mary", "had", "a", "little", "lamb"};
    		Pair<String>mm=ArrayAlg1.minmax(words);
    		System.out.println("min = " + mm.getFirst());
    		System.out.println("max = " + mm.getSecond());
    	}
    }
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    VC6 下 libpng 库的编译与初步使用
    Windows上编译libtiff
    ActiveX控件开发
    静态库和动态库的优缺点
    KStudio window上编译uclinux
    4. API之打印函数
    window消息机制二
    消息机制、子窗口和父窗口的消息传递
    window消息机制
    dll 显示调用
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13658478.html
Copyright © 2011-2022 走看看