当前位置:   article > 正文

【数据结构】ArrayList与顺序表

【数据结构】ArrayList与顺序表

目录

1.线性表

2.顺序表

2.1接口的实现

3. ArrayList简介

​编辑

4.ArrayList使用

4.1 ArrayList的构造

4.2ArraysList常见操作


1.线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列...
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
 

2.顺序表

2.1接口的实现

我们先自己来完成一个顺序表8:

 具体效果如图:

源码如下:

建议小伙伴们自己思考一下上手敲一敲代码,对后续的学习可以更好的理解哟~

MyArrayList.java

  1. import javax.xml.bind.annotation.XmlType;
  2. import java.lang.reflect.Array;
  3. import java.util.Arrays;
  4. public class MyArratList {
  5. public int[] elem;
  6. public int usedSize;
  7. public static final int DEFAULT_SIZE = 10;
  8. public MyArratList(){
  9. this.elem = new int[DEFAULT_SIZE];
  10. }
  11. //打印数组
  12. public void display(){
  13. for (int i = 0; i < this.usedSize; i++) {
  14. System.out.print(elem[i]+" ");
  15. }
  16. System.out.println();
  17. }
  18. //新增元素,默认在素组最后新增
  19. public void add(int data) {
  20. //1.判断是不是满了
  21. if(isFull()){
  22. // 2.如果满了,要扩容
  23. this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
  24. }
  25. //3.增加数据
  26. this.elem[this.usedSize] = data;
  27. //4.useSize++
  28. usedSize++;
  29. }
  30. public int size() {
  31. return this.usedSize;
  32. }
  33. public boolean isFull(){
  34. if(size() >= this.elem.length ){
  35. return true;
  36. }
  37. return false;
  38. }
  39. // 在 pos 位置新增元素
  40. //如果pos位置不合法,那么就会抛出一个PosWronfulExpection
  41. public void add(int pos, int data) throws PosWronfulExpection{
  42. if(isFull()){
  43. System.out.println("满了!");
  44. this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
  45. }
  46. if(pos < 0 || pos > this.usedSize){
  47. System.out.println("pos位置不合法!");
  48. throw new PosWronfulExpection("pos位置不合法!");
  49. }
  50. //pos合法
  51. //1.开始挪动数据
  52. for (int i = usedSize-1; i >= pos; i--) {
  53. this.elem[i+1] = this.elem[i];
  54. }
  55. //2.插入数据
  56. this.elem[pos] = data;
  57. //3.useSize++
  58. usedSize++;
  59. }
  60. // 判定是否包含某个元素
  61. public boolean contains(int toFind) {
  62. for (int i = 0; i < usedSize; i++) {
  63. if(toFind == this.elem[i]){
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. // 查找某个元素对应的位置
  70. public int indexOf(int toFind) {
  71. for (int i = 0; i < usedSize; i++) {
  72. if(toFind == elem[i]){
  73. return i;
  74. }
  75. }
  76. return -1;
  77. }
  78. // 获取 pos 位置的元素
  79. public int get(int pos) {
  80. if(isEmpty()){
  81. throw new EmptyExpection("当前顺序表为空!");
  82. }
  83. if(pos < 0||pos >=this.usedSize){
  84. throw new PosWronfulExpection("pos不合法!");
  85. }
  86. return elem[pos];
  87. }
  88. public boolean isEmpty(){
  89. return size()== 0;
  90. }
  91. // 给 pos 位置的元素设为 value
  92. public void set(int pos, int value) {
  93. if(isEmpty()){
  94. throw new EmptyExpection("当前顺序表为空!!");
  95. }
  96. if(pos < 0||pos >=this.usedSize){
  97. throw new PosWronfulExpection("pos位置不合法!!!");
  98. }
  99. this.elem[pos] = value;
  100. }
  101. //删除第一次出现的关键字key
  102. public void remove(int key) {
  103. if(isEmpty()){
  104. throw new EmptyExpection("当前顺序表为空!");
  105. }
  106. int index = this.indexOf(key);
  107. if(index == -1){
  108. System.out.println("没有这个数字!");
  109. }
  110. for (int i = index; i < this.usedSize-1; i++) {
  111. this.elem[i] = this.elem[i+1];
  112. }
  113. usedSize--;
  114. }
  115. // 获取顺序表长度
  116. // 清空顺序表
  117. public void clear() {
  118. /* //当变量是引用类型时:
  119. for (int i = 0; i < size(); i++) {
  120. this.elem = null;
  121. }*/
  122. this.usedSize = 0;
  123. }
  124. }

 EmptyExpection.java

  1. public class EmptyExpection extends RuntimeException{
  2. public EmptyExpection() {
  3. }
  4. public EmptyExpection(String message) {
  5. super(message);
  6. }
  7. }

 PosWronfulExpection.java

  1. public class PosWronfulExpection extends RuntimeException{
  2. public PosWronfulExpection(){
  3. }
  4. public PosWronfulExpection(String message){
  5. super(message);
  6. }
  7. }

 TestList.java

  1. import com.sun.xml.internal.bind.v2.TODO;
  2. public class TestList {
  3. public static void main(String[] args) {
  4. MyArratList myArratList = new MyArratList();
  5. myArratList.add(1);
  6. myArratList.add(2);
  7. myArratList.add(3);
  8. myArratList.display();
  9. try {
  10. myArratList.add(1,10);
  11. }catch (PosWronfulExpection e){
  12. e.printStackTrace();
  13. }
  14. myArratList.display();
  15. //trycatch的快捷键!!
  16. System.out.println(myArratList.contains(10));
  17. System.out.println(myArratList.contains(100));
  18. System.out.println(myArratList.indexOf(10));
  19. System.out.println(myArratList.indexOf(100));
  20. try{
  21. System.out.println(myArratList.get(1));
  22. }catch (PosWronfulExpection e){
  23. e.printStackTrace();
  24. }
  25. System.out.println("-------------------------------------");
  26. myArratList.set(0,99);
  27. myArratList.display();
  28. }
  29. }

3. ArrayList简介

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

 【说明】
1. ArrayList实现了RandomAccess接口,表明ArrayList支持随机访问
2. ArrayList实现了Cloneable接口,表明ArrayList是可以clone的
3. ArrayList实现了Serializable接口,表明ArrayList是支持序列化的
4. 和Vector不同,ArrayList不是线程安全的,在单线程下可以使用,在多线程中可以选择Vector或者CopyOnWriteArrayList
5. ArrayList底层是一段连续的空间,并且可以动态扩容,是一个动态类型的顺序表
 

4.ArrayList使用

4.1 ArrayList的构造

方法解释
ArrayList()无参构造
ArrayList(Collection<? extends E> c)利用其他 Collection 构建 ArrayList
ArrayList(int initialCapacity)指定顺序表初始容量

4.2ArraysList常见操作

详情可以去帮助手册中查找:

方法解释
boolean add(E e)尾插 e
void add(int index, E element)将 e 插入到 index 位置
boolean addAll(Collection<? extends E> c)尾插 c 中的元素
E remove(int index)删除 index 位置元素
boolean remove(Object o)删除遇到的第一个 o
E get(int index)获取下标 index 位置元素
E set(int index, E element)将下标 index 位置元素设置为 element
void clear()清空
boolean contains(Object o)判断 o 是否在线性表中
int indexOf(Object o)返回第一个 o 所在下标
int lastIndexOf(Object o)返回最后一个 o 的下标
List<E> subList(int fromIndex, int toIndex)截取部分 list
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/477378
推荐阅读
相关标签
  

闽ICP备14008679号