Java Reference
In-Depth Information
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
clear
public boolean
contains(E e) {
contains
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
get
return
data[index];
57 }
58
59
private void
checkIndex(
int
index) {
60
if
(index <
0
|| index >= size)
61
throw new
IndexOutOfBoundsException
62 (
"index "
+ index +
" out of bounds"
);
63 }
64
65 @Override
/** Return the index of the first matching element
66
* in this list. Return -1 if no match. */
67
checkIndex
public int
indexOf(E e) {
indexOf
68
for
(
int
i =
0
; i < size; i++)
69
if
(e.equals(data[i]))
return
i;
70
71
return
-1
;
72 }
73
74 @Override
/** Return the index of the last matching element
75
* in this list. Return -1 if no match. */
76
public int
lastIndexOf(E e) {
lastIndexOf
77
for
(
int
i = size -
1
; i >=
0
; i--)
78
if
(e.equals(data[i]))
return
i;
79
80
return
-1
;
81 }
82
83 @Override
/** Remove the element at the specified position
84
* in this list. Shift any subsequent elements to the left.
85
* Return the element that was removed from the list. */
86
public
E remove(
int
index) {
87 checkIndex(index);
88
89 E e = data[index];
90
91
// Shift data to the left
92
for
(
int
j = index; j < size -
1
; j++)
93 data[j] = data[j +
1
];
94
95 data[size -
1
] =
null
;
// This element is now null
96
97
// Decrement size
98 size--;
remove
Search WWH ::
Custom Search