当前位置: 首页 > news >正文

Java :List,LinkedList,ArrayList

文章目录

  • List常用方法
  • List集合的遍历方式
  • ArrayList底层的原理
  • LinkedList底层原理
    • 常用方法

List常用方法

在这里插入图片描述

//1.创建一个ArrayList集合对象(有序、有索引、可以重复)
List<String> list = new ArrayList<>();
list.add("蜘蛛精");
list.add("至尊宝");
list.add("至尊宝");
list.add("牛夫人"); 
System.out.println(list); //[蜘蛛精, 至尊宝, 至尊宝, 牛夫人]//2.public void add(int index, E element): 在某个索引位置插入元素
list.add(2, "紫霞仙子");
System.out.println(list); //[蜘蛛精, 至尊宝, 紫霞仙子, 至尊宝, 牛夫人]//3.public E remove(int index): 根据索引删除元素, 返回被删除的元素
System.out.println(list.remove(2)); //紫霞仙子
System.out.println(list);//[蜘蛛精, 至尊宝, 至尊宝, 牛夫人]//4.public E get(int index): 返回集合中指定位置的元素
System.out.println(list.get(3));//5.public E set(int index, E e): 修改索引位置处的元素,修改后,会返回原数据
System.out.println(list.set(3,"牛魔王")); //牛夫人
System.out.println(list); //[蜘蛛精, 至尊宝, 至尊宝, 牛魔王]

List集合的遍历方式

有四种,多了一种可以用索引遍历的方式
普通for循环,迭代器,增强for,forEach

public class Test_List_for {public static void main(String[] args) {List<String> list = new ArrayList<>();list.add("张三");list.add("李四");list.add("王五");//1. 普通for循环for (int i = 0; i < list.size(); i++) {System.out.println(i);// 索引System.out.println(list.get(i));}//2. 增强forfor(String s : list){System.out.println(s);}//3. 迭代器遍历Iterator<String> it = list.iterator();while(it.hasNext()){System.out.println(it.next());}//4. forEachlist.forEach(s -> System.out.println(s));}
}

ArrayList底层的原理

ArrayList集合底层是基于数组结构实现的,也就是说当往集合容器中存储元素时,底层本质上是往数组里存数据
在这里插入图片描述

  1. 数组长度不可变,但集合长度可变
    数组不可以扩容,底层是创建一个新的数组,把原数组的所有元素都复制到新数组去

LinkedList底层原理

LinkedList底层是链表结构,双向链表。可以用来设计栈结构,队列结构
在这里插入图片描述

常用方法

在这里插入图片描述

public static void main(String[] args) {// LinkedList模拟队列:先进先出LinkedList<String> queue = new LinkedList<>();// 入队queue.addLast("1人");queue.addLast("2人");queue.addLast("3人");queue.addLast("4人");System.out.println(queue);// 出队System.out.println(queue.removeFirst());System.out.println(queue.removeFirst());System.out.println(queue.removeFirst());System.out.println(queue.removeFirst());System.out.println("`````````````````");// LinkedList模拟栈:后进先出LinkedList<String> stack = new LinkedList<>();// 入栈  push = addFirststack.addFirst("1ren");stack.addFirst("2ren");stack.push("3ren");stack.push("4ren");System.out.println(stack);// 出栈  pop  = removeFirstSystem.out.println(stack.pop());System.out.println(stack.pop());System.out.println(stack.removeFirst());System.out.println(stack.removeFirst());System.out.println(stack);}

文章转载自:
http://abherent.zzyjnl.cn
http://chaunt.zzyjnl.cn
http://chemmy.zzyjnl.cn
http://bpi.zzyjnl.cn
http://bubonic.zzyjnl.cn
http://calcium.zzyjnl.cn
http://aspartate.zzyjnl.cn
http://carbamoyl.zzyjnl.cn
http://bobwhite.zzyjnl.cn
http://athwarthawse.zzyjnl.cn
http://cambrian.zzyjnl.cn
http://caespitose.zzyjnl.cn
http://backbeat.zzyjnl.cn
http://biogenesis.zzyjnl.cn
http://causalgia.zzyjnl.cn
http://blockbusting.zzyjnl.cn
http://biotron.zzyjnl.cn
http://adjudge.zzyjnl.cn
http://cereal.zzyjnl.cn
http://accouche.zzyjnl.cn
http://bloodfin.zzyjnl.cn
http://amphictyonic.zzyjnl.cn
http://abortion.zzyjnl.cn
http://battledore.zzyjnl.cn
http://blockade.zzyjnl.cn
http://carriageable.zzyjnl.cn
http://amphibology.zzyjnl.cn
http://anthracoid.zzyjnl.cn
http://bullterrier.zzyjnl.cn
http://awn.zzyjnl.cn
http://www.dtcms.com/a/280590.html

相关文章:

  • ov5640,ov2640,ov7670摄像头比较
  • OSPF过滤
  • 在百亿流量面前,让“不存在”无处遁形——Redis 缓存穿透的极限攻防实录
  • uniapp 微信小程序Vue3项目使用内置组件movable-area封装悬浮可拖拽按钮(拖拽结束时自动吸附到最近的屏幕边缘)
  • 解锁Python爬虫:数据获取与清洗的进阶指南
  • 运维技术教程之Jenkins的秘钥设置
  • TP商城登录系统测试报告
  • Python Fabric库详解:从入门到自动化运维实战
  • C++ Boost Aiso TCP 网络聊天(服务端客户端一体化)
  • 【论文阅读 | PR 2024 |ITFuse:一种用于红外与可见光图像融合的交互式 Transformer】
  • 第三章 OB SQL 引擎高级技术
  • 【网络安全】大型语言模型(LLMs)及其应用的红队演练指南
  • 【Git】详解git commit --amend用法以及使用遇到的问题
  • Vue 2 和 Vue 3 中,组件的封装、二次开发和优化
  • Sersync和Rsync部署
  • Keil 5下载的时候提示“No J-Link found”
  • 《恋与深空》中龙和蛇分别是谁的代表
  • 25、企业能源管理(Energy):锚定双碳目标,从分类管控到智能优化的数字化转型之路
  • flutter弹窗:fluttertoast
  • HTTP 性能优化实战:突破高并发瓶颈的工业级方案
  • elasticsearch 下载/安装
  • 飞睿UWB超宽带定位测距技术,数字钥匙重塑智能生活,高精度厘米级定位无感解锁
  • ffmpeg音视频处理大纲
  • HR数字化转型:3大痛点解决方案与效率突破指南
  • QT 中各种坑
  • 基于Scikit-learn的机器学习建模与SHAP解释分析
  • 如何解决 Spring Boot 使用 Maven 打包后运行失败的问题(附详细排查步骤)
  • [雨云教程]端口冲突该如何解决
  • 前端报错:“Uncaught SyntaxError: missing ) after argument list
  • 【学习笔记】条件变量+互斥锁解决问题