-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList_inArraySlots.java
More file actions
137 lines (112 loc) · 3.73 KB
/
List_inArraySlots.java
File metadata and controls
137 lines (112 loc) · 3.73 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
Implement a list of integer elements, including
both data and operations.
*/
public class List_inArraySlots {
// declare fields here
private int[] elements; // container for the elements of the list
private int filledElements; // the number of elements in this list
private static final int INITIAL_CAPACITY = 10;
/**
Construct an empty list with a small initial capacity.
*/
public List_inArraySlots() {
elements = new int[ INITIAL_CAPACITY];
// filledElements has been initialized to the desired value, 0
}
/**
@return the number of elements in this list
*/
public int size() {
return filledElements;
}
/**
@return a string representation of this list,
in [a,b,c,] format
*/
public String toString() {
String result = "[";
for( int elemIndex = 0; elemIndex < filledElements; elemIndex++)
result += elements[ elemIndex] + ",";
return result + "]";
}
/**
Appends @value to the end of this list.
@return true, in keeping with conventions yet to be discussed
*/
public boolean add( int value) {
// expand if necessary
if( filledElements == elements.length) expand();
elements[ filledElements] = value;
filledElements++;
// idiomatic version: elements[ filledElements++] = value;
return true;
}
/**
Double the capacity of the List_inArraySlots,
preserving existing data.
*/
private void expand() {
System.out.println( "expand... (for debugging)");
/* S.O.P. rules for debugging:
Working methods should be silent. But during
development, the programmer must verify that
this method is called when that is appropriate.
So test using the println(), then comment it out.
*/
int[] bigger = new int[ elements.length * 2];
for( int elemIndex = 0; elemIndex < filledElements; elemIndex++)
bigger[ elemIndex] = elements[ elemIndex];
elements = bigger;
}
/**
accessor
@return element @index from this list
precondition: @index is within the bounds of the array.
(Having warned the user about this precondition,
you should NOT complicate your code to check
whether user violated the condition.)
*/
public int get( int index ) {
return elements[index];
}
/**
Set value at @index to @newValue
@return old value at @index
@precondition: @index is within the bounds of this list.
*/
public int set( int index, int newValue ) {
int oldValue = elements[index];
elements[index]= newValue;
return oldValue;
}
/**
Insert @value at position @index in this list.
Shift the element currently at that position (if any)
and any subsequent elements to the right
(that is, increase the index associated with each).
*/
public void add( int index, int value) {
// expand if necessary
if( filledElements == elements.length) expand();
for(int num = filledElements; num > index; num--){
elements[num] = elements[num - 1];
}
set(index, value);
filledElements++;
}
/**
Remove the element at position @index in this list.
Shift any subsequent elements to the left (that is,
decrease the index associated with each).
@return the value that was removed from the list
*/
public int remove( int index) {
int oldValue = elements[index];
for(int num = index; num < filledElements - 1; num++){
elements[num] = elements[num + 1];
}
elements[filledElements--] = 0;
return oldValue;
}
}