JAVA实现线性表顺序存储结构ArrayList

  • 线性表的顺序存储结构底层结构采用数组进行实现,主要方法包括元素在集合中的增删改查,核心方法是当添加元素前进行集合容量的判断并进行扩容处理。详见代码

  • 优点:元素查找方便。 缺点:删除元素比较麻烦,需要移动元素位置

  
  
  1. /**
  2. * ArrayList底层使用数组结构,暴露方法,增删改查 其中元素增加,需要判断数组的长度
  3. */
  4. public class MyArrayList<E> {
  5. private Object[] array;// 存放元素的数组
  6. private Object[] EMPTY_ARRAY = {};// 空数组
  7. private static final int DEFAULT_CAPACITY = 8;// 默认数组长度
  8. private int size;// 数组中元素的个数
  9. private int modSize;// 线性表修改的次数
  10. public MyArrayList() {
  11. array = EMPTY_ARRAY;// 默认是给个空数组还是给个8个空间的数组呢?
  12. }
  13. public MyArrayList(int initCapacity) {
  14. if (initCapacity < 0) {// 传入的数量为负数,抛出异常
  15. throw new IllegalArgumentException("参数错误:" + initCapacity);
  16. } else if (initCapacity == 0) {// 空数组
  17. array = EMPTY_ARRAY;
  18. } else {
  19. array = new Object[initCapacity];
  20. }
  21. }
  22. public MyArrayList(Collection<E> c) {
  23. Object[] obj = c.toArray();
  24. if ((size = obj.length) != 0) {// 将Collection中的数据拷贝出来,防止污染
  25. System.arraycopy(obj, 0, array, 0, size);
  26. } else {
  27. array = EMPTY_ARRAY;
  28. }
  29. }
  30. /**
  31. * 添加一个元素到线性表尾部->先判断数组大小和元素个数是否相同-》相同的话,需要扩容
  32. *
  33. * @param e
  34. * @return
  35. */
  36. public boolean add(E e) {
  37. array = judgeIsGrow();
  38. array[size] = e;
  39. size++;
  40. modSize++;
  41. return true;
  42. }
  43. // 判断是否扩容
  44. private Object[] judgeIsGrow() {
  45. if (size == array.length) {
  46. // 确定新数组的大小--》new出来--》将原来数组的数据拷贝到新数组中
  47. int newSize = 0;
  48. if (size == 0) {
  49. newSize = DEFAULT_CAPACITY;
  50. } else {
  51. newSize = size < Integer.MAX_VALUE / 2 ? size << 1 : Integer.MAX_VALUE;
  52. }
  53. Object[] newArray = new Object[newSize];
  54. System.arraycopy(array, 0, newArray, 0, size);
  55. array = newArray;
  56. }
  57. return array;
  58. }
  59. /**
  60. * 在第index位置插入一个元素
  61. *
  62. * @param index
  63. * @param e
  64. * @return
  65. */
  66. public boolean add(int index, E e) {
  67. checkArgument(index);
  68. array = judgeIsGrow();
  69. Object[] newObj = new Object[array.length];
  70. // 将array数组中的元素拷贝到新数组中
  71. System.arraycopy(array, 0, newObj, 0, index);
  72. System.arraycopy(array, index, newObj, index + 1, size- index);
  73. newObj[index] = e;
  74. array = newObj;
  75. size++;
  76. modSize++;
  77. return true;
  78. }
  79. // 参数检查
  80. private void checkArgument(int index) {
  81. if (index < 0) {
  82. throw new IllegalArgumentException("参数错误:" + index);
  83. } else if (index > size) {
  84. throw new IllegalArgumentException("超过数组长度,参数错误:" + index);
  85. }
  86. }
  87. /**
  88. * 删除第index个元素
  89. *
  90. * @param index
  91. * @return
  92. */
  93. @SuppressWarnings("unchecked")
  94. public E remove(int index) {
  95. Object[] newObj = new Object[array.length];
  96. E obj = (E) array[index];
  97. // 将array数组中的元素拷贝到新数组中
  98. System.arraycopy(array, 0, newObj, 0, index);
  99. System.arraycopy(array, index + 1, newObj, index, size - index - 1);
  100. array = newObj;
  101. size--;
  102. modSize++;
  103. return obj;
  104. }
  105. @SuppressWarnings("unchecked")
  106. public E get(int index) {
  107. return (E) array[index];
  108. }
  109. public void set(int index, E e) {
  110. array[index] = e;
  111. }
  112. public int size() {
  113. return size;
  114. }
  115. public static void main(String[] args) {
  116. MyArrayList<String> list = new MyArrayList<>();
  117. for (int i = 0; i < 10; i++) {
  118. list.add("data "+i);
  119. }
  120. list.add(2,"add23");
  121. list.add(11,"dddd");
  122. list.remove(3);
  123. list.set(9, "dajdfljfl");
  124. int size = list.size();
  125. for (int i = 0; i < size; i++) {
  126. System.out.println("元素:"+(i+1)+" == " + list.get(i));
  127. }
  128. }
  129. }

结果打印:

元素:1 == data 0

元素:2 == data 1

元素:3 == add23

元素:4 == data 3

元素:5 == data 4

元素:6 == data 5

元素:7 == data 6

元素:8 == data 7

元素:9 == data 8

元素:10 == dajdfljfl

元素:11 == dddd


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值