-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserOfList.java
More file actions
102 lines (85 loc) · 3.38 KB
/
UserOfList.java
File metadata and controls
102 lines (85 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
Test list features.
*/
public class UserOfList {
private static List_inArraySlots list;
public static void main( String[] args ) {
list = new List_inArraySlots();
System.out.println( "number of elements: " + list.size() );
System.out.println( "empty list: " + list);
/* Populate the list with elements, but with a small enough
number that we expect no invocation of expand().
*/
int elemIndex;
for( elemIndex = 0; elemIndex < 5; elemIndex++ ) {
list.add( -elemIndex); // differs from index, but similar
System.out.println( "number of elements: " + list.size() );
}
System.out.println("initial population of " + list.size() + " elements:");
System.out.println( list + System.lineSeparator());
// Add enough elements that expansion is expected
for( ; elemIndex < 15; elemIndex++ ) {
if( elemIndex == 10) System.out.println( "expansion expected");
list.add( -elemIndex);
System.out.println( "number of elements: " + list.size() );
}
System.out.println("result of second population: " + list.size() + " elements:");
System.out.println( list + System.lineSeparator());
// Trust no one.
for( ; elemIndex < 35; elemIndex++ )
list.add( -elemIndex);
System.out.println("after second expansion: " + list.size() + " elements:");
System.out.println( list + System.lineSeparator());
// --------- end of "code that worked in v0" ---------
// test accessor
System.out.println( "sample elements from list:");
for( elemIndex = 1; elemIndex < list.size(); elemIndex *= 2 ) {
System.out.println( "element " + elemIndex + ": "
+ list.get( elemIndex)
);
}
setTest( 8);
setTest( 16);
System.out.println();
// test adding at a specified position
addAtTest( 0, 29); // beginning of the list
// end of the list using the new add method
addAtTest( list.size(), 17);
addAtTest( 2, 19); // middle of a small list
// force an expansion
addAtTest( 2, 23);
addAtTest( 2, 23);
addAtTest( 2, 23);
// test removing an element
System.out.println("removing value " + list.remove( 6)
+ ", leaving " + list.size() + " elements:");
System.out.println( list);
System.out.println(
"expecting:" + System.lineSeparator()
+ "[29,0,23,23,23,19, NO -1 HERE! -2,-3...]");
} // end of main()
private static void setTest( int modifyAt
) {
System.out.println(
"changed element " + modifyAt + " from "
+ list.set( modifyAt, modifyAt + 1000) + " to "
+ list.get( modifyAt)
);
}
/**
Test the 2-argument add( index, value) method.
*/
private static void addAtTest( int addAt
, int value
) {
list.add( addAt, value);
System.out.println(
"insert " + value
+ " at position " + addAt
+ ", resulting in " + list.size() + " elements:"
+ System.lineSeparator()
+ list
+ System.lineSeparator()
);
}
}