Skip to content

Commit 8872d60

Browse files
committed
。。。
1 parent 6036020 commit 8872d60

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

group17/1264835468/src/assignment/BinaryTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public BinaryTreeNode<T> insert(BinaryTreeNode<T> node) {
2727

2828
private void insert(BinaryTreeNode<T> current, BinaryTreeNode<T> node) {
2929

30-
if (current.getData().compareTo(node.getData()) >= 0) {
30+
if (current.getData().compareTo(node.getData()) > 0) {
3131
if (current.getLeft() == null)
3232
current.setLeft(node);
3333
else

group17/1264835468/src/assignment/MyArrayList.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public MyArrayList() {
1212
}
1313

1414
public MyArrayList(int initSize) {
15-
if (initSize < 0) throw new IllegalArgumentException(initSize + " < 0");
15+
if (initSize < 0) {
16+
throw new IllegalArgumentException(initSize + " < 0");
17+
}
1618
if (initSize == 0) {
1719
elementData = new Object[DEFAULT_SIZE];
1820
}
@@ -28,6 +30,9 @@ public void add(E o) {
2830
}
2931

3032
public void add(int index, E o) {
33+
if (index < 0 || index > size) {
34+
throw new IllegalArgumentException("index:" + index);
35+
}
3136
growIfNeed();
3237
System.arraycopy(elementData, index, elementData, index + 1, size - index);
3338
elementData[index] = o;
@@ -43,7 +48,6 @@ public E get(int index) {
4348
public E remove(int index) {
4449
rangeCheck(index);
4550
E target = get(index);
46-
// moveForwardFrom(index + 1);
4751
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
4852
size--;
4953
return target;
@@ -55,12 +59,13 @@ public int size() {
5559

5660
private void rangeCheck(int index) {
5761
if (index >= size) {
58-
throw new NoElementException("index:" + index);
62+
throw new NoSuchElementException("index:" + index);
5963
}
6064
}
6165

6266
private void growIfNeed() {
63-
if (size == elementData.length) grow();
67+
if (size == elementData.length)
68+
grow();
6469
}
6570

6671
private void grow() {
@@ -96,9 +101,9 @@ public String toString() {
96101

97102
}
98103

99-
class NoElementException extends RuntimeException {
104+
class NoSuchElementException extends RuntimeException {
100105

101-
public NoElementException(String string) {
106+
public NoSuchElementException(String string) {
102107
super(string);
103108
}
104109

group17/1264835468/src/assignment/MyLinkedList.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public void addLast(E o) {
3434
}
3535

3636
public void add(int index, E o) {
37-
rangeCheck(index - 1);
37+
if (index > size || index < 0) {
38+
throw new IllegalArgumentException("index:" + index);
39+
}
3840
if (index == 0) {
3941
addFirst(o);
4042
return;
@@ -64,6 +66,7 @@ public E remove(int index) {
6466
}
6567

6668
public E get(int index) {
69+
rangeCheck(index);
6770
return movePtrTo(index).data;
6871
}
6972

@@ -81,7 +84,7 @@ private Node<E> movePtrTo(int index) {
8184

8285
private void rangeCheck(int index) {
8386
if (index >= size) {
84-
throw new NoElementException("index:" + index);
87+
throw new NoSuchElementException("index:" + index);
8588
}
8689
}
8790

0 commit comments

Comments
 (0)