package package4;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListDemo1 {
public static void main(String[] args) {
//1,创建集合对象
List<String> list =new ArrayList<>();
//添加元素
list.add("hello");
list.add("world");
list.add("java");
//E remove(int index) 删除指定索引处的元素,返回被删除的元素
// String s = list.remove(2);
//System.out.println(s);
//void add(int index,E element) 在此集合中的指定位置插入指定的元素
list.add(3,"wan");
list.add(2,"qw");
//输出集合对象
System.out.println(list);
//遍历集合(迭代器)
Iterator<String> itr = list.iterator();
while(itr.hasNext()){
String s = itr.next();
System.out.println(s);
}
//遍历集合for循环
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
标签:java,List,list,System,add,集合,操作,out
From: https://www.cnblogs.com/knlf/p/16794021.html