diff --git a/group19/398129523/ArrayList b/group19/398129523/ArrayList new file mode 100644 index 0000000000..4557672f10 --- /dev/null +++ b/group19/398129523/ArrayList @@ -0,0 +1,88 @@ +package com.coding.work; + +import java.util.Arrays; +import java.util.Iterator; + + +public class ArrayList { + private Object[] elementData; + private int size; + + + public ArrayList(int init) { + // TODO Auto-generated constructor stub + if (init <= 0) { + throw new IllegalArgumentException(); + } + this.elementData = new Object[init]; + } + + public ArrayList(){ + this(10); //调用重载的构造函数 + } + + public void changeCapacity(int curCapacity) { + int oldCapacity = elementData.length; + if(curCapacity > oldCapacity){ + Object[] oldData = elementData; + int newCapacity = oldCapacity + oldCapacity >> 1; //右移一位除以2,相当于扩 扩大到1.5倍 + if (newCapacity elementData.length - 1) { + changeCapacity(index + 1); + } + elementData[index] = e; + size++; + return true; + } + + @SuppressWarnings("unchecked") + public E get(int index) { + if (index <= size - 1) { + return (E) elementData[index]; + + }else { + return null; + } + } + + @SuppressWarnings("unchecked") + public E remove(int index) { + if (index >= 0 && index <= size - 1) { + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return (E) elementData[index]; + } + else { + return null; + } + } + + + + public int size() { + return size;//全局变量 + } + + //不会实现 + public Iterator iterator() { + return null; + + } + +} diff --git a/group19/398129523/LinkedList b/group19/398129523/LinkedList new file mode 100644 index 0000000000..8cf3803e2b --- /dev/null +++ b/group19/398129523/LinkedList @@ -0,0 +1,46 @@ +package com.coding.work; + +public class LinkedList{ + private int size; + + private static class Node { + Object data; + Node next; + } + private Node head = new Node(); + private Node lastNode; + + public LinkedList(){ + head.next = null; + head.data = null; + lastNode = head; + } + + public void add(Object o) { + Node curnode = new Node(); + curnode.data = o; + curnode.next = null; + if (head.next == null) { + head.next = curnode; + lastNode = curnode; + size++; + }else { + lastNode.next = curnode; + size++; + } + + + } + public void add(int index , Object o) { + if (index > size - 1) { + for(int i = size - 1; i <= index; i++){ + Node curnode = new Node(); + lastNode.next = curnode; + lastNode = curnode; + } + lastNode.data = o; + } + + } + +}