Java Reference
In-Depth Information
}
}
class CopyList
{
public static void main(String[] args)
{
List<String> ls = new ArrayList<String>();
ls.add("A");
ls.add("B");
ls.add("C");
outputList(ls);
List<String> lsCopy = new ArrayList<String>();
copyList(ls, lsCopy);
outputList(lsCopy);
List<Circle> lc = new ArrayList<Circle>();
lc.add(new Circle(10.0, 20.0, 30.0));
lc.add(new Circle (5.0, 4.0, 16.0));
outputList(lc);
List<Circle> lcCopy = new ArrayList<Circle>();
copyList(lc, lcCopy);
outputList(lcCopy);
}
static <T> void copyList(List<T> src, List<T> dest)
{
for (int i = 0; i < src.size(); i++)
dest.add(src.get(i));
}
static void outputList(List<?> list)
{
for (int i = 0; i < list.size(); i++)
System.out.println(list.get(i));
System.out.println();
}
}
The generic method's type parameters are inferred from the context in which the
method was invoked. For example, the compiler determines that copyList(ls,
Search WWH ::




Custom Search