目录一.List集合的特有方法1.1 add(int index, E element)在指定位置插入元素。1.2 remove(int index) 删除指定索引的元素并返回被删除的元素。1.3 set(int index, E element) 修改指定索引的元素并返回被修改前的元素。1.4 get(int index) 获取指定索引的元素。二.List集合的遍历方式2.1 迭代器 Iterator2.2 增强 for2.3 Lambda 表达式 forEach2.4 普通 for 循环2.5 列表迭代器 ListIterator正向遍历反向遍历遍历时添加元素三.List集合五种遍历方式对比List是单列集合里非常常用的一类。特点是有序、可重复、有索引。常见实现类ArrayList、LinkedList。ListUser users new ArrayList();一.List集合的特有方法List 继承了 Collection 的所有方法如add(E e)remove(Object o)contains(Object o)clear()size()isEmpty()List集合因为自己有索引所以多了一些带索引操作的方法。方法名称说明voidadd(int index, E element)在此集合中的指定位置插入指定的元素Eremove(int index)删除指定索引处的元素返回被删除的元素Eset(int index, E element)修改指定索引处的元素返回被修改的元素Eget(int index)返回指定索引处的元素1.1 add(int index, E element)在指定位置插入元素。ListString list new ArrayList(); list.add(张三); list.add(王五); // 在索引 1 的位置插入 李四 list.add(1, 李四); System.out.println(list);//[张三, 李四, 王五]注意索引不能越界。1.2remove(int index)删除指定索引的元素并返回被删除的元素。ListString list new ArrayList(); list.add(张三); list.add(李四); String removed list.remove(1); System.out.println(被删除的是 removed);//被删除的是李四1.3set(int index, E element)修改指定索引的元素并返回被修改前的元素。String old list.set(1, 王五); System.out.println(被修改前的是 old);//被修改前的是李四1.4get(int index)获取指定索引的元素。System.out.println(list.get(0));二.List集合的遍历方式迭代器遍历继承Collection列表迭代器遍历増强for遍历继承CollectionLambda表达式遍历继承Collection普通for循环因为List集合存在索引我们准备一个集合ListString list new ArrayList(); list.add(张三); list.add(李四); list.add(王五);2.1 迭代器 Iterator适合遍历时删除元素。IteratorString it list.iterator(); while (it.hasNext()) { String name it.next(); System.out.println(name); }2.2 增强 for适合只读取元素。增强 for 里不要直接修改集合结构比如添加、删除元素。for (String name : list) { System.out.println(name); }2.3Lambda 表达式 forEachlist.forEach(name - System.out.println(name));2.4普通 for 循环使用size方法和get方法还有循环结合的方式利用索引获取到集合中的每一个元素。适合需要索引的场景。for (int i 0; i list.size(); i) { String name list.get(i); System.out.println(name); }2.5列表迭代器 ListIteratorListIterator是Iterrator的子接口。ListIterator 是List 特有的迭代器。它比普通 Iterator 更强可以从前往后遍历从后往前遍历遍历时添加元素遍历时修改元素正向遍历ListIteratorString it list.listIterator(); while (it.hasNext()) { String name it.next(); System.out.println(name); }反向遍历ListIteratorString it list.listIterator(list.size()); while (it.hasPrevious()) { String name it.previous(); System.out.println(name); }遍历时添加元素ListIteratorString it list.listIterator(); while (it.hasNext()) { String name it.next(); if (李四.equals(name)) { it.add(赵六); } } System.out.println(list);//[张三, 李四, 赵六, 王五]三.List集合五种遍历方式对比
【Java-Day19】集合3 List中常见的方法和5种遍历方式
目录一.List集合的特有方法1.1 add(int index, E element)在指定位置插入元素。1.2 remove(int index) 删除指定索引的元素并返回被删除的元素。1.3 set(int index, E element) 修改指定索引的元素并返回被修改前的元素。1.4 get(int index) 获取指定索引的元素。二.List集合的遍历方式2.1 迭代器 Iterator2.2 增强 for2.3 Lambda 表达式 forEach2.4 普通 for 循环2.5 列表迭代器 ListIterator正向遍历反向遍历遍历时添加元素三.List集合五种遍历方式对比List是单列集合里非常常用的一类。特点是有序、可重复、有索引。常见实现类ArrayList、LinkedList。ListUser users new ArrayList();一.List集合的特有方法List 继承了 Collection 的所有方法如add(E e)remove(Object o)contains(Object o)clear()size()isEmpty()List集合因为自己有索引所以多了一些带索引操作的方法。方法名称说明voidadd(int index, E element)在此集合中的指定位置插入指定的元素Eremove(int index)删除指定索引处的元素返回被删除的元素Eset(int index, E element)修改指定索引处的元素返回被修改的元素Eget(int index)返回指定索引处的元素1.1 add(int index, E element)在指定位置插入元素。ListString list new ArrayList(); list.add(张三); list.add(王五); // 在索引 1 的位置插入 李四 list.add(1, 李四); System.out.println(list);//[张三, 李四, 王五]注意索引不能越界。1.2remove(int index)删除指定索引的元素并返回被删除的元素。ListString list new ArrayList(); list.add(张三); list.add(李四); String removed list.remove(1); System.out.println(被删除的是 removed);//被删除的是李四1.3set(int index, E element)修改指定索引的元素并返回被修改前的元素。String old list.set(1, 王五); System.out.println(被修改前的是 old);//被修改前的是李四1.4get(int index)获取指定索引的元素。System.out.println(list.get(0));二.List集合的遍历方式迭代器遍历继承Collection列表迭代器遍历増强for遍历继承CollectionLambda表达式遍历继承Collection普通for循环因为List集合存在索引我们准备一个集合ListString list new ArrayList(); list.add(张三); list.add(李四); list.add(王五);2.1 迭代器 Iterator适合遍历时删除元素。IteratorString it list.iterator(); while (it.hasNext()) { String name it.next(); System.out.println(name); }2.2 增强 for适合只读取元素。增强 for 里不要直接修改集合结构比如添加、删除元素。for (String name : list) { System.out.println(name); }2.3Lambda 表达式 forEachlist.forEach(name - System.out.println(name));2.4普通 for 循环使用size方法和get方法还有循环结合的方式利用索引获取到集合中的每一个元素。适合需要索引的场景。for (int i 0; i list.size(); i) { String name list.get(i); System.out.println(name); }2.5列表迭代器 ListIteratorListIterator是Iterrator的子接口。ListIterator 是List 特有的迭代器。它比普通 Iterator 更强可以从前往后遍历从后往前遍历遍历时添加元素遍历时修改元素正向遍历ListIteratorString it list.listIterator(); while (it.hasNext()) { String name it.next(); System.out.println(name); }反向遍历ListIteratorString it list.listIterator(list.size()); while (it.hasPrevious()) { String name it.previous(); System.out.println(name); }遍历时添加元素ListIteratorString it list.listIterator(); while (it.hasNext()) { String name it.next(); if (李四.equals(name)) { it.add(赵六); } } System.out.println(list);//[张三, 李四, 赵六, 王五]三.List集合五种遍历方式对比