ArrayList

  1. package com.lius.javase.demo.collection;
  2. import java.util.Arrays;
  3. import java.util.Objects;
  4. /**
  5. * 手撸 ArrayList
  6. */
  7. public class MyArrayList {
  8. private Object[] elementData; // 存放元素的数组
  9. private int size; // 元素个数
  10. private static final Object[] EMPTY_ELEMENT_DATA = {}; // 空数组
  11. private static final int DEFAULT_LENGTH = 10; // 默认长度为10
  12. public MyArrayList() {
  13. this.elementData = EMPTY_ELEMENT_DATA;
  14. }
  15. public MyArrayList(int length) {
  16. if(length > 0) {
  17. this.elementData = new Object[length];
  18. } else if(length == 0) {
  19. this.elementData = EMPTY_ELEMENT_DATA;
  20. } else {
  21. throw new IllegalArgumentException("集合长度不能为负数");
  22. }
  23. }
  24. public int size() {
  25. return this.size;
  26. }
  27. public boolean add(Object object) {
  28. int minCapacity; // 最小容量
  29. if(elementData == EMPTY_ELEMENT_DATA) {
  30. minCapacity = DEFAULT_LENGTH; // 初始为10
  31. } else {
  32. minCapacity = size + 1; // 需要至少 size+1 个桶
  33. }
  34. if(minCapacity > elementData.length) {
  35. // 需要进行扩容
  36. grow(minCapacity);
  37. }
  38. elementData[size++] = object;
  39. return true;
  40. }
  41. /**
  42. * 扩容
  43. * @param minCapacity 最小容量
  44. */
  45. private void grow(int minCapacity) {
  46. int oldCapacity = elementData.length;
  47. // 新容量的大小: 源码中是 oldCapacity + (oldCapacity >> 1), 其结果一致, 只不过移位速度快
  48. int newCapacity = oldCapacity + (oldCapacity / 2);
  49. if(minCapacity > newCapacity) {
  50. newCapacity = minCapacity;
  51. }
  52. elementData = Arrays.copyOf(elementData, newCapacity);
  53. }
  54. public boolean remove(Object object) {
  55. // 注意: 空和非空时的比较 要区分开
  56. if(object == null) {
  57. for(int index = 0; index < this.size; index++) {
  58. if(this.elementData[index] == null) {
  59. fastRemove(index);
  60. return true;
  61. }
  62. }
  63. } else {
  64. for(int index = 0; index < this.size; index++) {
  65. if(Objects.equals(object, elementData[index])) {
  66. fastRemove(index);
  67. return true;
  68. }
  69. }
  70. }
  71. return false;
  72. }
  73. public Object remove(int index) {
  74. if(index < 0 || index >= size) {
  75. throw new ArrayIndexOutOfBoundsException("数组越界:" + index);
  76. } else {
  77. Object oldObject = elementData[index];
  78. int numRemoved = size - index - 1;
  79. System.arraycopy(elementData, index+1, elementData, index, numRemoved);
  80. elementData[--size] = null;
  81. return oldObject;
  82. }
  83. }
  84. /**
  85. * System.arraycopy(this.elementData, index+1, this.elementData, index, numMoved);
  86. * 将指定源数组中的数组从指定位置复制到目标数组的指定位置。
  87. *
  88. * public static void arraycopy(
  89. * Object src, 要复制的数组(源数组)
  90. * int srcPos, 复制源数组的起始位置
  91. * Object dest, 目标数组
  92. * int destPos, 目标数组的下标位置
  93. * int length); 要复制的长度
  94. */
  95. private void fastRemove(int index) {
  96. int numMoved = this.size - index - 1; // 需要移动的个数
  97. if(numMoved > 0) {
  98. System.arraycopy(this.elementData, index+1, this.elementData, index, numMoved);
  99. }
  100. elementData[--size] = null; // 删除完之后, 都会前移一个, 把原来的最后一个置为空
  101. }
  102. public void print() {
  103. for(int i=0; i<size; i++) {
  104. System.out.println(elementData[i]);
  105. }
  106. }
  107. public boolean isEmpty() {
  108. return size == 0;
  109. }
  110. public boolean contains(Object object) {
  111. return indexOf(object) >= 0;
  112. }
  113. public int indexOf(Object object) {
  114. if(object == null) {
  115. for(int i=0; i<size; i++) {
  116. if(elementData[i] == null) {
  117. return i;
  118. }
  119. }
  120. } else {
  121. for(int i=0; i<size; i++) {
  122. if(object.equals(elementData[i])) {
  123. return i;
  124. }
  125. }
  126. }
  127. return -1;
  128. }
  129. public Object[] toArray() {
  130. return Arrays.copyOf(elementData, size);
  131. }
  132. public Object get(int index) {
  133. if(index < 0 || index >= size) {
  134. throw new ArrayIndexOutOfBoundsException("数组越界:" + index);
  135. }
  136. return elementData[index];
  137. }
  138. public Object set(int index, Object object) {
  139. if(index < 0 || index >= size) {
  140. throw new ArrayIndexOutOfBoundsException("数组越界:" + index);
  141. }
  142. Object oldValue = elementData[index];
  143. elementData[index] = object;
  144. return oldValue;
  145. }
  146. public void clear() {
  147. for (int i=0; i<size; i++) {
  148. elementData[i] = null;
  149. }
  150. size = 0;
  151. }
  152. }