import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class ListTest {
/**
* @param args
*/
public static void main(String[] args) {
List list=new ArrayList();
list.add("hello");
list.add("he");
list.add(3);
list.add("xixi");
list.add(2,"heihei");
System.out.println("方法1");
System.out.println(list);
System.out.println("方法2");
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
System.out.println("方法3");
Object[] o=list.toArray();
for(int i=0;i<o.length;i++)
{
System.out.println(o[i]);
}
System.out.println("方法4");
Iterator it=list.iterator();
//Iterator 迭代器
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println("方法5");
//for-each
// :后面可以是集合或对象
for(Object obj:list)
{
System.out.println(obj);
}
}
}
|