Skip to content

Commit

Permalink
Merge pull request #13 from stevenshane/master
Browse files Browse the repository at this point in the history
add ArrayList and LinkedList
  • Loading branch information
gaodekui authored Feb 26, 2017
2 parents 38e9252 + 416c93c commit a478b9f
Show file tree
Hide file tree
Showing 17 changed files with 757 additions and 137 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

*.iml
*.idea
*.DS_Store

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
Expand Down
145 changes: 125 additions & 20 deletions group16/214074094/src/com/coding/basic/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package com.coding.basic;
package coding.basic;


import java.util.Arrays;
import java.util.NoSuchElementException;

/**
* @Author shane
* @Time 2017/2/25 13:06
* @Email [email protected]
* @Desc OwnArrayList
*/
public class ArrayList implements List {

private int size = 0;
Expand All @@ -17,44 +27,139 @@ public ArrayList() {
this.elementData = EMPTY_ELEMENTDATA;
}

public static void main(String[] args) {
ArrayList list = new ArrayList();
System.out.println(list.elementData == list.EMPTY_ELEMENTDATA);
}

@Override
public void add(Object o) {
if (elementData == EMPTY_ELEMENTDATA) {
elementData = new Object[DEFAULT_CAPACITY];
}else if(size < elementData.length){
size++;
}else{
elementData = Arrays.copyOf(elementData, DEFAULT_CAPACITY);
elementData[0] = o;
} else if (size < elementData.length) {
elementData[size] = o;
} else {
_grow();
elementData[size] = o;
}
elementData[size] = o;
}

private void _grow() {

size++;
_analyze();
}

@Override
public void add(int index, Object o) {

if (index < 0) {
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
}
if (elementData == EMPTY_ELEMENTDATA) {
if (index != 0) {
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
} else {
elementData = new Object[DEFAULT_CAPACITY];
elementData[0] = o;
}
} else if (index > size) {
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
} else if (index == size) {
_grow();
elementData[size] = o;
size++;
} else {
if (elementData.length == size) {
_grow();
}
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = o;
size++;
}
_analyze();
}

@Override
public Object get(int index) {
return null;
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
}
return elementData[index];
}

@Override
public Object remove(int index) {
return null;

if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
}
Object oldValue = elementData[index];
//需要复制的长度
int needMoveLength = size - index - 1;
//如果该长度小于0, 说明只有一个元素, 直接置空即可
if (needMoveLength > 0) {
System.arraycopy(elementData, index + 1, elementData, index, needMoveLength);
}
elementData[--size] = null;
_analyze();
return oldValue;
}

@Override
public int size() {
return -1;
return size;
}

public Iterator iterator() {
return null;
return new ArrayListItrator();
}

/**
* @Author: shane
* @Time: 2017/2/25 20:18
* @Email: [email protected]
* @param:
* @Return:
* @Throw:
* @Desc: 返回真实长度的数组数据
*/
private void _analyze() {
if (size < elementData.length) {
elementData = Arrays.copyOf(elementData, size);
}
}

/**
* @Author: shane
* @Time: 2017/2/25 20:19
* @Email: [email protected]
* @param:
* @Return:
* @Throw:
* @Desc: 将数组的长度扩容至2倍
*/
private void _grow() {
elementData = Arrays.copyOf(elementData, elementData.length << 1);
}

@Override
public String toString() {
return Arrays.toString(elementData);
}

public boolean isEmpty() {
return size == 0;
}

private class ArrayListItrator implements Iterator {

private int position = 0;

@Override
public boolean hasNext() {
return position != size;
}

@Override
public Object next() {
int i = position;
if (i >= size) {
throw new NoSuchElementException();
}
position = i + 1;
return elementData[i];
}
}
}
65 changes: 36 additions & 29 deletions group16/214074094/src/com/coding/basic/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
package com.coding.basic;
package coding.basic;

public class BinaryTreeNode {

private Object data;
private BinaryTreeNode left;
private BinaryTreeNode right;

public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
this.right = right;
}

public BinaryTreeNode insert(Object o){
return null;
}


private Object data;

private BinaryTreeNode left;

private BinaryTreeNode right;

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

public BinaryTreeNode getLeft() {
return left;
}

public void setLeft(BinaryTreeNode left) {
this.left = left;
}

public BinaryTreeNode getRight() {
return right;
}

public void setRight(BinaryTreeNode right) {
this.right = right;
}

public BinaryTreeNode insert(Object o) {
return null;
}

}
8 changes: 5 additions & 3 deletions group16/214074094/src/com/coding/basic/Iterator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.coding.basic;
package coding.basic;

public interface Iterator {
public boolean hasNext();
public Object next();

boolean hasNext();

Object next();

}
Loading

0 comments on commit a478b9f

Please sign in to comment.