MyList接口

  1. package collection;
  2. /**
  3. * 自定义接口Mylist,模仿List
  4. * @param <E>
  5. */
  6. public interface MyList<E> {
  7. int size();
  8. boolean isEmpty();
  9. void add(E obj);
  10. void add(int index, E obj);
  11. void set(int index, E obj);
  12. boolean contains(E obj);
  13. Object[] toArray();
  14. boolean remove(E obj);
  15. void clear();
  16. }

MyArrayList类

  1. package collection;
  2. import java.util.Arrays;
  3. public class MyArrayList<E> implements MyList<E>{
  4. private static final int DEFAULT_CAPACITY = 10;
  5. // size是列表元素个数
  6. private int size;
  7. private Object[] elementData;
  8. public MyArrayList() {
  9. this(DEFAULT_CAPACITY);
  10. }
  11. public MyArrayList(int initialCapacity) {
  12. if (initialCapacity < 0) {
  13. try {
  14. throw new Exception("大于零");
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }else{
  19. this.elementData = new Object[initialCapacity];
  20. }
  21. }
  22. public static void main(String[] args) {
  23. MyArrayList<String> myList1 = new MyArrayList<>();
  24. System.out.println(myList1);
  25. myList1.add("aa");
  26. myList1.add("bb");
  27. myList1.add("cc");
  28. myList1.add("dd");
  29. myList1.add("ee");
  30. myList1.add("ff");
  31. myList1.add(2,"fuck");
  32. System.out.println(myList1.set(0,"start"));
  33. System.out.println(myList1.get(4));
  34. System.out.println(myList1.contains("ee"));
  35. myList1.remove(2);
  36. myList1.remove("start");
  37. System.out.println(myList1);
  38. // myList1.clear();
  39. }
  40. @Override
  41. public int size() {
  42. return size;
  43. }
  44. @Override
  45. public boolean isEmpty() {
  46. return size == 0;
  47. }
  48. @Override
  49. public void add(E obj) {
  50. ensureCapacity();
  51. elementData[size] = obj;
  52. size++;
  53. }
  54. @Override
  55. public void add(int index, E obj) {
  56. rangeCheck(index);
  57. ensureCapacity();
  58. System.arraycopy(elementData,index,elementData,index+1,size-index);
  59. elementData[index] = obj;
  60. size++;
  61. }
  62. private void rangeCheck(int index){
  63. if (index<0 || index>=size) {
  64. try {
  65. throw new Exception("index参数不合法");
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }
  71. private void ensureCapacity(){
  72. if(size == elementData.length){
  73. Object[] newElementsData = new Object[2*size+1];
  74. System.arraycopy(elementData,0,newElementsData,0,size);
  75. elementData = newElementsData;
  76. }
  77. }
  78. public void remove(int index){
  79. rangeCheck(index);
  80. int numMoved = size - (index + 1);
  81. if (numMoved > 0){
  82. System.arraycopy(elementData,index+1,elementData,index,numMoved);
  83. }
  84. size--;
  85. elementData[size] = null;
  86. }
  87. @Override
  88. public E set(int index, E obj) {
  89. rangeCheck(index);
  90. Object oldValue = elementData[index];
  91. elementData[index] = obj;
  92. return (E) oldValue;
  93. }
  94. public E get(int index){
  95. rangeCheck(index);
  96. return (E) elementData[index];
  97. }
  98. @Override
  99. public boolean contains(E obj) {
  100. for (Object el : elementData) {
  101. if (el.equals(obj)) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. @Override
  108. public Object[] toArray() {
  109. return elementData;
  110. }
  111. @Override
  112. public boolean remove(E obj) {
  113. for (int i = 0; i < size; i++) {
  114. if (elementData[i].equals(obj)) {
  115. remove(i);
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. @Override
  122. public void clear() {
  123. for (int i = 0; i < size; i++) {
  124. elementData[i] = null;
  125. }
  126. size = 0;
  127. }
  128. @Override
  129. public String toString() {
  130. StringBuilder sb = new StringBuilder();
  131. sb.append("[");
  132. for (int i = 0; i < size; i++) {
  133. sb.append(elementData[i]);
  134. if (i !=size-1) {
  135. sb.append(",");
  136. }
  137. }
  138. sb.append("]");
  139. return sb.toString();
  140. }
  141. }

MyLinkedList类

  • 结构分析 每个节点存储了数据内容、前一个和后一个节点的内容
  • 与arraylist相比,不便于查找,但是便于插入与删除
    1. class Node {
    2. Node prevoius; // 前一个节点
    3. Object element; // 本节点保存的数据
    4. Node next; // 后一个节点
    5. }
    ```java package collection;

public class MyLinkedList implements MyList{ private Node first; private Node last; private int size;

  1. public static void main(String[] args) {
  2. MyLinkedList<String> mll = new MyLinkedList<>();
  3. mll.add("aa");
  4. mll.add("bb");
  5. mll.add("cc");
  6. mll.add("dd");
  7. mll.add("ee");
  8. mll.add("ff");
  9. Node nn = mll.getNode(5);
  10. mll.add(5,"fuck");
  11. System.out.println(mll);
  12. }
  13. private void rangeCheck(int index){
  14. if (index<0 || index>=size) {
  15. try {
  16. throw new Exception("index参数不合法");
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }
  22. private Node getNode(int index){
  23. rangeCheck(index);
  24. Node n = null;
  25. if (first != null) {
  26. if ((size >> 1) > index) {
  27. // 从前向后
  28. n = first;
  29. for (int i = 0; i < index; i++) {
  30. n = n.getNext();
  31. }
  32. } else {
  33. // 从后向前
  34. n = last;
  35. for (int i = size-1; i > index; i--) {
  36. n = n.getPrevious();
  37. }
  38. }
  39. }
  40. return n;
  41. }
  42. @Override
  43. public int size() {
  44. return size;
  45. }
  46. @Override
  47. public boolean isEmpty() {
  48. return size == 0;
  49. }
  50. @Override
  51. public void add(E obj) {
  52. Node<E> n = new Node<>();
  53. if (size == 0) {
  54. n.setPrevious(null);
  55. n.setNext(null);
  56. n.setElement(obj);
  57. first = n;
  58. last = n;
  59. }else{
  60. n.setPrevious(last);
  61. n.setNext(null);
  62. n.setElement(obj);
  63. last.setNext(n);
  64. last = n;
  65. }
  66. size++;
  67. }
  68. @Override
  69. public void add(int index, E obj) {
  70. Node target = this.getNode(index);
  71. Node previous = target.getPrevious();
  72. Node<E> n = new Node<>();
  73. n.setElement(obj);
  74. n.setPrevious(previous);
  75. n.setNext(target);
  76. if (previous != null){
  77. previous.setNext(n);
  78. }
  79. target.setPrevious(n);
  80. // 处理一下链表
  81. if (index == 0){
  82. first = n;
  83. }
  84. if (index == size() -1){
  85. last = n.getNext();
  86. }
  87. size++;
  88. }
  89. @Override
  90. public Object set(int index, Object obj) {
  91. return null;
  92. }
  93. @Override
  94. public E get(int index) {
  95. return (E) getNode(index).getElement();
  96. }
  97. @Override
  98. public boolean contains(Object obj) {
  99. return false;
  100. }
  101. @Override
  102. public Object[] toArray() {
  103. return new Object[0];
  104. }
  105. @Override
  106. public boolean remove(Object obj) {
  107. return false;
  108. }
  109. @Override
  110. public void remove(int index) {
  111. }
  112. @Override
  113. public void clear() {
  114. }

}

class Node { Node previous; E element; Node next;

  1. public Node() {}
  2. public Node getPrevious() {
  3. return previous;
  4. }
  5. public void setPrevious(Node previous) {
  6. this.previous = previous;
  7. }
  8. public Object getElement() {
  9. return element;
  10. }
  11. public void setElement(E element) {
  12. this.element = element;
  13. }
  14. public Node getNext() {
  15. return next;
  16. }
  17. public void setNext(Node next) {
  18. this.next = next;
  19. }

}

```