Java泛型方法
package PairTestMyCode.copy;
import java.util.*;
public class PairTest2
{
public static void main(String[] args)
{
GregorianCalendar[] birthdays =
{
new GregorianCalendar(1906, Calendar.DECEMBER, 9), // G. Hopper
new GregorianCalendar(1815, Calendar.DECEMBER, 10), // A. Lovelace
new GregorianCalendar(1903, Calendar.DECEMBER, 3), // J. von Neumann
new GregorianCalendar(1910, Calendar.JUNE, 22), // K. Zuse
};
Pair<GregorianCalendar> mm = ArrayAlg.minmax(birthdays);
System.out.println("min = " + mm.getFirst().getTime());
System.out.println("max = " + mm.getSecond().getTime());
}
}
class ArrayAlg
{
/**
Gets the minimum and maximum of an array of objects of type T.
@param a an array of objects of type T
@return a pair with the min and max value, or null if a is
null or empty
*/
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<T>(min, max);
}
}
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;
}
}
其中第30行代码是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
相关文档:
package com.yzy;
import java.util.regex.*;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Pattern p=Pattern.compile("^[a-z]+");
Matcher m=p.matcher("a233"); //true
//Matcher m=p.matcher("2233") ......
工作台窗口的Editor区域默认是显示的,而且它支持拖拽操作。在Eclipse里面,把一个文件拖到Editor区域,就会自动打开该文件的
Editor.该特性是在IWorkbenchWindowConfigurer 中设置。
在PassWord Gate中,当拖动Password Gate View中的一个Group 或者
Service到Editor区域,会在Editor显示该 ......
java socket通信
TCP客户端:
import java.net.*;
import java.io.*;
public class Client {
static Socket server;
public static void main(String[] args) throws Exception {
server = new Socket(InetAddress.getLocalHost(), 23);
BufferedReader in = new BufferedReader(new InputStreamReader ......
在实现singleton模式时,我们有以下几种方法。
1. public static final 字段加上private 的构造函数。
public class Singleton{
public static final Singleton INSTANCE = new Singleton();
& ......