汎用学習の例

7653 ワード

package genericType;

public class Pair<T> {
	
	//  
	public Pair() {
		this.min = null;
		this.max = null;
	}
	// 
	public Pair(T _min,T _max){
		this.min = _min;
		this.max = _max;
	}
	
	// 、 
	public T getMin() {
		return min;
	}
	public void setMin(T min) {
		this.min = min;
	}
	public T getMax() {
		return max;
	}
	public void setMax(T max) {
		this.max = max;
	}

	//  
	private T min;
	private T max;
}

 
package genericType;

public class ArrayAlg {
	
	// , ,
	// Comparable 
	public static < T extends Comparable<? super T> > 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<T>(min,max);
	}
	
	// 
	public static <T> T getMiddle(T[] a){
		return a[a.length / 2];
	}
}

 
package genericType;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class PairTest {
	public static void main(String[] args) {
		
		
		String[] words = {"Mary","had","a","little","lamb"}; 
		Pair<String> mm = ArrayAlg.minmax(words);
		System.out.println("Min = " + mm.getMin());
		System.out.println("Max = " + mm.getMax());
		
		
		Integer[] names = {1,2,3};
		Integer middle = ArrayAlg.getMiddle(names);
		System.out.println("Middle = " + middle);
		
		
		GregorianCalendar[] birthdays = {
			new GregorianCalendar(1906,Calendar.DECEMBER,9),
			new GregorianCalendar(1815,Calendar.DECEMBER,10),
			new GregorianCalendar(1903,Calendar.DECEMBER,3),
			new GregorianCalendar(1910,Calendar.JUNE,22),
		};
		Pair<GregorianCalendar> nn = ArrayAlg.minmax(birthdays); 
		System.out.println("Min = " + nn.getMin().getTime());
		System.out.println("Max = " + nn.getMax().getTime());
	}
}

 
結果:
Min = Mary
Max = little
Middle = 2
Min = Sun Dec 10 00:00:00 CST 1815
Max = Wed Jun 22 00:00:00 CST 1910
 
 
例2:
package genericType2;

import java.util.Date;
import java.util.GregorianCalendar;


public class Employee {
	
	//  
	public Employee() {
		
	}	
	public Employee(String n,double s,int year,int month,int day) {
		this.name = n;
		this.salary = s;
		GregorianCalendar calendar = new GregorianCalendar(year,month-1,day);
		this.hireDay = calendar.getTime();
	}
	
	//   、  
	public String getName() {
		return name;
	}
	public double getSalary() {
		return salary;
	}
	public Date getHireDay() {
		return hireDay;
	}
	
	//  
	public void raiseSalary(double byPercent){
		double raise = this.salary * byPercent / 100 ;
		this.salary = this.salary + raise ;
	}
	
	//  
	private String name ;
	private double salary ;
	private Date hireDay ;
}

 
package genericType2;

public class Manager extends Employee {
	
	//  
	public Manager(String n,double s,int year,int month,int day) {
		super(n,s,year,month,day);
		this.bonus = 0 ;
	}
	
	//  
	public double getSalary(){
		double baseSalary = super.getSalary();
		return baseSalary + bonus ;
	}
	
	//   、  
	public double getBonus() {
		return bonus;
	}

	public void setBonus(double bonus) {
		this.bonus = bonus;
	}
	
	//  
	private double bonus ;	
}

 
package genericType2;

public class Pair<T> {
	
	//  
	public Pair() {
		this.min = null;
		this.max = null;
	}
	// 
	public Pair(T _min,T _max){
		this.min = _min;
		this.max = _max;
	}
	
	// 、 
	public T getMin() {
		return min;
	}
	public void setMin(T min) {
		this.min = min;
	}
	public T getMax() {
		return max;
	}
	public void setMax(T max) {
		this.max = max;
	}

	//  
	private T min;
	private T max;
}

 
package genericType2;

public class PairAlg {

	public static boolean hashNulls(Pair<?> p){
		return p.getMin() == null || p.getMax() == null;
	}
	
	// 
	public static void swap(Pair<?> p){
		swapHelper(p);
	}
	//  swap 
	public static <T> void swapHelper(Pair<T> p) {
		T t = p.getMin();
		p.setMin(p.getMax());
		p.setMax(t);
	}
}

 
package genericType2;

public class PairTest2 {
	public static void main(String[] args) {
		
		Manager ceo = new Manager(" ", 800000, 2008, 12, 15);
		Manager cfo = new Manager(" ", 600000, 2008, 12, 15);
		Pair<Manager> buddies = new Pair<Manager>(ceo,cfo);
		printBuddies(buddies);
		
		ceo.setBonus(1000000);
		cfo.setBonus(500000);
		Manager[] managers = {ceo , cfo};
		
		Pair<Employee> result = new Pair<Employee>();
		minMaxBonus(managers , result);
		System.out.println("Min : " + result.getMin().getName() + 
				" , Max : " + result.getMax().getName());
		
		System.out.println("-------- swap --------");
		
		maxMinBonus(managers,result);
		System.out.println("Min : " + result.getMin().getName() + 
				" , Max : " + result.getMax().getName());
	}

	//  <? super Manager>
	private static void maxMinBonus(Manager[] a, Pair<? super Manager> result) {
		
		minMaxBonus(a, result);
		// OK -- swapHelper captures wildcard type
		PairAlg.swapHelper(result);
	}

	//  <? super Manager>
	private static void minMaxBonus(Manager[] a, Pair<? super Manager> result) {
		
		if(a == null || a.length == 0)
			return;
		Manager min = a[0];
		Manager max = a[0];
		for (int i = 1; i < a.length; i++) {
			if(min.getBonus() > a[i].getBonus())
				min = a[i];
			if(max.getBonus() < a[i].getBonus())
				max = a[i];
		}
		result.setMin(min);
		result.setMax(max);
	}

	// <? extends Employee>
	private static void printBuddies(Pair<? extends Employee> p) {
		Employee min = p.getMin();
		Employee max = p.getMax();
		System.out.println(min.getName() + " and " + max.getName() + " are buddies.");
	}
}

 
結果:
張作強and陳恵儀are buddies.
Min:陳恵儀、Max:張作強
-------- swap --------
Min:張作強、Max:陳恵儀