Skip to content

Commit

Permalink
Merge pull request #13 from CottonChou/master
Browse files Browse the repository at this point in the history
398129523
  • Loading branch information
zeyuanpinghe authored Feb 27, 2017
2 parents 297fd96 + c223e3c commit c33af41
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
88 changes: 88 additions & 0 deletions group19/398129523/ArrayList
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.coding.work;

import java.util.Arrays;
import java.util.Iterator;


public class ArrayList<E> {
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 <curCapacity ) {
newCapacity = curCapacity;
}
elementData = Arrays.copyOf(elementData, newCapacity);//Arrys.copyOf返回一个新的数组
}
}

public boolean add(E e) {
changeCapacity(size + 1);
elementData[size++] = e;
return true;
}

public boolean add(int index, E e) {
if (index < 0) {
throw new IllegalArgumentException();
}
if (index > 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;

}

}
46 changes: 46 additions & 0 deletions group19/398129523/LinkedList
Original file line number Diff line number Diff line change
@@ -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;
}

}

}

0 comments on commit c33af41

Please sign in to comment.