当前位置:网站首页>ArrayList集合基本使用
ArrayList集合基本使用
2022-04-23 14:12:00 【Zinksl】
个人介绍
大家好我是:一颗松
认真分享技术,记录学习点滴
如果分享对你有用请支持我哦
点赞: 留言:收藏:️
个人格言: 想法落实的最佳时机就是现在!
集合概述
集合,长度可变的容器
1 ArrayList集合可变长度原理:
- ArrayList集合:底层原理也是,数组;初始大小长度为10
1 初始化一个长度为10的数组,数值依次存储,当数组存空间不够的时候进入第二环节;
2 创建一个原数组长度1.5倍的新数组,将原数组内容拷贝到新数组,再将第一环节没存到的数据,存到新数组中;
注意:集合与数组的选择【长度不变用数组,长度经常改变用集合】
2 ArrayList集合
- ArrayList构造方1
- public ArrayList();
- 泛型:<>
- 在类名之后加<>,可以使集合存储的数据类型,进行类型限制;
- 尖括号中只能使用引用数据类型,不能是基本数据类型
解决方案:【基本类型都有对应包装类】
基本数据类型 | 对应包装类 |
---|---|
int | Integer |
short | Short |
byte | Byte |
double | Double |
float | Float |
char | Character |
3 ArrayList的常用成员方法
- 增
- public boolean add(E e);向集合尾部添加数据,返回添加成功的状态
- public void add(int index,E element);插队添加
ArrayList<String> list1 = new ArrayList<>();//创建list集合
list1.add("张三"); //添加元素
- 删
- public E remove(int index);根据索引删除集合中的元素
- public boolean remove(元素);根据元素删除集合中的内容,重复元素只删除第一个;返回添加成功的状态
ArrayList<String> list1 = new ArrayList<>();//创建list集合
list1.add("张三"); //添加元素
list1.add("王五");
list1.add("上官铁蛋");
list1.add("张三");
list1.add("诸葛订单");
list1.remove("张三"); //根据元素删除
list1.remove(2); //根据索引值删除元素
- 改
- public E set(int index,E element);修改指定索引位置,返回被覆盖的元素
ArrayList<String> list1 = new ArrayList<>();//创建list集合
list1.add("张三"); //添加元素
list1.add("王五");
list1.add("上官铁蛋");
list1.add("张三");
list1.add("诸葛订单");
list1.set(2,"年薪百万"); //修改集合数据
- 查
- public E get(int index);根据索引,获取集合中的元素
ArrayList<String> list1 = new ArrayList<>();//创建集合
list1.add("张三"); //添加元素
list1.add("王五");
list1.add("上官铁蛋");
list1.add("张三");
list1.add("诸葛订单");
list1.get(2); //查找集合元素
4 ArrayList集合基本案例
4.1 案例1
创建一个存储字符串集合,存储5个字符串元素,找出4个字的人名,并打印
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<>();//创建list集合
list1.add("张三"); //添加元素
list1.add("王五");
list1.add("上官铁蛋");
list1.add("张三");
list1.add("诸葛订单");
for (int i = 0; i < list1.size(); i++) {
//遍历集合
String name = list1.get(i); //获取集合元素
if (list1.get(i).length()==4){
//判断字符串长度是否为4
System.out.println(name); //打印4个字的名字
}
}
}
结语
大佬请留步
既然看到这了不如点个赞再走吧
本文目的在于分享技术以及在学习过程中个人记得需要注意的点,记录学习过程;
如果出现错误欢迎大家指正,如有意见或建议欢迎在评论区讨论
版权声明
本文为[Zinksl]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Zinkse/article/details/124075695
边栏推荐
猜你喜欢
正则表达式
Operation instructions of star boundary automatic text translator (advanced version)
man man随记和crontab的@reboot用法
squid代理
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
顺序栈的基本操作
循环队列的基本操作(实验)
顺序表的操作,你真的学会了吗?
循环队列的基本操作,你学会了吗?
MySQL数据库讲解(八)
随机推荐
How to do a project easily
x509证书cer格式转pem格式
js 抛物线运动方法封装
API gateway / API gateway (IV) - use of Kong - Integrated JWT and fuse plug-in
快速搞懂线程实现的三种方式
KVM learning resources
source insight via samba
mysql 5.1升级到5.610
爬虫练习题(一)
Introduction to the use of countdownlatch and cyclicbarrier for inter thread control
SSH 通过跳板机连接远程主机
A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios
Operation instructions of star boundary text automatic translator
ActiveMQ Basics
Wechat applet rotation map swiper
MySQL数据库讲解(八)
TUN 设备原理
在电视屏幕上进行debug调试
dp-[NOIP2000]方格取数
Golang 对分片 append 是否会共享数据