Skip to content

Commit b8e1698

Browse files
committed
update javalang
1 parent 5bf1fe9 commit b8e1698

File tree

2 files changed

+147
-73
lines changed

2 files changed

+147
-73
lines changed

java/arraylist/MyArrayList.java

Lines changed: 71 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,79 @@
1-
package myarraylist
1+
package arraylist;
22

33
import java.lang.ArrayIndexOutOfBoundsException;
4-
import java.lang.String;
54

65
public class MyArrayList<AnyType> {
76
private static final int DEFAULT_CAPACITY = 10;
87

98
private int size;
10-
private AnyType[] items;
11-
12-
public MyArrayList() {
13-
clear();
14-
}
15-
16-
public void clear() {
17-
this.size = 0;
18-
this.ensureCapacity(DEFAULT_CAPACITY);
19-
}
20-
21-
public int size() {
22-
return this.size;
23-
}
24-
25-
public AnyType get(int index) {
26-
if (index < 0 || index >= this.size()) {
27-
throw new ArrayIndexOutOfBoundsException();
28-
}
29-
return this.items[index];
30-
}
31-
32-
public void set(int index, AnyType newVal) {
33-
if (index < 0 || index >= this.size()) {
34-
throw new ArrayIndexOutOfBoundsException();
35-
}
36-
this.items[index] = newVal;
37-
}
38-
39-
public void add(int index, AnyType item) {
40-
if (this.size() == this.items.length) {
41-
ensureCapacity(2*this.size());
42-
}
43-
for (int i = this.size; i > index; i--) {
44-
this.items[i] = this.items[i-1];
45-
}
46-
this.items[index] = item;
47-
48-
this.size++;
49-
}
50-
51-
public void add(AnyType item) {
52-
this.add(this.size(), item);
53-
}
54-
55-
public AnyType remove(int index) {
56-
if (index < 0 || index >= this.size()) {
57-
throw new ArrayIndexOutOfBoundsException();
58-
}
59-
AnyType removedItem = this.items[index];
60-
for (int i = index; i < this.size(); i++) {
61-
this.items[i] = this.items[i+1];
62-
}
63-
64-
this.size--;
65-
return removedItem;
66-
}
67-
68-
public String toString() {
69-
return this.items.toString();
70-
}
71-
72-
private void ensureCapacity(int newCapacity) {
73-
if (newCapacity < this.size) return;
74-
75-
AnyType[] old = this.items;
76-
items = new AnyType[newCapacity];
77-
for (int i = 0; i < this.size(); i++) {
78-
items[i] = old[i];
79-
}
80-
}
9+
private AnyType[] items;
10+
11+
public MyArrayList() {
12+
clear();
13+
}
14+
15+
public void clear() {
16+
this.size = 0;
17+
this.ensureCapacity(DEFAULT_CAPACITY);
18+
}
19+
20+
public int size() {
21+
return this.size;
22+
}
23+
24+
public AnyType get(int index) {
25+
if (index < 0 || index >= this.size()) {
26+
throw new ArrayIndexOutOfBoundsException();
27+
}
28+
return this.items[index];
29+
}
30+
31+
public void set(int index, AnyType newVal) {
32+
if (index < 0 || index >= this.size()) {
33+
throw new ArrayIndexOutOfBoundsException();
34+
}
35+
this.items[index] = newVal;
36+
}
37+
38+
public void add(int index, AnyType item) {
39+
if (this.size() == this.items.length) {
40+
ensureCapacity(2 * this.size());
41+
}
42+
43+
for (int i = this.size; i > index; i--) {
44+
this.items[i] = this.items[i - 1];
45+
}
46+
this.items[index] = item;
47+
48+
this.size++;
49+
}
50+
51+
public void add(AnyType item) {
52+
this.add(this.size(), item);
53+
}
54+
55+
public AnyType remove(int index) {
56+
if (index < 0 || index >= this.size()) {
57+
throw new ArrayIndexOutOfBoundsException();
58+
}
59+
AnyType removedItem = this.items[index];
60+
for (int i = index; i < this.size(); i++) {
61+
this.items[i] = this.items[i + 1];
62+
}
63+
64+
this.size--;
65+
return removedItem;
66+
}
67+
68+
@SuppressWarnings("unchecked")
69+
private void ensureCapacity(int newCapacity) {
70+
if (newCapacity < this.size)
71+
return;
72+
73+
AnyType[] old = this.items;
74+
items = (AnyType[]) new Object[newCapacity];
75+
for (int i = 0; i < this.size(); i++) {
76+
items[i] = old[i];
77+
}
78+
}
8179
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package arraylist;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
public class MyArrayListTest {
9+
10+
private static MyArrayList<String> list = new MyArrayList<String>();
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
list.clear();
15+
}
16+
17+
@Test
18+
public void testClear() {
19+
list.clear();
20+
assertEquals(0, list.size());
21+
}
22+
23+
@Test
24+
public void testSize() {
25+
assertEquals(0, list.size());
26+
list.add("1");
27+
assertEquals(1, list.size());
28+
}
29+
30+
@Test
31+
public void testGet() {
32+
list.add("1");
33+
list.add("2");
34+
list.add("3");
35+
String item = list.get(0);
36+
assertEquals("1", item);
37+
}
38+
39+
@Test
40+
public void testSet() {
41+
list.add("1");
42+
list.add("2");
43+
list.add("3");
44+
list.set(2, "4");
45+
assertEquals("4", list.get(2));
46+
}
47+
48+
@Test
49+
public void testAddIntAnyType() {
50+
list.add("1");
51+
list.add("2");
52+
list.add("3");
53+
list.add(0, "4");
54+
assertEquals("4", list.get(0));
55+
}
56+
57+
@Test
58+
public void testAddAnyType() {
59+
list.add("1");
60+
list.add("2");
61+
list.add("3");
62+
assertEquals(3, list.size());
63+
assertEquals("3", list.get(2));
64+
}
65+
66+
@Test
67+
public void testRemove() {
68+
list.add("1");
69+
list.add("2");
70+
list.add("3");
71+
list.remove(2);
72+
assertEquals(2, list.size());
73+
assertEquals("2", list.get(1));
74+
}
75+
76+
}

0 commit comments

Comments
 (0)