Skip to content

Commit

Permalink
Merge pull request #8 from likeleaf/master
Browse files Browse the repository at this point in the history
first commit
  • Loading branch information
DonaldY authored Mar 12, 2017
2 parents 2e31cf0 + 104bd9b commit 1a25f72
Show file tree
Hide file tree
Showing 9 changed files with 728 additions and 0 deletions.
5 changes: 5 additions & 0 deletions group24/893022254/task1-list/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.settings/
/bin/
.classpath
.project

106 changes: 106 additions & 0 deletions group24/893022254/task1-list/src/com/oneflyingleaf/util/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.oneflyingleaf.util;

import java.util.Arrays;

public class ArrayList implements List {

//非线程安全
private int size = 0;

private Object[] elementData = new Object[100];

public void add(Object o){

checkLength(1);

elementData[ size ++] = o;
}


public void add(int index, Object o){
checkBound(index);

if(index != size){
checkLength(1);
}

int temp = index;

//index == size 不移动,直接放
while(index < size){
elementData[ ++ index ] = elementData[index];
}

size ++ ;
elementData[temp] = o;
}

public Object get(int index){
checkBound(index);

return elementData [index];
}

public Object remove(int index){
checkBound(index);

Object ret = elementData[index];
while(index < size){
elementData[index] = elementData[ ++ index];
}

-- size ;

return ret;
}

public int size(){
return size;
}

public Iterator iterator(){
return new ArrayListIterator();
}

/**
* 判断扩容
* @param length
*/
private void checkLength(int length){
if( (size + length) > elementData.length){
//默认扩大一倍
Arrays.copyOf(elementData, elementData.length << 1);
}
}

/**
* 校验是否超出
* @param index
*/
private void checkBound(int index){
if(index < 0 || index > size){
throw new IndexOutOfBoundsException("size : " + size +" , index : " + index);
}
}


private class ArrayListIterator implements Iterator{

private int count = -1;

private ArrayListIterator(){
}

@Override
public boolean hasNext() {
return (count+1) < size?true:false;
}

@Override
public Object next() {
count ++ ;
return elementData[count];
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.oneflyingleaf.util;

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){
if(! (o instanceof Comparable)){
throw new RuntimeException("未实现Comparable接口");
}
Comparable<Object> temp = (Comparable<Object>)o;

if(temp.compareTo(data) > 0){
if(right == null){
right= new BinaryTreeNode();
right.data = o;
}else{
right.insert(o);
}
}

if(temp.compareTo(data) <= 0){
if(left == null){

left = new BinaryTreeNode();
left.data = o;
}else{
left.insert(o);
}
}


return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.oneflyingleaf.util;

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

}
Loading

0 comments on commit 1a25f72

Please sign in to comment.