package com.msb.test01; import java.util.ArrayList; import java.util.Iterator; /** * @author : liu * 日期:16:47:13 * 描述:IntelliJ IDEA * 版本:1.0 */ public class Test03 { //这是一个main方法:是程序的入口 public static void main(String[] args) { /* list接口中常用方法 增加:add(int index, E element) 删除:remove(int index) remove(Object o) 修改:set(int index, E element) 查看:get(int index) 判断: * */ ArrayList list = new ArrayList(); list.add(13); list.add(17); list.add(6); list.add(-1); list.add(2); list.add("abc"); System.out.println(list); list.add(3,66); System.out.println(list); list.set(3,77); System.out.println(list); list.remove(2);//在集合中存入的是Inter类型数据的时候,调用remove方法调用的是:remove(Integer) System.out.println(list); list.remove("abc");// System.out.println(list.toString()); Object o = list.get(0); System.out.println(o.toString()); System.out.println("================"); //List集合 遍历 //方式1:普通for循环 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("==============="); //方式2:增强for循环 for (Object o1:list ) { System.out.println(o1); } System.out.println("============"); //方式3迭代器 Iterator iterator = list.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
标签:遍历,List,list,System,接口,add,remove,println,out From: https://www.cnblogs.com/jeldp/p/16822153.html