JavaSE——集合2:List(Iterator迭代器、增强for、普通for循环遍历集合)

embedded/2024/10/18 10:26:55/

目录

List-toc" style="margin-left:0px;">一、List

List%E6%8E%A5%E5%8F%A3%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D-toc" style="margin-left:40px;">(一)List接口基本介绍

List%E6%8E%A5%E5%8F%A3%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95-toc" style="margin-left:0px;">二、List接口的常用方法

List%E9%9B%86%E5%90%88%E7%9A%84%E4%B8%89%E7%A7%8D%E9%81%8D%E5%8E%86%E6%96%B9%E5%BC%8F-toc" style="margin-left:0px;">三、List集合的三种遍历方式

四、小练习——使用冒泡排序遍历集合


一、List

List%E6%8E%A5%E5%8F%A3%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D">(一)List接口基本介绍

List接口是Collection接口的子接口

public interface List<E> extends Collection<E> 
  1. List集合类中元素有序(即添加顺序和取出顺序一致)、且可重复
    List list = new ArrayList();
    list.add("jack");
    list.add("mary");
    list.add("tom");
    list.add("tom");
    System.out.println("list=" + list);
    // list=[jack, mary, tom, tom]
  2. List集合中的每个元素都有其对应的顺序索引,即支持索引。索引是从0开始的,可以根据索引存取集合中的元素。
    System.out.println(list.get(2)); // tom
  3. JDK API中List接口的实现类有:常用的是ArrayList、LinkedList和Vector

List%E6%8E%A5%E5%8F%A3%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95">二、List接口的常用方法

// 准备数据
List list = new ArrayList();
list.add("jack");
list.add("mary");
list.add("tom");
list.add("tom");// void add(int index, Object ele):在index位置插入元素
list.add(1, "john");
System.out.println("list=" + list);
// list=[jack, john, mary, tom, tom]// boolean addAll(int index, Collection eles):从index位置开始将eles中的所有元素添加进来
List list2 = new ArrayList();
list2.add("张三");
list2.add("李四");
list.addAll(3, list2);
System.out.println("list=" + list);
// list=[jack, john, mary, 张三, 李四, tom, tom]// get(int index):获取指定index位置的元素
System.out.println(list.get(5)); // tom// int indexOf(Object obj):返回obj在集合中首次出现的位置
System.out.println(list.indexOf("tom")); // 5// int lastindexOf(Object obj):返回obj在集合中末次出现的位置
System.out.println(list.lastIndexOf("tom")); // 6// Object remove(int index):移除指定index位置的元素,并返回此元素
Object remove = list.remove(3);
System.out.println("移除的元素是:" + remove); // 张三// Object set(int index, Object ele):设置指定index位置的元素为ele,相当于是替换
System.out.println("替换前:list=" + list);
// 替换前:list=[jack, john, mary, 李四, tom, tom]Object ele = list.set(3, "王五");
System.out.println(ele + "替换后:list=" + list);
// 李四替换后:list=[jack, john, mary, 王五, tom, tom]// List subList(int fromIndex, int toIndex):返回从fromIndex到toIndex位置的子集合
// 注意:包头不包尾[fromIndex, toIndex)
List subList = list.subList(1, 5);
System.out.println("subList=" + subList);
// subList=[john, mary, 王五, tom]

List%E9%9B%86%E5%90%88%E7%9A%84%E4%B8%89%E7%A7%8D%E9%81%8D%E5%8E%86%E6%96%B9%E5%BC%8F">三、List集合的三种遍历方式

注意:ArrayList、LinkedList和Vector集合的遍历,都适用于下面的遍历方法。

List list = new ArrayList();
// List list = new LinkedList();
// List list = new Vector();for (int i = 0; i < 12; i++) {list.add("hello" + i);
}
// 方式一:迭代器遍历
Iterator iterator = list.iterator();
while (iterator.hasNext()){Object obj = iterator.next();System.out.println(obj);
}// 方式二:增强for
for (Object obj : list) {System.out.println(obj);
}// 方式三:普通for循环
for (int i = 0; i < list.size(); i++) {Object obj = list.get(i);System.out.println(obj);
}

四、小练习——使用冒泡排序遍历集合

创建Book类:

class Book {private String name;private String author;private double price;public Book(String name, String author, double price) {this.name = name;this.author = author;this.price = price;}@Overridepublic String toString() {return "名称:" + name + "\t\t价格:" + price + "\t\t作者:" + author;}public double getPrice() {return price;}
}

创建集合: 

