分类: Java
2008-10-10 11:59:09
List stringList=new LinkedList();
stringList.add("firstString");
stringList.add("secondString");
String str=(String)stringList.iterator().next(); |
List
stringList.add("firstString");
stringList.add("secondString");
String str=stringList.iterator().next(); |
public class MyGenericClass
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
} |
public class MyGenericClass
public static T value;//错误的定义
}
|
List
List |
List
strList.add("a string"); |
void printCollection(Collection c) {
Iterator i = c.iterator();
for (k = 0; k < c.size(); k++)
{
System.out.println(i.next());
}
} |
void printCollection(Collection
{
for (Object e : c)
{
System.out.println(e);
}
} |
void printCollection(Collection> c)
{
for (Object e : c)
{
System.out.println(e);
}
} |
private void clearAllMaps(Collection
{
for(Map m:c)
{
m.clear();
}
} |
private void clearAllMaps(Collection extends Map> c)
{
for(Map m:c)
{
m.clear();
}
} |
List
HashMap
m.put("key","value");
h.add(m);
clearAllMaps(h); |
public void addRectangle(List extends Shape> shapes)
{
shapes.add(0, new Rectangle()); // 错误用法!
} |
private void addString(Collection super String> c)
{
c.add("a String");
} |
private void fromArrayToCollection(Object[] a, Collection> c)
{
for (Object o : a)
{
c.add(o); // 错误的代码
}
} |
private
{
for (T o : a)
{
c.add(o); //这样是正确的
}
} |
class Collections {
static void swap(List> list, int i, int j) {...}
static
} |
static |