Skip to content

Commit ea405b2

Browse files
committed
lecture code repo
1 parent 6407e80 commit ea405b2

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

lec7_lists4/AList.java

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
11
package lec7_lists4;
22

3-
public class AList {
3+
public class AList<Glorp> {
4+
private Glorp[] items;
45
private int size;
5-
private int[] items;
66

77
public AList() {
88
size = 0;
9-
items = new int[100];
9+
items = (Glorp[]) new Object[100];
10+
}
11+
// 0, 0, 0, 0, 0, 0, 0, ...
12+
// 3, 0, 0, 0, 0, 0, 0,
13+
// 3, 7, 0, 0, 0, 0, 0,
14+
// ^ size = 2
15+
//
16+
// Observation: Next new item will go in position size
17+
// The last item is in position size - 1
18+
// Size is the number of items.
19+
20+
// resize the underlying array to the desired capacity
21+
private void resize(int capacity) {
22+
Glorp[] a = (Glorp[]) new Object[capacity];
23+
System.arraycopy(items, 0, a, 0, size);
24+
items = a;
1025
}
1126

12-
// [3, 4, 2, 0, 0, 0, ....]
13-
// ^ (size = 3)
14-
// size is the location of the next add
15-
// size - 1 location of the last item
16-
17-
public void addLast(int x) {
27+
public void addLast(Glorp x) {
1828
if (size == items.length) {
1929
resize(size + 1);
2030
}
2131
items[size] = x;
2232
size += 1;
2333
}
2434

25-
private void resize(int newSize) {
26-
int[] a = new int[newSize + 1];
27-
System.arraycopy(items, 0, a, 0, size);
28-
items = a;
29-
}
30-
31-
public int getLast() {
35+
public Glorp getLast() {
3236
return items[size - 1];
3337
}
3438

35-
public int get(int i) {
39+
public Glorp get(int i) {
40+
if (i >= size) {
41+
throw new IllegalArgumentException("We don't have that much stuff");
42+
}
3643
return items[i];
3744
}
3845

39-
public int removeLast() {
40-
int itemToReturn = getLast();
46+
public Glorp removeLast() {
47+
Glorp itemToReturn = getLast();
4148
size -= 1;
4249
return itemToReturn;
4350
}

lec7_lists4/AListTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class AListTest {
1212
@Test
1313
public void testAddAndGet() {
14-
AList list = new AList();
14+
AList<Integer> list = new AList<>();
1515
list.addLast(5);
1616
list.addLast(10);
1717

@@ -22,7 +22,7 @@ public void testAddAndGet() {
2222

2323
@Test
2424
public void testGetLast() {
25-
AList list = new AList();
25+
AList<Integer> list = new AList<>();
2626
list.addLast(7);
2727
list.addLast(14);
2828
list.addLast(21);
@@ -33,7 +33,7 @@ public void testGetLast() {
3333

3434
@Test
3535
public void testRemoveLast() {
36-
AList list = new AList();
36+
AList<Integer> list = new AList<>();
3737
list.addLast(3);
3838
list.addLast(6);
3939
list.addLast(9);
@@ -50,15 +50,15 @@ public void testRemoveLast() {
5050
assertThat(list.removeLast()).isEqualTo(3);
5151
}
5252

53-
/*@Test
53+
@Test
5454
public void add200Items() {
55-
lec7_lists4.resizeExercise.AList list = new lec7_lists4.resizeExercise.AList();
55+
AList<Integer> list = new AList<>();
5656
for (int i = 0; i < 200; i += 1) {
5757
list.addLast(i);
5858
}
5959

6060
for (int i = 0; i < 200; i += 1) {
6161
assertThat(list.get(i)).isEqualTo(i);
6262
}
63-
}*/
63+
}
6464
}

0 commit comments

Comments
 (0)