public class ListExercise02 {public static void main(String[] args) {List list = new ArrayList();//  List list2 = new LinkedList();//  List list3 = new Vector();list.add(new Book("红楼梦", "曹雪芹", 100));list.add(new Book("西游记", "吴承恩", 10));list.add(new Book("水浒传", "施耐庵", 9));list.add(new Book("三国", "罗贯中", 80));list.add(new Book("西游记", "吴承恩", 10));        }   
}

编写冒泡排序,从大到小排序:

public static void sort(List list) {for (int i = 0; i < list.size() - 1; i++) {for (int j = 0; j < list.size() - 1 - i; j++) {// 强制类型转换Book book1 = (Book) list.get(j);Book book2 = (Book) list.get(j + 1);if (book1.getPrice() < book2.getPrice()) {// 替换list.set(j, book2);list.set(j + 1, book1);}}}}

main方法中调用sort()方法: 

// 排序前
for (Object o : list) {System.out.println(o);
}
sort(list);System.out.println("---------------排序后------------------");
// 排序后
for (Object o : list) {System.out.println(o);
}

运行结果:


http://www.ppmy.cn/embedded/127787.html

相关文章

CMake函数:get_filename_component——从文件路径中提取特定组件

get_filename_component是CMake中的一个命令&#xff0c;用于从文件路径中提取特定组件&#xff08;例如目录、文件名、扩展名等&#xff09;。它的语法如下&#xff1a; get_filename_component(<VAR> <FileName> <COMP> [CACHE])其中&#xff1a; <VA…

Animatediff 工作流之神 Jerry Davos 新作! 使用Differential Diffusion使视频转绘生成稳定的背景。

今天给大家介绍一个新的ComfyUI工作流程&#xff0c;是Animatediff 工作流之神 Jerry Davos 新作。利用 Differential Diffusion 确保视频转绘的时候生成稳定的背景。 它可以使用蒙版对主体和背景进行不同的降噪值降噪&#xff0c;也可以设置它们的控制网为不同的强度。这样&a…

一种用于机械手自适应抓取控制的紧凑型指尖形视触觉传感器

背景 在机器人操作中&#xff0c;手部触觉感知对于稳定抓取起着重要作用。然而&#xff0c;传统的机械手多依赖于固定的抓力预设&#xff0c;无法灵活调整以适应不同类型的物体。尤其在处理脆弱、柔软或不规则物体时&#xff0c;预设的抓力可能导致物体损坏或抓取失败。为此&am…

国产化工业AI浪潮下,鸿道Intewell操作系统加速生产自动化向智能化转变

鸿道&#xff08;Intewell&#xff09;操作系统全面展示工业AI自主力量&#xff0c;加快生产自动化向智能化转变。 “将A处所有不同形状不同颜色的小方块移动到B处&#xff0c;并整齐堆叠。”工作人员对着眼前一台机械臂模样的工业机器人发出指令。短暂几秒后&#xff0c;“听懂…

STL.string(中)

string 迭代器findswapsubstrrfindfind_first_of&#xff08;用的很少&#xff09;find_last_of&#xff08;用的很少&#xff09;find_first_not_of&#xff08;用的很少&#xff09; 迭代器 int main() {//正向迭代器string s1("hello world!");string::iterator i…

自制简单的黄金投资预测脚本

github地址&#xff1a;https://github.com/CaLlMeErIC/GoldInvest 起因是在蚂蚁财富上有黄金ETF&#xff0c;就想着能不能写个脚本&#xff0c;通过读取历史黄金数据来给出一定的投资建议。 首先是数据来源&#xff0c;可以通过yfinance 来下载&#xff0c;不仅可以下载黄金数…

我常用的两个单例模式写法 (继承Mono和不继承Mono的)

不继承Mono 不继承Mono代表不用挂载到场景物体上面&#xff0c;因此直接饿汉式 加 合并空运算符判空创建实例 >(lambda表达式)的意思是get&#xff0c;就是将instance赋给Instance属性 //单例private static JsonDataManager instance new JsonDataManager();public stati…

框架一 Mybatis Spring SpringMVC(东西居多 后边的没怎么处理)

Mybatis 使用简单的XML或注解来配置和映射原生类型、接 口和Java的POJO (Plain Old Java Objects,普通老式Java对象)为数据库中的记录。 ${}和#{}的区别是 ${}替换成变量的值 #{}替换成&#xff1f; Mybatis中&#xff0c;resultType和ResultMap的区别是 如果数据库列名和…