Skip to content

Commit 65f68d8

Browse files
committed
chapter 22
1 parent d9581b7 commit 65f68d8

File tree

6 files changed

+438
-3
lines changed

6 files changed

+438
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ ____
8686
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_20"><strong>Chapter 20</strong></a> - Lists, Stacks, Queues, and Priority Queues</li><br>
8787
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_21"><strong>Chapter 21</strong></a> - Sets and Maps</li><br>
8888
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_22"><strong>Chapter 22</strong></a> - Developing Efficient Algorithms</li><br>
89-
<li><!--<a href="https://github.com/HarryDulaney/java-programming-daniel-liang-10th/tree/master/">--><strong>Chapter 23</strong><!--</a>--> - Sorting</li><br>
90-
<li><!--<a href="https://github.com/HarryDulaney/java-programming-daniel-liang-10th/tree/master/">--><strong>Chapter 24</strong><!--</a>--> - Implementing Lists, Stacks, Queues, and Priority Queues</li><br>
89+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_23"><strong>Chapter 23</strong></a> - Sorting</li><br>
90+
<li><a href="https://github.com/HarryDulaney/java-programming-daniel-liang-10th/tree/master/ch_24"><strong>Chapter 24</strong></a> - Implementing Lists, Stacks, Queues, and Priority Queues</li><br>
9191
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_25"><strong>Chapter 25</strong></a> - Binary Search Trees </li><br>
9292
<li><!--<a href="https://github.com/HarryDulaney/java-programming-daniel-liang-10th/tree/master/">--><strong>Chapter 26</strong><!--</a>--> - AVL Trees </li><br>
9393
<li><!--<a href="https://github.com/HarryDulaney/java-programming-daniel-liang-10th/tree/master/">--><strong>Chapter 27</strong><!--</a>--> - Hashing</li><br>

