Java Reference
In-Depth Information
41 result += "]";
42
return
result;
43 }
44 }
45
46 // post : returns the position of the first occurrence of the
47 // given value (-1 if not found)
48
public int
indexOf(
int
value) {
49
for
(
int
i = 0; i < size; i++) {
50
if
(elementData[i] == value) {
51
return
i;
52 }
53 }
54
return
-1;
55 }
56
57 // pre : size() < capacity
58 // post: appends the given value to the end of the list
59
public void
add(
int
value) {
60 elementData[size] = value;
61 size++;
62 }
63
64 // pre : size() < capacity && 0 <= index <= size()
65 // post: inserts the given value at the given index, shifting
66 // subsequent values right
67
public void
add(
int
index,
int
value) {
68
for
(
int
i = size; i >= index + 1; i--) {
69 elementData[i] = elementData[i - 1];
70 }
71 elementData[index] = value;
72 size++;
73 }
74
75 // pre : 0 <= index < size()
76 // post: removes value at the given index, shifting
77 // subsequent values left
78
public void
remove(
int
index) {
79
for
(
int
i = index; i < size - 1; i++) {
80 elementData[i] = elementData[i + 1];
81 }
82 size--;
83 }
84 }
Search WWH ::
Custom Search