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
48 changes: 16 additions & 32 deletions group24/1525619747/homework_20170312/src/com/basic/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,82 +9,66 @@ public class ArrayList implements List

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

public void add(Object o)
{
if (size > MAXNSIZE)
{
public void add(Object o) {
if (size > MAXNSIZE) {
String errorInfo = "Out of max size" + MAXNSIZE;
throw new ArrayIndexOutOfBoundsException(errorInfo);
}
elementData[size++] = o;
}

public void add(int index, Object o)
{
if (index >= size && size > 0)
{
public void add(int index, Object o) {
if (index >= size && size > 0) {
String errorInfo = "Index to add: " + index
+ " is out of current size: " + size;
throw new ArrayIndexOutOfBoundsException(errorInfo);
}
for (int i = size; i > index; i--)
{
for (int i = size; i > index; i--) {
elementData[i] = elementData[i - 1];
}
elementData[index] = o;
++size;
}

public Object get(int index)
{
if (index < 0 || index >= size)
{
public Object get(int index) {
if (index < 0 || index >= size) {
String errorInfo = "Index to get: " + index
+ " is invalid, current range: 0 - " + (size - 1);
throw new ArrayIndexOutOfBoundsException(errorInfo);
}
return elementData[index];
}

public Object remove(int index)
{
if (index < 0 || index >= size)
{
public Object remove(int index) {
if (index < 0 || index >= size) {
String errorInfo = "Index to remove: " + index
+ " is invalid, current range: 0 - " + (size - 1);
throw new ArrayIndexOutOfBoundsException(errorInfo);
}

Object o = elementData[index];
for (int i = index; i < size - 1; i++)
{
for (int i = index; i < size - 1; i++) {
elementData[i] = elementData[i + 1];
}
elementData[size--] = null;
return o;
}

public int size()
{
public int size() {
return size;
}

public Iterator iterator()
{
return new Iterator()
{
public Iterator iterator() {
return new Iterator() {

private int index = 0;

public boolean hasNext()
{
public boolean hasNext() {
return (index < size);
}

public Object next()
{
if (hasNext())
{
public Object next() {
if (hasNext()) {
return elementData[index++];
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,57 @@ public class BinaryTreeNode
private BinaryTreeNode left;
private BinaryTreeNode right;

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

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

public Object getData()
{
public Object getData() {
return data;
}

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

public BinaryTreeNode getLeft()
{
public BinaryTreeNode getLeft() {
return left;
}

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

public BinaryTreeNode getRight()
{
public BinaryTreeNode getRight() {
return right;
}

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

/*
* 排序二叉树的插入
*/
public BinaryTreeNode insert(Object o)
{
if (((Integer) data) > ((Integer) o))
{
if (left == null)
{
public BinaryTreeNode insert(Object o) {
if (((Integer) data) > ((Integer) o)) {
if (left == null) {
setLeft(new BinaryTreeNode(o));
}
else
{
} else {
left.insert(o);
}
}
else
{
if (right == null)
{
} else {
if (right == null) {
setRight(new BinaryTreeNode(o));
}
else
{
} else {
right.insert(o);
}
}
Expand All @@ -85,17 +67,14 @@ public BinaryTreeNode insert(Object o)
/*
* 前序遍历
*/
public void preOrderInterator()
{
if (left != null)
{
public void preOrderInterator() {
if (left != null) {
left.preOrderInterator();
}

System.out.print(data.toString() + " ");

if (right != null)
{
if (right != null) {
right.preOrderInterator();
}
}
Expand Down
Loading