java的List接口
2021/6/29 14:21:24
本文主要是介绍java的List接口,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
JAVA集合概述
文章目录
- java的List接口
- List实现类
- ArrayList实现类
- ArrayList常用方法
- LinkedList实现类
- LinkedList常用方法
- List接口总结
java的List接口
List实现类
List接口,在jdk的API的说明文档查看:
在这里只是简单介绍ArrayList和LinkedList
ArrayList实现类
ArrayList实现类在java.util.ArrayList
中,在API中查看基本信息
ArrayList的特点如下:
ArrayList是一个长度可变的数组
方便遍历元素和随机访问数组
ArrayList实现了长度可变的数组,在内存中分配连续的存储空间,遍历元素和随机访问元素的效率比较高
ArrayList是List接口的一个具体的实现类 ,实现类可变大小的数组,给随机访问和遍历元素提供更好的性能
接下来介绍一些ArrayLis创建和几个常用的方法,更多的方法可以在jdk的API文档中查看学习
ArrayList常用方法
写一个新闻管理系统,有如下的需求:
可以存储各类新闻标题(ID 名称 创建者)
可以获取新闻标题的总数
可以逐条打印每条新闻标题的名称
package cn.zhz.Kind; public class NewsTitle { private int id; private String title; private String author; public NewsTitle() { } public NewsTitle(int id, String title, String author) { super(); this.id = id; this.title = title; this.author = author; } public String getTitle() { return title; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public void setTitle(String title) { this.title = title; } }
package cn.zhz.Test; import java.util.ArrayList; import cn.zhz.Kind.NewsTitle; public class NewsMgr1 { public static void main(String[] args) { // 创建新闻标题对象 NewsTitle title1 = new NewsTitle(1, "北京热门景点故宫", "author"); NewsTitle title2 = new NewsTitle(2, "北京热门景点长城", "author"); NewsTitle title3 = new NewsTitle(3, "北京热门景点北海", "author"); NewsTitle title4 = new NewsTitle(4, "北京热门景点颐和园", "author"); NewsTitle title5 = new NewsTitle(5, "北京热门景点天安门", "author"); // 创建集合对象,将新闻标题加入到集合中 ArrayList list = new ArrayList(); // 依次添加在列表的末尾 list.add(title1); list.add(title2); list.add(title3); list.add(title4); // 在指定的位置中插入某个特定的元素 list.add(1, title5); // 获取新闻标题的总数 // ArrayList的size()方法等同于数组的length属性的作用 System.out.println("新闻标题一种有" + list.size() + "条记录"); System.out.println("************************************"); // 逐条打印新闻标题的名称 // 方法一:遍历ArrayList元素的位置(下标) for (int i = 0; i < list.size(); i++) { NewsTitle title = (NewsTitle) list.get(i);// 等同于list[i],返回的是Object System.out.println(title.getTitle()); } System.out.println("************************************"); // 方法二:增强型的for循环 for (Object obj : list) { NewsTitle title = (NewsTitle) obj; System.out.println(title.getTitle()); } // 判断列表中是否包含某个元素 System.out.println(list.contains(title1)); // 删除指定位置的列表元素 // list.remove(0); // 删除指定的元素 list.remove(title1); System.out.println(list.contains(title1)); System.out.println("************************"); System.out.println(list.size()); // 清空集合 list.clear(); System.out.println(list.size()); // 判断一个集合是否为空 System.out.println(list.isEmpty()); } }
可以自己尝试插入一个断点进行调试一下,体会一下:
LinkedList实现类
认识了上面的ArrayList,List家族中还有一个LinkedList,也就是链表的意思。
LinkedList采用链表存储方式,这样让插入和删除元素的效率比较高
LinkedList类是一个List接口的具体实现类,用来创建链表数据结构,插入和删除元素时,可以提高更好的性能
查看LinkedList的基本信息
和上面的ArrayList一样,接下来介绍LinkedList的创建和几个常用的方法:
LinkedList常用方法
还是上面的存储新闻的栗子,但是和上一个不同的是:
可以添加头条新闻标题
获取头条和最末条的新闻标题
可以删除末条的新闻标题
package cn.zhz.Test; import cn.zhz.Kind.NewsTitle; import java.util.LinkedList; public class NewsMgr2 { public static void main(String[] args) { // 创建新闻标题对象 NewsTitle title1 = new NewsTitle(1, "北京热门景点故宫", "author"); NewsTitle title2 = new NewsTitle(2, "北京热门景点长城", "author"); NewsTitle title3 = new NewsTitle(3, "北京热门景点北海", "author"); NewsTitle title4 = new NewsTitle(4, "北京热门景点颐和园", "author"); NewsTitle title5 = new NewsTitle(5, "北京热门景点天安门", "author"); LinkedList list = new LinkedList(); list.add(title1); list.add(title2); list.add(title3); list.add(title4); list.add(1,title5); NewsTitle title6 = new NewsTitle(6,"北京首都博物馆","author"); list.addFirst(title6); // 获取新闻标题的总数 // ArrayList的size()方法等同于数组的length属性的作用 System.out.println("新闻标题一种有" + list.size() + "条记录"); System.out.println("************************************"); // 逐条打印新闻标题的名称 // 方法一:遍历ArrayList元素的位置(下标) for (int i = 0; i < list.size(); i++) { NewsTitle title = (NewsTitle) list.get(i);// 等同于list[i],返回的是Object,强制数据类型转换 System.out.println(title.getTitle()); } System.out.println("************************************"); // 方法二:增强型的for循环 for (Object obj : list) { NewsTitle title = (NewsTitle) obj; System.out.println(title.getTitle()); } // 判断列表中是否包含某个元素 System.out.println(list.contains(title1)); // 删除指定位置的列表元素 // list.remove(0); // 删除指定的元素 list.remove(title1); System.out.println(list.contains(title1)); System.out.println("************************"); System.out.println(list.size()); // 清空集合 // list.clear(); System.out.println(list.size()); // 判断一个集合是否为空 System.out.println(list.isEmpty()); //获取头条和末条的新闻标题 System.out.println("***************************"); NewsTitle titleFirst = (NewsTitle) list.getFirst(); System.out.println("头条新闻信息:"+ titleFirst.getTitle()+ titleFirst.getAuthor()); NewsTitle titleLast = (NewsTitle) list.getLast(); System.out.println("最末新闻信息"+ titleLast.getTitle()+ titleLast.getAuthor()); list.removeLast(); System.out.println("删除后的新闻标题总共有:"+ list.size()+ "条"); for (Object obj : list) { NewsTitle title = (NewsTitle) obj; System.out.println(title.getTitle()); } } }
自己可以插入断点进行调试一下。
List接口总结
从上面的ArrayList和LinkedList,可以看出他们都有什么异同呢?
- LinkedList和ArrayList的相同点:
同样是List实现类 元素有序 不唯一 长度可变
都有Collection和List的通用方法
- LinkedList和ArrayList的不同点:
ArrayList实现了长度可变的数组,在内存中分配连续的空间,遍历元素和随机访问元素的效率比较高
LinkedList采用链表存储方式,插入和删除的效率比较高
这篇关于java的List接口的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南