ch_22/Exercise22_01.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Time complexity is O(n^2).
2020
* As the length of the input grows, the time complexity will grow
2121
* exponentially because we use an inner loop to traverse each
22-
* sub-string of input string.
22+
* sub-string of the input string.
2323
*/
2424
public class Exercise22_01 {
2525
public static void main(String[] args) {
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ch_24.exercise24_01;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
8+
/**
9+
* 24.1 (Add set operations in MyList) Define the following methods in MyList and
10+
* implement them in MyAbstractList:
11+
* Adds the elements in otherList to this list.
12+
* Returns true if this list changed as a result of the call
13+
* public boolean addAll(MyList<E> otherList);
14+
* <p>
15+
* Removes all the elements in otherList from this list
16+
* Returns true if this list changed as a result of the call
17+
* <p>
18+
* public boolean removeAll(MyList<E> otherList);
19+
* <p>
20+
* Retains the elements in this list that are also in otherList
21+
* Returns true if this list changed as a result of the call
22+
* <p>
23+
* public boolean retainAll(MyList<E> otherList);
24+
* <p>
25+
* <p>
26+
* Write a test program that creates two MyArrayLists,list1 and list2, with
27+
* the initial values{"Tom","George","Peter","Jean","Jane"} and
28+
* {"Tom","George","Michael","Michelle","Daniel"},then perform the following operations:
29+
* ■ Invokes list1.addAll(list2),and displays list1 and list2.
30+
* ■ Recreates list1 and list2 with the same initial values,invokes
31+
* list1.removeAll(list2),and displays list1 and list2.
32+
* ■ Recreates list1 and list2 with the same initial values,invokes
33+
* list1.retainAll(list2),and displays list1 and list2.
34+
*/
35+
public class Exercise24_01 {
36+
private final static String[] LIST_ONE_INIT_VALS = {"Tom", "George", "Peter", "Jean", "Jane"};
37+
private final static String[] LIST_TWO_INIT_VALS = {"Tom", "George", "Michael", "Michelle", "Daniel"};
38+
39+
public static void main(String[] args) {
40+
41+
MyList<String> list1 = new MyArrayList<>(LIST_ONE_INIT_VALS);
42+
MyList<String> list2 = new MyArrayList<>(LIST_TWO_INIT_VALS);
43+
System.out.println("List 1 and List 2 initial values: ");
44+
System.out.println("list1: " + list1);
45+
System.out.println("list2: " + list2);
46+
47+
48+
System.out.println("list1.addAll(list2) result: ");
49+
list1.addAll(list2);
50+
System.out.println("list1: " + list1);
51+
System.out.println("list2: " + list2);
52+
53+
list1 = new MyArrayList<>(LIST_ONE_INIT_VALS);
54+
list2 = new MyArrayList<>(LIST_TWO_INIT_VALS);
55+
56+
System.out.println("list1.removeAll(list2) result: ");
57+
list1.removeAll(list2);
58+
System.out.println("list1: " + list1);
59+
System.out.println("list2: " + list2);
60+
61+
list1 = new MyArrayList<>(LIST_ONE_INIT_VALS);
62+
list2 = new MyArrayList<>(LIST_TWO_INIT_VALS);
63+
64+
System.out.println("list1.retainAll(list2) result: ");
65+
list1.retainAll(list2);
66+
System.out.println("list1: " + list1);
67+
System.out.println("list2: " + list2);
68+
69+
}
70+
}
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package ch_24.exercise24_01;
2+
3+
import java.util.Iterator;
4+
5+
public abstract class MyAbstractList<E> implements MyList<E> {
6+
protected int size = 0; // The size of the list
7+
8+
/**
9+
* Create a default list
10+
*/
11+
protected MyAbstractList() {
12+
}
13+
14+
/**
15+
* Create a list from an array of objects
16+
*/
17+
protected MyAbstractList(E[] objects) {
18+
for (int i = 0; i < objects.length; i++)
19+
add(objects[i]);
20+
}
21+
22+
/**
23+
* Add a new element at the end of this list
24+
*/
25+
@Override
26+
public void add(E e) {
27+
add(size, e);
28+
}
29+
30+
/**
31+
* Return true if this list doesn't contain any elements
32+
*/
33+
@Override
34+
public boolean isEmpty() {
35+
return size == 0;
36+
}
37+
38+
/**
39+
* Return the number of elements in this list
40+
*/
41+
@Override
42+
public int size() {
43+
return size;
44+
}
45+
46+
47+
/**
48+
* Remove the first occurrence of the element e
49+
* from this list. Shift any subsequent elements to the left.
50+
* Return true if the element is removed.
51+
*/
52+
@Override
53+
public boolean remove(E e) {
54+
if (indexOf(e) >= 0) {
55+
remove(indexOf(e));
56+
return true;
57+
} else
58+
return false;
59+
}
60+
61+
/*--------------------------------------------- Exercise24_01 ---------------------------------------------------*/
62+
63+
/**
64+
* Adds the elements in otherList to this list.
65+
* Returns true if this list changed as a result of the call
66+
*/
67+
@Override
68+
public boolean addAll(MyList<E> otherList) {
69+
int startSize = size;
70+
for (E e : otherList) {
71+
if (!contains(e)) {
72+
add(e);
73+
}
74+
}
75+
return size == startSize;
76+
}
77+
78+
/**
79+
* Removes all the elements in otherList from this list
80+
* Returns true if this list changed as a result of the call
81+
*/
82+
@Override
83+
public boolean removeAll(MyList<E> otherList) {
84+
boolean changed = false;
85+
for (E e : otherList) {
86+
boolean removed = remove(e);
87+
if (removed) {
88+
changed = true;
89+
}
90+
}
91+
return changed;
92+
}
93+
94+
/**
95+
* Retains the elements in this list that are also in otherList
96+
* Returns true if this list changed as a result of the call
97+
*/
98+
@Override
99+
public boolean retainAll(MyList<E> otherList) {
100+
boolean changed = false;
101+
for (E e : this) {
102+
if (!otherList.contains(e)) {
103+
remove(indexOf(e));
104+
changed = true;
105+
}
106+
}
107+
return changed;
108+
}
109+
}

ch_24/exercise24_01/MyArrayList.java

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package ch_24.exercise24_01;
2+
3+
import java.util.Iterator;
4+
5+
public class MyArrayList<E> extends MyAbstractList<E> {
6+
7+
public static final int INITIAL_CAPACITY = 16;
8+
private E[] data = (E[]) new Object[INITIAL_CAPACITY];
9+
10+
/**
11+
* Create a default list
12+
*/
13+
public MyArrayList() {
14+
}
15+
16+
/**
17+
* Create a list from an array of objects
18+
*/
19+
public MyArrayList(E[] objects) {
20+
for (int i = 0; i < objects.length; i++)
21+
add(objects[i]); // Warning: don't use super(objects)!
22+
}
23+
24+
@Override
25+
/** Add a new element at the specified index */
26+
public void add(int index, E e) {
27+
ensureCapacity();
28+
// Move the elements to the right after the specified index
29+
for (int i = size - 1; i >= index; i--)
30+
data[i + 1] = data[i];
31+
// Insert new element to data[index]
32+
data[index] = e;
33+
// Increase size by 1
34+
size++;
35+
}
36+
37+
/**
38+
* Create a new larger array, double the current size + 1
39+
*/
40+
private void ensureCapacity() {
41+
if (size >= data.length) {
42+
E[] newData = (E[]) (new Object[size * 2 + 1]);
43+
System.arraycopy(data, 0, newData, 0, size);
44+
data = newData;
45+
}
46+
}
47+
48+
@Override
49+
/** Clear the list */
50+
public void clear() {
51+
data = (E[]) new Object[INITIAL_CAPACITY];
52+
size = 0;
53+
}
54+
55+
@Override
56+
/** Return true if this list contains the element */
57+
public boolean contains(E e) {
58+
for (int i = 0; i < size; i++)
59+
if (e.equals(data[i])) return true;
60+
return false;
61+
}
62+
63+
@Override
64+
/** Return the element at the specified index */
65+
public E get(int index) {
66+
checkIndex(index);
67+
return data[index];
68+
}
69+
70+
private void checkIndex(int index) {
71+
if (index < 0 || index >= size)
72+
throw new IndexOutOfBoundsException
73+
("index " + index + " out of bounds");
74+
}
75+
76+
@Override
77+
/** Return the index of the first matching element
78+
* in this list. Return -1 if no match. */
79+
public int indexOf(E e) {
80+
for (int i = 0; i < size; i++)
81+
if (e.equals(data[i])) return i;
82+
return -1;
83+
}
84+
85+
@Override
86+
/** Return the index of the last matching element
87+
* in this list. Return -1 if no match. */
88+
public int lastIndexOf(E e) {
89+
for (int i = size - 1; i >= 0; i--)
90+
if (e.equals(data[i])) return i;
91+
return -1;
92+
}
93+
94+
@Override
95+
/** Remove the element at the specified position
96+
* in this list. Shift any subsequent elements to the left.
97+
* Return the element that was removed from the list. */
98+
public E remove(int index) {
99+
checkIndex(index);
100+
E e = data[index];
101+
// Shift data to the left
102+
for (int j = index; j < size - 1; j++)
103+
data[j] = data[j + 1];
104+
data[size - 1] = null; // This element is now null
105+
// Decrement size
106+
size--;
107+
return e;
108+
}
109+
110+
@Override
111+
/** Replace the element at the specified position
112+
* in this list with the specified element. */
113+
public E set(int index, E e) {
114+
checkIndex(index);
115+
E old = data[index];
116+
data[index] = e;
117+
return old;
118+
}
119+
120+
@Override
121+
public String toString() {
122+
StringBuilder result = new StringBuilder("[");
123+
for (int i = 0; i < size; i++) {
124+
result.append(data[i]);
125+
if (i < size - 1) result.append(", ");
126+
}
127+
return result.toString() + "]";
128+
}
129+
130+
/**
131+
* Trims the capacity to current size
132+
*/
133+
public void trimToSize() {
134+
if (size != data.length) {
135+
E[] newData = (E[]) (new Object[size]);
136+
System.arraycopy(data, 0, newData, 0, size);
137+
data = newData;
138+
} // If size == capacity, no need to trim
139+
}
140+
141+
@Override
142+
/** Override iterator() defined in Iterable */
143+
public java.util.Iterator<E> iterator() {
144+
return new ArrayListIterator();
145+
}
146+
147+
private class ArrayListIterator
148+
implements java.util.Iterator<E> {
149+
private int current = 0; // Current index
150+
151+
@Override
152+
public boolean hasNext() {
153+
return (current < size);
154+
}
155+
156+
@Override
157+
public E next() {
158+
return data[current++];
159+
}
160+
161+
@Override
162+
public void remove() {
163+
MyArrayList.this.remove(current);
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)