当前位置:   article > 正文

Java List与ArrayList_java list和arraylist

java list和arraylist

目录

List的介绍

什么是List?

List的使用

ArrayList与顺序表

ArrayList简介

 ArrayList的使用

 ArrayList的常见操作

 ArrayList的扩容机制

 ArrayList的模拟实现


List的介绍

什么是List?

在集合框架中,List是一个接口,继承自Collection。

Collection也是一个接口,该接口中规范了后序容器中常用的一些方法,具体如下所示:

站在数据结构的角度来看,List就是一个线性表,即n个具有相同类型元素的有限序列,在该序列上可以执行增删改查。

List的使用

List是个接口,并不能直接用来实例化。

如果要使用,必须去实例化List的实现类。

在集合框架中,ArrayList和LinkedList都实现了List接口。

ArrayList与顺序表

ArrayList简介

在集合框架中,ArrayList是一个普通的类,实现了List接口,具体框架图如下:

 1. ArrayList实现了RandomAccess接口,表明ArrayList支持随机访问

2. ArrayList实现了Cloneable接口,表明ArrayList是可以clone的

3. ArrayList实现了Serializable接口,表明ArrayList是支持序列化的

4. 和Vector不同,ArrayList不是线程安全的,在单线程下可以使用,在多线程中可以选择Vector或者CopyOnWriteArrayList

5. ArrayList底层是一段连续的空间,并且可以动态扩容,是一个动态类型的顺序表

 ArrayList的使用

  1. public static void main1(String[] args) {
  2. // 此时底层的数组 大小 默认是0 ,
  3. ArrayList<Integer> list = new ArrayList<>();
  4. ArrayList<Integer> list2 = new ArrayList<>(20);
  5. //当你第一次进行add的时候,此时大小才被分为了10
  6. list.add(1);
  7. list.add(2);
  8. list.add(3);
  9. list.add(4);
  10. System.out.println(list);
  11. for (int i = 0; i < list.size(); i++) {
  12. System.out.print(list.get(i)+" ");
  13. }
  14. System.out.println("=============");
  15. for (Integer x : list) {
  16. System.out.print(x+" ");
  17. }
  18. }

 ArrayList的常见操作

 ArrayList的扩容机制

1. 检测是否真正需要扩容,如果是调用grow准备扩容

2. 预估需要库容的大小

        a.初步预估按照1.5倍大小扩容

        b.如果用户所需大小超过预估1.5倍大小,则按照用户所需大小扩容

        c.真正扩容之前检测是否能扩容成功,防止太大导致扩容失败

3. 使用copyOf进行扩容

 ArrayList的模拟实现

  1. import java.util.Arrays;
  2. public class MyArrayList {
  3. public int[] elem;//null
  4. public int usedSize;//0
  5. public MyArrayList() {
  6. this.elem = new int[5];
  7. }
  8. // 新增元素,默认在数组最后新增
  9. public void add(int data) {
  10. //1、判断是不是满的
  11. if(isFull()) {
  12. //扩容
  13. this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
  14. }
  15. this.elem[this.usedSize] = data;
  16. this.usedSize++;
  17. }
  18. public boolean isFull() {
  19. if(this.usedSize == this.elem.length) {
  20. return true;//满的
  21. }
  22. return false;
  23. }
  24. // 打印顺序表
  25. public void myToString() {
  26. for (int i = 0; i < this.usedSize; i++) {
  27. System.out.print(this.elem[i]+" ");
  28. }
  29. System.out.println();
  30. }
  31. // 在 pos 位置新增元素
  32. public void add(int pos, int data) {
  33. //0、对pos位置进行合法判断
  34. if(pos < 0 || pos > this.usedSize) {
  35. System.out.println("pos位置不合法!");
  36. return;
  37. }
  38. //1、不能是满的
  39. if(isFull()) {
  40. //扩容
  41. this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
  42. }
  43. //2、正常的插入
  44. for (int i = this.usedSize-1; i >= pos ; i--) {
  45. this.elem[i+1] = this.elem[i];
  46. }
  47. this.elem[pos] = data;
  48. this.usedSize++;
  49. }
  50. // 判定是否包含某个元素
  51. public boolean contains(int toFind) {
  52. for (int i = 0; i < this.usedSize; i++) {
  53. if(this.elem[i] == toFind) {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. // 查找某个元素对应的位置
  60. public int indexOf(int toFind) {
  61. for (int i = 0; i < this.usedSize; i++) {
  62. if(this.elem[i] == toFind) {
  63. return i;
  64. }
  65. }
  66. return -1;//数组没有-1下标
  67. }
  68. // 获取 pos 位置的元素
  69. public int get(int pos) {
  70. if(pos < 0 || pos >= this.usedSize) {
  71. System.out.println("pos位置不合法!");
  72. throw new RuntimeException("pos位置不合法!");
  73. //return -1;//这里只是检查一下,实际业务处理,这里需要抛异常,自定义的异常
  74. }
  75. return this.elem[pos];
  76. }
  77. // 给 pos 位置的元素设为 value
  78. public void set(int pos, int value) {
  79. if(pos < 0 || pos >= this.usedSize) {
  80. System.out.println("pos位置不合法!");
  81. throw new RuntimeException("pos位置不合法!");
  82. }
  83. this.elem[pos] = value;//覆盖原来的值
  84. }
  85. //删除第一次出现的关键字key
  86. public void remove(int key) {
  87. if(isEmpty()) {
  88. System.out.println("空的 不能删除!");
  89. return;
  90. }
  91. int index = indexOf(key);
  92. for (int i = index; i < this.usedSize-1; i++) {
  93. this.elem[i] = this.elem[i+1];
  94. }
  95. this.usedSize--;
  96. //this.elem[this.usedSize] = null; 如果是引用数据类型,一定要置为空
  97. }
  98. public boolean isEmpty() {
  99. if(this.usedSize == 0) {
  100. return true;
  101. }
  102. return false;
  103. }
  104. // 获取顺序表长度
  105. public int size() {
  106. return this.usedSize;
  107. }
  108. // 清空顺序表
  109. public void clear() {
  110. // for (int i = 0; i < this.usedSize; i++) {
  111. // this.elem[i] = null;
  112. // }
  113. this.usedSize = 0;
  114. }
  115. }
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/752601
推荐阅读
相关标签
  

闽ICP备14008679号