public class ListTest1 {
@Test
public void test1(){
ArrayList list = new ArrayList();
list.add(123);
list.add(456);
list.add("AA");
list.add(456);
list.add(new Person("TOM",12));
System.out.println(list);
// void add(int index,Object ele):在index位置插入ele元素
list.add(2,"ZZ");
System.out.println(list);
// boolean addAll(int index,Collection eles):从index位置开始将eles中所有元素添加进来
List list1 = Arrays.asList(1, 2, 3);
list.addAll(list1);
System.out.println(list.size());//9
System.out.println(list);
list.remove(2);
System.out.println(list);
list.set(1,"XX");
System.out.println(list);
list.add(1,"time");
System.out.println(list);
System.out.println("*******************");
List list2 = list.subList(2, 4);//左闭右开
System.out.println(list2);
System.out.println("*******************");
Iterator iterator = list.iterator();
// 方式1:
while(iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("*******************");
// 方式2:
for(Object obj:list){
System.out.println(obj);
}
System.out.println("*******************");
//方式3:
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
list常用方法-增删改查插长度遍历等
最新推荐文章于 2024-04-18 08:00:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)