Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitefe3acb

Browse files
author
jsquared21
committed
Add Ex_27.10
1 parentcfce324 commitefe3acb

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+
publicclassExercise_27_10 {
10+
staticfinalintVALUES =1000000;
11+
12+
publicstaticvoidmain(String[]args) {
13+
MySet<Double>set =newMyHashSet<>();// Create a MyHashSet
14+
MyList<Double>arrayList =newMyArrayList<>();// Create a MyArrayList
15+
16+
// Generate 1000000 random double values between 0 and 999999
17+
for (inti =0;i <VALUES;i++) {
18+
doublerandomDouble =Math.random() *VALUES;
19+
set.add(randomDouble);
20+
arrayList.add(randomDouble);
21+
}
22+
23+
// Create a list
24+
double[]list =newdouble[VALUES];
25+
for (inti =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+
publicstaticlongtestTimeSet(double[]list,MySet<Double>set) {
38+
longstartTime =System.currentTimeMillis();
39+
for (inti =0;i <VALUES;i++) {
40+
set.contains(list[i]);
41+
}
42+
returnSystem.currentTimeMillis() -startTime;
43+
}
44+
45+
/** Return total test time for the array list */
46+
publicstaticlongtestTimeArray(double[]list,MyList<Double>array) {
47+
longstartTime =System.currentTimeMillis();
48+
for (inti =0;i <VALUES;i++) {
49+
array.contains(list[i]);
50+
}
51+
returnSystem.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+
publicabstractclassMyAbstractList<E>implementsMyList<E> {
2+
protectedintsize =0;// The size of the list
3+
4+
/** Create a default list */
5+
protectedMyAbstractList() {
6+
}
7+
8+
/** Create a list from an array of objects */
9+
protectedMyAbstractList(E[]objects) {
10+
for (inti =0;i <objects.length;i++)
11+
add(objects[i]);
12+
}
13+
14+
@Override/** Add a new element at the end of this list */
15+
publicvoidadd(Ee) {
16+
add(size,e);
17+
}
18+
19+
@Override/** Return true if this list doen't contain any elements */
20+
publicbooleanisEmpty() {
21+
returnsize ==0;
22+
}
23+
24+
@Override/** Return the number of elements in this list */
25+
publicintsize() {
26+
returnsize;
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+
publicbooleanremove(Ee) {
33+
if (indexOf(e) >=0) {
34+
remove(indexOf(e));
35+
returntrue;
36+
}
37+
else
38+
returnfalse;
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+
publicclassMyArrayList<E>extendsMyAbstractList<E> {
2+
publicstaticfinalintINITIAL_CAPACITY =16;
3+
privateE[]data = (E[])newObject[INITIAL_CAPACITY];
4+
5+
/** Create a default list */
6+
publicMyArrayList() {
7+
}
8+
9+
/** Create a list from an array of objects */
10+
publicMyArrayList(E[]objects) {
11+
for (inti =0;i <objects.length;i++)
12+
add(objects[i]);
13+
}
14+
15+
@Override/** Add a new element at the specified index */
16+
publicvoidadd(intindex,Ee) {
17+
ensureCapacity();
18+
19+
// Move the elements to the right after the speciied index
20+
for (inti =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+
privatevoidensureCapacity() {
32+
if (size >=data.length) {
33+
E[]newData = (E[])(newObject[size *2 +1]);
34+
System.arraycopy(data,0,newData,0,size);
35+
data =newData;
36+
}
37+
}
38+
39+
@Override/** Clear the list */
40+
publicvoidclear() {
41+
data = (E[])newObject[INITIAL_CAPACITY];
42+
size =0;
43+
}
44+
45+
@Override/** Return true if this list contains the element */
46+
publicbooleancontains(Ee) {
47+
for (inti =0;i <size;i++)
48+
if (e.equals(data[i]))returntrue;
49+
50+
returnfalse;
51+
}
52+
53+
@Override/** Return the element at the specified index */
54+
publicEget(intindex) {
55+
checkIndex(index);
56+
returndata[index];
57+
}
58+
59+
privatevoidcheckIndex(intindex) {
60+
if (index <0 ||index >=size)
61+
thrownewIndexOutOfBoundsException("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+
publicintindexOf(Ee) {
67+
for (inti =0;i <size;i++)
68+
if (e.equals(data[i]))returni;
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+
publicintlastIndexOf(Ee) {
76+
for (inti =size -1;i >=0;i--)
77+
if (e.equals(data[i]))returni;
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+
publicEremove(intindex) {
86+
checkIndex(index);
87+
88+
Ee =data[index];
89+
90+
// Shift data to the left
91+
for (intj =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+
returne;
100+
}
101+
102+
@Override/** Replace the element at the specified position
103+
*in this list with the specified element. */
104+
publicEset(intindex,Ee) {
105+
checkIndex(index);
106+
Eold =data[index];
107+
data[index] =e;
108+
returnold;
109+
}
110+
111+
@Override
112+
publicStringtoString() {
113+
StringBuilderresult =newStringBuilder("[");
114+
115+
for (inti =0;i <size;i++) {
116+
result.append(data[i]);
117+
if (i <size -1)result.append(", ");
118+
}
119+
120+
returnresult.toString() +"]";
121+
}
122+
123+
/** Trims the capacity to current size */
124+
publicvoidtrimToSize() {
125+
if (size !=data.length) {
126+
E[]newData = (E[])(newObject[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+
publicjava.util.Iterator<E>iterator() {
134+
returnnewArrayListIterator();
135+
}
136+
137+
privateclassArrayListIterator
138+
implementsjava.util.Iterator<E> {
139+
privateintcurrent =0;// Current index
140+
141+
@Override
142+
publicbooleanhasNext() {
143+
return (current <size);
144+
}
145+
146+
@Override
147+
publicEnext() {
148+
returndata[current++];
149+
}
150+
151+
@Override
152+
publicvoidremove() {
153+
MyArrayList.this.remove(current);
154+
}
155+
}
156+
}
Binary file not shown.
3.68 KB
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp