/*
泛型:java1.5版本以后出现新特性,用于解决安全问题。是一个安全机制.
好处:
1,将运行时期出现的问题classCastException。转移到了编译时期、
方便于程序员解决问题,让运行事情问题减少。安全
2,避免了强制转换的麻烦!
泛型格式:通过<>来定义要操作的引用数据类型、
在使用java提供的对象时什么时候使用泛型呢?
通常在集合框架很常见
只要见到<>就要定义泛型
其实<>就是用来接收类型的。
当使用集合时,将集合中药存储的数据类型作为参数传递到<>中即可
*/
import java.util.*;
class Generic1
{
public static void main(String[] args)
{
ArrayList a1 = new ArrayList();
a1.add("abc412");
a1.add("abc234");
a1.add("abc24");
a1.add("abc334");
Iterator it =a1.iterator();
//Iterator it = (String)a1.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class LenComparator implements Comparator { public int compare(String o1, String o2) { int num = new Integer(o1.length()).compareTo(new Integer(o2.length())); if(num == 0) return o1.compareTo(o2); } }