Java泛型的简单实例
package com.chase.test;
import java.util.arraylist;
import java.util.hashtable;
import java.util.list;
public class testt {
public static <t> void main(string[] args) {
testt classt = new testt();
list<t> find = classt.find(0, 10);
if (find != null && find.size()>0) {
for (t integer : find) {
system.out.println(integer);
}
}
// showlist();
}
public static <t> void showlist() {
testt classt = new testt();
list<t> find = classt.find(0, 10);
for (t t : find) {
system.out.println(t);
}
}
public <t> list<t> find(int begin, int end) {
list<t> list = new arraylist<t>();
list.add((t)new integer(222));
list.add((t)"111");
list.add((t)"昨天是重阳节!");
return list;
}
}
class testgen0<k,v>{
public hashtable<k,v> h=new hashtable<k,v>();
public void put(k k, v v) {
h.put(k,v);
}
public v get(k k) {
return h.get(k);
}
public static void main(string args[]){
testgen0<string,string> t=new testgen0<string,string>();
t.put("key", "value");
string s=t.get("key");
system.out.println(s);
}
}
testt 输出:
222
111
昨天是重阳节!
testgen0输出:
value