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
4 changes: 4 additions & 0 deletions group24/494800949/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/*
*.iml
build/*
.gradle/*
29 changes: 29 additions & 0 deletions group24/494800949/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
group 'com.hpe'
version '1.0'

buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'java'
apply plugin: 'idea'

sourceCompatibility = 1.8
sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies{
compile group: 'junit', name: 'junit', version: '4.11'
}
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked"
options.encoding = "UTF-8"
}
}

defaultTasks "build"
102 changes: 102 additions & 0 deletions group24/494800949/src/main/java/com/coding/weak1/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.coding.weak1;

public class ArrayList implements List {

private int size = 0;

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

public void add(Object o){
ensureCapcity(size + 1);
elementData[size++] = o;
}

/**
* 当数组长度不够时进行扩容
*/
private void ensureCapcity(int requireSize){
int oldCapacity = elementData.length;
int newCapacity ;
if(oldCapacity < requireSize){
newCapacity = oldCapacity + (oldCapacity >> 1);

if(newCapacity < 0){
newCapacity = Integer.MAX_VALUE;
}

if(newCapacity > Integer.MAX_VALUE - 8){
newCapacity = Integer.MAX_VALUE;
}

Object[] elementDataNew = new Object[newCapacity];
System.arraycopy(elementData, 0, elementDataNew, 0, oldCapacity);
elementData = elementDataNew;
}
}


public void add(int index, Object o){
indexCheckForAdd(index);
ensureCapcity(index + 1);
Object[] newElementData = new Object[elementData.length];
if(index > 0)
System.arraycopy(elementData, 0, newElementData, 0, index-1);
System.arraycopy(elementData, index, newElementData, index+1, size-index);
elementData = newElementData;
elementData[index] = o;
size++;
}


private void indexCheckForAdd(int index) {
if(index < 0 || index >= size-1){
throw new IndexOutOfBoundsException("max index is: "+(size-1));
}
}

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

private void indexCheck(int index) {
if(index < 0 || index > size-1){
throw new IndexOutOfBoundsException("max index is: "+(size-1));
}
}

public Object remove(int index){
indexCheck(index);
Object obj = elementData[index];
System.arraycopy(elementData, index+1, elementData, index, size-index-1);
elementData[size--] = null;
return obj;
}

public int size(){
return size;
}

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

private class ListIter implements Iterator{

private int cursor;

@Override
public boolean hasNext() {
return cursor < size;
}

@Override
public Object next() {
int index = cursor;
if(index >= size)
throw new IndexOutOfBoundsException();
cursor++;
return elementData[index];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.coding.weak1;

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;
}

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

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

}
Loading