Skip to content

Commit

Permalink
Merge pull request #12 from zhaog/master
Browse files Browse the repository at this point in the history
425044891的第一次作业 上海平常心
  • Loading branch information
wizardzhang2017 authored Mar 12, 2017
2 parents 1c42683 + e3f10e9 commit c683db6
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 0 deletions.
97 changes: 97 additions & 0 deletions group27/425044891/src/com/coding/basic/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.coding.basic;

/**
* ArrayList
*
* @author Guang
* @date 2017年3月12日 下午1:55:47
*/
public class ArrayList implements List {

/**
* 数组中元素个数
*/
private int size = 0;

/**
* 数组
*/
private Object[] elementData = new Object[100];

public void add(Object o) {
if (size >= 100 || size < 0) {
throw new IndexOutOfBoundsException("越界.");
}
if (null == o) {
return;
}
elementData[size] = o;
size++;
}

public void add(int index, Object o) {
checkRangeForAdd(index);
if (null == o) {
return;
}
for (int i = size; i > index; i--) {
elementData[i] = elementData[i - 1];
}
elementData[index] = o;
size++;
}

private void checkRangeForAdd(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("越界.");
}
}

private void checkRangeForGet(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("越界.");
}
}

public Object get(int index) {
checkRangeForGet(index);
return elementData[index];
}

/**
* 移除第index位置的元素,后续元素前移
*/
public Object remove(int index) {
checkRangeForGet(index);
int i = index;
Object o = elementData[index];
for (; i < size - 1; i++) {
elementData[i] = elementData[i + 1];
}
elementData[i] = null;
size--;
return o;
}

public int size() {
return size;
}

public Iterator iterator() {
return new Iterator() {
int index = 0;
@Override
public Object next() {
checkRangeForGet(index);
Object o = elementData[index];
index++;
return o;
}

@Override
public boolean hasNext() {
return index < size && index >= 0;
}
};
}
}
41 changes: 41 additions & 0 deletions group27/425044891/src/com/coding/basic/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.coding.basic;

public class BinaryTreeNode<T extends Comparable<T>> {

private T data;
private BinaryTreeNode<T> left;
private BinaryTreeNode<T> right;

public T getData() {
return data;
}

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

public BinaryTreeNode<T> getLeft() {
return left;
}

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

public BinaryTreeNode<T> getRight() {
return right;
}

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

public BinaryTreeNode<T> insert(T o) {
if (o.compareTo(data) <= 0) {
return getLeft().insert(o);
} else {
return getRight().insert(o);
}
}

}
6 changes: 6 additions & 0 deletions group27/425044891/src/com/coding/basic/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.coding.basic;
public interface Iterator {
public boolean hasNext();
public Object next();

}
Loading

0 comments on commit c683db6

Please sign in to comment.