没事干,写点小东西,练练手(Java <T> T):
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class GenericsTest {
public static void main(String[] args) {
String[] arr = new String[]{"1","2","3","4"};
List<String> list = get(arr);
System.out.println("list= "+list);
String result = transfer(true, "abin", "lee");
System.out.println("result= "+result);
}
public static <T> List<T> get(T... t){
List<T> list = new CopyOnWriteArrayList<T>();
for(T s:t){
list.add(s);
}
return list;
}
public static <T> T transfer(boolean flag,T first,T second){
T t = flag ? first : second;
return t;
}
}