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

【List】List集合有序测试案例:ArrayList,LinkedList,Vector(123)

List是有序、可重复的容器。
有序:
List中每个元素都有索引标记。可以根据元素的索引标记(在List中的位置)访问 元素,从而精确控制这些元素。
可重复:
List允许加入重复的元素。更确切地讲,List通常允许满足 e1.equals(e2) 的元素重复加入容器。
List接口常用的实现类有3个:ArrayList、LinkedList和Vector。

1.ArrayList:
ArrayList底层是用数组实现的存储。 特点:查询效率高,增删效率低,线程不安全。我们一般使用它。
ArrayList底层使用Object数组来存储元素数据。所有的方法,都围绕这个核心的Object数组来开展。

2.LinkedList:
LinkedList底层用双向链表实现的存储。特点:查询效率低,增删效率高,线程不安全。
双向链表也叫双链表,是链表的一种,它的每个数据节点中都有两个指针,分别指向前一个节点和后一个节点。 所以,从双向链表中的任意一个节点开始,都可以很方便地找到所有节点。

3.Vector:
Vector底层是用数组实现的List,相关的方法都加了同步检查,因此“线程安全,效率低”。 比如,copyInto方法就增加了synchronized同步标记。

使用原则:ArrayList、LinkedList、Vector

  1. 需要线程安全时,用Vector。
  2. 不存在线程安全问题时,并且查找较多用ArrayList(一般使用它)。
  3. 不存在线程安全问题时,增加或删除元素较多用LinkedList。

测试案例:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;public class day17 {public static void main(String[] args) {List<String> arrayList = new ArrayList<>();List<String> linkedList = new LinkedList<>();List<String> vector = new Vector<>();List<Integer> arrayList2 = new ArrayList<>();List<Integer> linkedList2 = new LinkedList<>();List<Integer> vector2 = new Vector<>();List<String> arrayList3 = new ArrayList<>();List<String> linkedList3 = new LinkedList<>();List<String> vector3 = new Vector<>();List<String> list1 = Arrays.asList("黄","河","之","水","天","上","来","奔","流","到","海","不","复","回","黄","河","之","水","天","上","来","奔","流","到","海","不","复","回");List<Integer> list2 = Arrays.asList(2  , 1  , 4 , 3  ,  6 , 5  ,7  ,  14, 13  ,12  , 11 ,10 , 9 , 8,2  , 1  , 4 , 3  ,  6 , 5  ,7  ,  8 , 9  ,10  , 11 ,12 , 13 , 14);List<String> list3 = Arrays.asList("2"  , "1"  , "4" , "3"  ,  "6" , "5"  ,"7"  ,  "14", "13"  ,"12"  , "11" ,"10" , "9" , "8","2"  , "1"  , "4" , "3"  ,  "6" , "5"  ,"7"  ,  "8" , "9"  ,"10"  , "11" ,"12" , "13" , "14");arrayList.addAll(list1);System.out.println("arrayList:"+arrayList);arrayList2.addAll(list2);System.out.println("arrayList2:"+arrayList2);arrayList3.addAll(list3);System.out.println("arrayList3:"+arrayList3);System.out.println("-------------------------------------------");linkedList.addAll(list1);System.out.println("linkedList:"+linkedList);linkedList2.addAll(list2);System.out.println("linkedList2:"+linkedList2);linkedList3.addAll(list3);System.out.println("linkedList3:"+linkedList3);System.out.println("-------------------------------------------");vector.addAll(list1);System.out.println("vector:"+vector);vector2.addAll(list2);System.out.println("vector2:"+vector2);vector3.addAll(list3);System.out.println("vector3:"+vector3);System.out.println("-------------------------------------------");}
}

测试输出:

arrayList:[,,,,,,,,,,,,,,,,,,,,,,,,,,,]
arrayList2:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
arrayList3:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
-------------------------------------------
linkedList:[,,,,,,,,,,,,,,,,,,,,,,,,,,,]
linkedList2:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
linkedList3:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
-------------------------------------------
vector:[,,,,,,,,,,,,,,,,,,,,,,,,,,,]
vector2:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
vector3:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
-------------------------------------------
http://www.lryc.cn/news/145845.html

相关文章:

  • 【javaweb】学习日记Day6 - Mysql 数据库 DDL DML
  • 使用 PyTorch C ++前端
  • 6、NoSQL的四大分类
  • (动态规划) 剑指 Offer 60. n个骰子的点数 ——【Leetcode每日一题】
  • ArrayList与顺序表
  • 【【萌新的STM32-22中断概念的简单补充】】
  • Java 中数据结构HashMap的用法
  • Request对象和response对象
  • 设计模式之桥接模式
  • pom.xml配置文件失效,显示已忽略的pom.xml --- 解决方案
  • 文本编辑器Vim常用操作和技巧
  • 【算法系列篇】位运算
  • 机器学习的测试和验证(Machine Learning 研习之五)
  • RNN循环神经网络
  • 安防视频监控/视频集中存储/云存储平台EasyCVR无法播放HLS协议该如何解决?
  • Docker技术--Docker的安装
  • 客户案例|MemFire Cloud助推应急管理业务,打造百万级数据可视化大屏
  • 蒲公英路由器如何设置远程打印?
  • 国产自主可控C++工业软件可视化图形架构源码
  • 【linux命令讲解大全】022.网络管理工具和命令概述
  • 应急响应流程及思路
  • 网页自适应
  • 什么是Sui Kiosk,它可以做什么,如何赋能创作者?
  • 【MySQL】mysql connect
  • 基于 vue2 发布 npm包
  • 基于Axios完成前后端分离项目数据交互
  • 时序预测 | MATLAB实现基于PSO-BiLSTM、BiLSTM时间序列预测对比
  • C# 生成唯一ID
  • python怎么提取视频中的音频
  • 学习设计模式之建造者模式,但是宝可梦