Skip to content

Commit efe3acb

Browse files
author
jsquared21
committed
Add Ex_27.10
1 parent cfce324 commit efe3acb

16 files changed

+567
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*********************************************************************************
2+
* (Compare MyHashSet and MyArrayList) MyArrayList is defined in Listing 24.3. *
3+
* Write a program that generates 1000000 random double values between 0 and *
4+
* 999999 and stores them in a MyArrayList and in a MyHashSet. Generate a list of *
5+
* 1000000 random double values between 0 and 1999999. For each number in the *
6+
* list, test if it is in the array list and in the hash set. Run your program to *
7+
* display the total test time for the array list and for the hash set. *
8+
*********************************************************************************/
9+
public class Exercise_27_10 {
10+
public static void main(String[] args) {
11+
// Create a MyHashSet
12+
MySet<Integer> set = new MyHashSet<>();
13+
set.add();
14+
}
15+
}
Binary file not shown.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*********************************************************************************
2+
* (Compare MyHashSet and MyArrayList) MyArrayList is defined in Listing 24.3. *
3+
* Write a program that generates 1000000 random double values between 0 and *
4+
* 999999 and stores them in a MyArrayList and in a MyHashSet. Generate a list of *
5+
* 1000000 random double values between 0 and 999999. For each number in the *
6+
* list, test if it is in the array list and in the hash set. Run your program to *
7+
* display the total test time for the array list and for the hash set. *
8+
*********************************************************************************/
9+
public class Exercise_27_10 {
10+
static final int VALUES = 1000000;
11+
12+
public static void main(String[] args) {
13+
MySet<Double> set = new MyHashSet<>(); // Create a MyHashSet
14+
MyList<Double> arrayList = new MyArrayList<>(); // Create a MyArrayList
15+
16+
// Generate 1000000 random double values between 0 and 999999
17+
for (int i = 0; i < VALUES; i++) {
18+
double randomDouble = Math.random() * VALUES;
19+
set.add(randomDouble);
20+
arrayList.add(randomDouble);
21+
}
22+
23+
// Create a list
24+
double[] list = new double[VALUES];
25+
for (int i = 0; i < list.length; i++) {
26+
list[i] = Math.random() * VALUES;
27+
}
28+
29+
System.out.println("Total time for the hash set: " +
30+
testTimeSet(list, set) + " milliseconds");
31+
32+
System.out.println("Total time for the array list: " +
33+
testTimeArray(list, arrayList) + " milliseconds");
34+
}
35+
36+
/** Return total test time for the hash set */
37+
public static long testTimeSet(double[] list, MySet<Double> set) {
38+
long startTime = System.currentTimeMillis();
39+
for (int i = 0; i < VALUES; i++) {
40+
set.contains(list[i]);
41+
}
42+
return System.currentTimeMillis() - startTime;
43+
}
44+
45+
/** Return total test time for the array list */
46+
public static long testTimeArray(double[] list, MyList<Double> array) {
47+
long startTime = System.currentTimeMillis();
48+
for (int i = 0; i < VALUES; i++) {
49+
array.contains(list[i]);
50+
}
51+
return System.currentTimeMillis() - startTime;
52+
}
53+
}
1015 Bytes
Binary file not shown.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public abstract class MyAbstractList<E> implements MyList<E> {
2+
protected int size = 0; // The size of the list
3+
4+
/** Create a default list */
5+
protected MyAbstractList() {
6+
}
7+
8+
/** Create a list from an array of objects */
9+
protected MyAbstractList(E[] objects) {
10+
for (int i = 0; i < objects.length; i++)
11+
add(objects[i]);
12+
}
13+
14+
@Override /** Add a new element at the end of this list */
15+
public void add(E e) {
16+
add(size, e);
17+
}
18+
19+
@Override /** Return true if this list doen't contain any elements */
20+
public boolean isEmpty() {
21+
return size == 0;
22+
}
23+
24+
@Override /** Return the number of elements in this list */
25+
public int size() {
26+
return size;
27+
}
28+
29+
@Override /** Remove the first occurence of the element e
30+
* from this list. Shift any subsequent elements to the left.
31+
* Return true if the element is removed. */
32+
public boolean remove(E e) {
33+
if (indexOf(e) >= 0) {
34+
remove(indexOf(e));
35+
return true;
36+
}
37+
else
38+
return false;
39+
}
40+
}
181 Bytes
Binary file not shown.
Binary file not shown.
3.08 KB
Binary file not shown.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
public class MyArrayList<E> extends MyAbstractList<E> {
2+
public static final int INITIAL_CAPACITY = 16;
3+
private E[] data = (E[]) new Object[INITIAL_CAPACITY];
4+
5+
/** Create a default list */
6+
public MyArrayList() {
7+
}
8+
9+
/** Create a list from an array of objects */
10+
public MyArrayList(E[] objects) {
11+
for (int i = 0; i < objects.length; i++)
12+
add(objects[i]);
13+
}
14+
15+
@Override /** Add a new element at the specified index */
16+
public void add(int index, E e) {
17+
ensureCapacity();
18+
19+
// Move the elements to the right after the speciied index
20+
for (int i = size - 1; i >= index; i--)
21+
data[i + 1] = data[i];
22+
23+
// Insert new element to data[index]
24+
data[index] = e;
25+
26+
// Increase size by 1
27+
size++;
28+
}
29+
30+
/** Create a new larger array, double the current size + 1 */
31+
private void ensureCapacity() {
32+
if (size >= data.length) {
33+
E[] newData = (E[])(new Object[size * 2 + 1]);
34+
System.arraycopy(data, 0, newData, 0, size);
35+
data = newData;
36+
}
37+
}
38+
39+
@Override /** Clear the list */
40+
public void clear() {
41+
data = (E[])new Object[INITIAL_CAPACITY];
42+
size = 0;
43+
}
44+
45+
@Override /** Return true if this list contains the element */
46+
public boolean contains(E e) {
47+
for (int i = 0; i < size; i++)
48+
if (e.equals(data[i])) return true;
49+
50+
return false;
51+
}
52+
53+
@Override /** Return the element at the specified index */
54+
public E get(int index) {
55+
checkIndex(index);
56+
return data[index];
57+
}
58+
59+
private void checkIndex(int index) {
60+
if (index < 0 || index >= size)
61+
throw new IndexOutOfBoundsException("index" + index + " out of bounds");
62+
}
63+
64+
@Override /** Return the index of the first matching element
65+
* in this list. Return -1 if no match. */
66+
public int indexOf(E e) {
67+
for (int i = 0; i < size; i++)
68+
if (e.equals(data[i])) return i;
69+
70+
return -1;
71+
}
72+
73+
@Override /** Return the index of the last matching element
74+
* in this list. Return -1 if no match. */
75+
public int lastIndexOf(E e) {
76+
for (int i = size - 1; i >= 0; i--)
77+
if (e.equals(data[i])) return i;
78+
79+
return -1;
80+
}
81+
82+
@Override /** Remove the element at the specified position
83+
* in this list. Shift any subsequent elements to the left.
84+
* Return the element that was removed from the list. */
85+
public E remove(int index) {
86+
checkIndex(index);
87+
88+
E e = data[index];
89+
90+
// Shift data to the left
91+
for (int j = index; j < size - 1; j++)
92+
data[j] = data[j + 1];
93+
94+
data[size - 1] = null; // This element is now null
95+
96+
// Decrement size
97+
size--;
98+
99+
return e;
100+
}
101+
102+
@Override /** Replace the element at the specified position
103+
* in this list with the specified element. */
104+
public E set(int index, E e) {
105+
checkIndex(index);
106+
E old = data[index];
107+
data[index] = e;
108+
return old;
109+
}
110+
111+
@Override
112+
public String toString() {
113+
StringBuilder result = new StringBuilder("[");
114+
115+
for (int i = 0; i < size; i++) {
116+
result.append(data[i]);
117+
if (i < size - 1) result.append(", ");
118+
}
119+
120+
return result.toString() + "]";
121+
}
122+
123+
/** Trims the capacity to current size */
124+
public void trimToSize() {
125+
if (size != data.length) {
126+
E[] newData = (E[])(new Object[size]);
127+
System.arraycopy(data, 0, newData, 0, size);
128+
data = newData;
129+
} // If size == capacity, no need to trim
130+
}
131+
132+
@Override /** Override iterator() defined in Iterable */
133+
public java.util.Iterator<E> iterator() {
134+
return new ArrayListIterator();
135+
}
136+
137+
private class ArrayListIterator
138+
implements java.util.Iterator<E> {
139+
private int current = 0; // Current index
140+
141+
@Override
142+
public boolean hasNext() {
143+
return (current < size);
144+
}
145+
146+
@Override
147+
public E next() {
148+
return data[current++];
149+
}
150+
151+
@Override
152+
public void remove() {
153+
MyArrayList.this.remove(current);
154+
}
155+
}
156+
}
Binary file not shown.

0 commit comments

Comments
 (0)