初版:

  1. public interface ArrayList<E> {
  2. public int size(); // 元素的数量
  3. public boolean isEmpty(); // 是否为空
  4. public boolean contains(int element); // 是否包含某个元素
  5. public void add(int element); // 添加元素到最后面
  6. public int get(int index); // 返回index位置对应的元素
  7. public int set(int index, int element); // 设置index位置的元素
  8. public void add(int index, int element); // 往index位置添加元素
  9. public int remove(int index); // 删除index位置对应的元素
  10. public int indexOf(int element); // 查看元素的位置
  11. public void clear(); // 清除所有元素
  12. }
  1. package com.test;
  2. @SuppressWarnings("unchecked")
  3. public class MyArrayList<E> implements ArrayList<E> {
  4. /**
  5. * 元素的数量
  6. */
  7. private int size;
  8. /**
  9. * 元素的原始数组,存放变动元素的数组
  10. */
  11. private int[] elements;
  12. /**
  13. * 默认容量
  14. */
  15. private static final int DEFAULT_CAPACITY = 10;
  16. /**
  17. * 元素错误值
  18. */
  19. private static final int ELEMENT_NOT_FOUND = -1;
  20. /**
  21. * 构造函数,创建一个固定大小 数组
  22. *
  23. * @param capaticy 容量
  24. */
  25. public MyArrayList(int capaticy) {
  26. // 固定数组
  27. // elements = new int[capaticy];
  28. // 修改为动态数组,即如果容量小于默认值,那么直接使用默认10大小的容量
  29. // 否则使用大于10默认值的容量
  30. // 以此来创建灵活多变的数组
  31. capaticy = (capaticy < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : capaticy;
  32. elements = new int[capaticy];
  33. }
  34. /**
  35. * 默认一个10位大小的整数型数组
  36. */
  37. public MyArrayList() {
  38. // elements = new int[10];
  39. // elements = new int[DEFAULT_CAPACITY];
  40. // 调用构造函数
  41. this(DEFAULT_CAPACITY);
  42. }
  43. /**
  44. * 元素的数量
  45. */
  46. @Override
  47. public int size() {
  48. return size;
  49. }
  50. /**
  51. * 是否为空
  52. */
  53. @Override
  54. public boolean isEmpty() {
  55. return size == 0;
  56. }
  57. /**
  58. * 是否包含某个元素
  59. */
  60. @Override
  61. public boolean contains(int element) {
  62. // 查找元素索引存在吗,不存在则false,存在为true
  63. return indexOf(element) != ELEMENT_NOT_FOUND;
  64. }
  65. /**
  66. * 添加元素到最后面
  67. */
  68. @Override
  69. public void add(int element) {
  70. // elements[size++] = element;
  71. // size++;
  72. add(this.size, element);
  73. }
  74. /**
  75. * 返回index位置对应的元素
  76. *
  77. * @param index 索引
  78. */
  79. @Override
  80. public int get(int index) {
  81. // 如果索引小于0或者索引大于元素的数量
  82. // if (index < 0 || index >= size) {
  83. // // 抛出下标越界异常,提示信息:索引值,list里存放的元素数量
  84. // throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
  85. // }
  86. rangeCheck(index);
  87. // 返回对应索引位置的元素数组
  88. return elements[index];
  89. }
  90. /**
  91. * 设置index位置的元素
  92. *
  93. * @param index 修改索引
  94. * @param element 修改元素值
  95. */
  96. @Override
  97. public int set(int index, int element) {
  98. // 如果索引小于0或者索引大于元素的数量
  99. // if (index < 0 || index >= size) {
  100. // // 抛出下标越界异常,提示信息:索引值,list里存放的元素数量
  101. // throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
  102. // }
  103. rangeCheck(index);
  104. // 拿到元素组对应的值
  105. int old = elements[index];
  106. // 将要修改元素值赋值给原元素值
  107. elements[index] = element;
  108. // 返回结果-被修改后的元素值
  109. return old;
  110. }
  111. /**
  112. * 往index位置添加元素
  113. */
  114. @Override
  115. public void add(int index, int element) {
  116. // if (index < 0 || index > size) {
  117. // // 抛出下标越界异常,提示信息:索引值,list里存放的元素数量
  118. // throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
  119. // }
  120. rangeCheckForAdd(index);
  121. ensureCapacity(size + 1);
  122. for (int i = size - 1; i >= index; i--) {
  123. elements[i + 1] = elements[i];
  124. }
  125. elements[index] = element;
  126. size++;
  127. }
  128. /**
  129. * 扩容
  130. *
  131. * @param capacity
  132. */
  133. private void ensureCapacity(int capacity) {
  134. int oldCapacity = elements.length;
  135. if (oldCapacity >= capacity) {
  136. return;
  137. }
  138. int newCapactiy = oldCapacity + (oldCapacity >> 2);
  139. int[] newElements = new int[newCapactiy];
  140. for (int i = 0; i < elements.length; i++) {
  141. newElements[i] = elements[i];
  142. }
  143. elements = newElements;
  144. }
  145. /**
  146. * 删除index位置对应的元素
  147. */
  148. @Override
  149. public int remove(int index) {
  150. // 如果索引小于0或者索引大于元素的数量
  151. // if (index < 0 || index >= size) {
  152. // // 抛出下标越界异常,提示信息:索引值,list里存放的元素数量
  153. // throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
  154. // }
  155. rangeCheck(index);
  156. //
  157. int old = elements[index];
  158. for (int i = index + 1; i <= size - 1; i++) {
  159. elements[i - 1] = elements[i];
  160. size--;
  161. }
  162. return old;
  163. }
  164. /**
  165. * 查看元素的位置
  166. */
  167. @Override
  168. public int indexOf(int element) {
  169. // 遍历元素数组里面的所有值
  170. for (int i = 0; i < size; i++) {
  171. // 如果该元素值和查询元素值相等
  172. if (elements[i] == element) {
  173. // 返回该元素索引
  174. return i;
  175. }
  176. }
  177. // 否则,返回 -1
  178. return ELEMENT_NOT_FOUND;
  179. }
  180. /**
  181. * 清除所有元素
  182. */
  183. @Override
  184. public void clear() {
  185. // 不清空数组,保留数组的内存,方便下次添加使用,避免重新创建数组,减少栈堆消耗
  186. size = 0;
  187. }
  188. @Override
  189. public String toString() {
  190. // 打印格式:[100,50,101]
  191. StringBuilder builder = new StringBuilder();
  192. builder.append("size = ").append(size).append(", body = [");
  193. for (int i = 0; i < size; i++) {
  194. if (i != 0) {
  195. builder.append(", ");
  196. }
  197. builder.append(elements[i]);
  198. }
  199. builder.append("]");
  200. return builder.toString();
  201. }
  202. private void outOfBounds(int index) {
  203. throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
  204. }
  205. private void rangeCheck(int index) {
  206. if (index < 0 || index >= size) {
  207. outOfBounds(index);
  208. }
  209. }
  210. private void rangeCheckForAdd(int index) {
  211. if (index < 0 || index > size) {
  212. outOfBounds(index);
  213. }
  214. }
  215. }

最终版:

  1. public interface ArrayList2<E> {
  2. public int size(); // 元素的数量
  3. public boolean isEmpty(); // 是否为空
  4. public boolean contains(E element); // 是否包含某个元素
  5. public void add(E element); // 添加元素到最后面
  6. public E get(int index); // 返回index位置对应的元素
  7. public E set(int index, E element); // 设置index位置的元素
  8. public void add(int index, E element); // 往index位置添加元素
  9. public E remove(int index); // 删除index位置对应的元素
  10. public int indexOf(E element); // 查看元素的位置
  11. public void clear(); // 清除所有元素
  12. }
  1. @SuppressWarnings("unchecked")
  2. public class MyArrayList2<E> implements ArrayList2<E> {
  3. /**
  4. * 元素的数量
  5. */
  6. private int size;
  7. /**
  8. * 元素的原始数组,存放变动元素的数组
  9. */
  10. private E[] elements;
  11. /**
  12. * 默认容量
  13. */
  14. private static final int DEFAULT_CAPACITY = 10;
  15. /**
  16. * 元素错误值
  17. */
  18. private static final int ELEMENT_NOT_FOUND = -1;
  19. /**
  20. * 构造函数,创建一个固定大小 数组
  21. *
  22. * @param capaticy 容量
  23. */
  24. public MyArrayList2(int capaticy) {
  25. // 固定数组
  26. // elements = new int[capaticy];
  27. // 修改为动态数组,即如果容量小于默认值,那么直接使用默认10大小的容量
  28. // 否则使用大于10默认值的容量
  29. // 以此来创建灵活多变的数组
  30. capaticy = (capaticy < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : capaticy;
  31. elements = (E[]) new Object[capaticy];
  32. }
  33. /**
  34. * 默认一个10位大小的整数型数组
  35. */
  36. public MyArrayList2() {
  37. // elements = new int[10];
  38. // elements = new int[DEFAULT_CAPACITY];
  39. // 调用构造函数
  40. this(DEFAULT_CAPACITY);
  41. }
  42. /**
  43. * 元素的数量
  44. */
  45. @Override
  46. public int size() {
  47. return size;
  48. }
  49. /**
  50. * 是否为空
  51. */
  52. @Override
  53. public boolean isEmpty() {
  54. return size == 0;
  55. }
  56. /**
  57. * 是否包含某个元素
  58. */
  59. @Override
  60. public boolean contains(E element) {
  61. // 查找元素索引存在吗,不存在则false,存在为true
  62. return indexOf(element) != ELEMENT_NOT_FOUND;
  63. }
  64. /**
  65. * 添加元素到最后面
  66. */
  67. @Override
  68. public void add(E element) {
  69. // elements[size++] = element;
  70. // size++;
  71. add(this.size, element);
  72. }
  73. /**
  74. * 返回index位置对应的元素
  75. *
  76. * @param index 索引
  77. */
  78. @Override
  79. public E get(int index) {
  80. // 如果索引小于0或者索引大于元素的数量
  81. // if (index < 0 || index >= size) {
  82. // // 抛出下标越界异常,提示信息:索引值,list里存放的元素数量
  83. // throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
  84. // }
  85. rangeCheck(index);
  86. // 返回对应索引位置的元素数组
  87. return elements[index];
  88. }
  89. /**
  90. * 设置index位置的元素
  91. *
  92. * @param index 修改索引
  93. * @param element 修改元素值
  94. */
  95. @Override
  96. public E set(int index, E element) {
  97. // 如果索引小于0或者索引大于元素的数量
  98. // if (index < 0 || index >= size) {
  99. // // 抛出下标越界异常,提示信息:索引值,list里存放的元素数量
  100. // throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
  101. // }
  102. rangeCheck(index);
  103. // 拿到元素组对应的值
  104. E old = elements[index];
  105. // 将要修改元素值赋值给原元素值
  106. elements[index] = element;
  107. // 返回结果-被修改后的元素值
  108. return old;
  109. }
  110. /**
  111. * 往index位置添加元素
  112. */
  113. @Override
  114. public void add(int index, E element) {
  115. // if (index < 0 || index > size) {
  116. // // 抛出下标越界异常,提示信息:索引值,list里存放的元素数量
  117. // throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
  118. // }
  119. rangeCheckForAdd(index);
  120. ensureCapacity(size + 1);
  121. for (int i = size - 1; i >= index; i--) {
  122. elements[i + 1] = elements[i];
  123. }
  124. elements[index] = element;
  125. size++;
  126. }
  127. /**
  128. * 扩容
  129. *
  130. * @param capacity
  131. */
  132. private void ensureCapacity(int capacity) {
  133. int oldCapacity = elements.length;
  134. if (oldCapacity >= capacity) {
  135. return;
  136. }
  137. int newCapactiy = oldCapacity + (oldCapacity >> 1);
  138. E[] newElements = (E[]) new Object[newCapactiy];
  139. for (int i = 0; i < size; i++) {
  140. newElements[i] = elements[i];
  141. }
  142. elements = newElements;
  143. }
  144. /**
  145. * 删除index位置对应的元素
  146. */
  147. @Override
  148. public E remove(int index) {
  149. // 如果索引小于0或者索引大于元素的数量
  150. // if (index < 0 || index >= size) {
  151. // // 抛出下标越界异常,提示信息:索引值,list里存放的元素数量
  152. // throw new IndexOutOfBoundsException("index:" + index + ",size:" + size);
  153. // }
  154. rangeCheck(index);
  155. // 获取该索引下的元素值
  156. E old = elements[index];
  157. // 从索引下一个位置处开始,到数组的数量遍历
  158. for (int i = index + 1; i <= size - 1; i++) {
  159. elements[i - 1] = elements[i];
  160. size--;
  161. }
  162. // 将size-1处的元素置为null,让垃圾回收器进行自动回收
  163. elements[--size] = null;
  164. return old;
  165. }
  166. /**
  167. * 查看元素的位置
  168. */
  169. @Override
  170. public int indexOf(E element) {
  171. if (element == null) { // 1
  172. for (int i = 0; i < size; i++) {
  173. if (elements[i] == null)
  174. return i;
  175. }
  176. } else {
  177. // 遍历元素数组里面的所有值
  178. for (int i = 0; i < size; i++) {
  179. // 如果该元素值和查询元素值相等
  180. if (elements[i] == element) {
  181. // 返回该元素索引
  182. return i;
  183. }
  184. }
  185. }
  186. // 否则,返回 -1
  187. return ELEMENT_NOT_FOUND;
  188. }
  189. /**
  190. * 清除所有元素
  191. */
  192. @Override
  193. public void clear() {
  194. for (int i = 0; i < size; i++) {
  195. elements[i] = null;
  196. }
  197. // 不清空数组,保留数组的内存,方便下次添加使用,避免重新创建数组,减少栈堆消耗
  198. size = 0;
  199. }
  200. @Override
  201. public String toString() {
  202. // 打印格式:[100,50,101]
  203. StringBuilder builder = new StringBuilder();
  204. builder.append("size = ").append(size).append(", body = [");
  205. for (int i = 0; i < size; i++) {
  206. if (i != 0) {
  207. builder.append(", ");
  208. }
  209. builder.append(elements[i]);
  210. }
  211. builder.append("]");
  212. return builder.toString();
  213. }
  214. private void outOfBounds(int index) {
  215. throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
  216. }
  217. private void rangeCheck(int index) {
  218. if (index < 0 || index >= size) {
  219. outOfBounds(index);
  220. }
  221. }
  222. private void rangeCheckForAdd(int index) {
  223. if (index < 0 || index > size) {
  224. outOfBounds(index);
  225. }
  226. }
  227. }

测试:

  1. public class Main {
  2. public static void main(String[] args) {
  3. ArrayList<Object> list = new MyArrayList<>();
  4. // 添加元素11
  5. list.add(11);
  6. list.add(100);
  7. list.add(11);
  8. // 将22添加到下标0的元素
  9. // list.add(0,22);
  10. System.out.println(list);
  11. // 删除下标为1的元素
  12. list.remove(1);
  13. System.out.println(list);
  14. }
  15. }

结果:

  1. size = 3, body = [11, 100, 11]
  2. size = 2, body = [11, 11]