-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from stevenshane/master
add ArrayList and LinkedList
- Loading branch information
Showing
17 changed files
with
757 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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
65
group16/214074094/src/com/coding/basic/BinaryTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
Oops, something went wrong.