Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions group27/276961139/src/learn/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.liam.learn.code2017;

import java.lang.reflect.Array;
import java.util.Arrays;

public class ArrayList implements List {

private int capacity = 10;
private int size = 0;

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

public ArrayList(){
this.capacity = capacity;
elementData = new Object[capacity];
}

public ArrayList(int custCapacity) {
if(custCapacity <= 0){
throw new IllegalArgumentException("Arraylist 长度不能为负数或0");
}
this.capacity = custCapacity;
elementData = new Object[capacity];
}

public void add(Object o){
if (size >= capacity){
enlargeCapacity();
}
elementData[size] = o;
size++;
}
public void add(int index, Object o){
if(index <0 || index >= size){
throw new IllegalArgumentException("数组越界");
}
if (size >= capacity){
enlargeCapacity();
}
System.arraycopy(elementData, index, elementData, index+1, size - index);
elementData[index] = o;
size++;
}

public Object get(int index){
if(index <0 || index >= size){
throw new IllegalArgumentException("数组越界");
}
return elementData[index];
}

public Object remove(int index){
Object removedObj = get(index);

int movedSize = size - (index + 1);
if (movedSize > 0) {
System.arraycopy(elementData, index+1, elementData, index, movedSize);
}
elementData[--size] = null;

return removedObj;
}

public int size(){
return size;
}

public Iterator iterator(){
return null;
}


@Override
public String toString() {
if (size < capacity){
//int needRemove = capacity - size;
Object[] toStringObj = new Object[size];
System.arraycopy(elementData, 0, toStringObj, 0, size);
return Arrays.toString(toStringObj);
}
return Arrays.toString(elementData);
}

private void enlargeCapacity(){
capacity = capacity * 2;
Object[] temp = new Object[capacity];
System.arraycopy(elementData, 0, temp, 0, size);
elementData = temp;
}



}
46 changes: 46 additions & 0 deletions group27/276961139/src/learn/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.liam.learn.code2017;

public class BinaryTreeNode {

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

public BinaryTreeNode(Object data) {
this.data = data;
}

public BinaryTreeNode() {
}

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 (left != null){
return left = new BinaryTreeNode(o);
}
if (right !=null){
return right = new BinaryTreeNode(o);
}
throw new RuntimeException("左右子树已经都有值了");
}

}
7 changes: 7 additions & 0 deletions group27/276961139/src/learn/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.liam.learn.code2017;

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

}
Loading