1- package myarraylist
1+ package arraylist ;
22
33import java .lang .ArrayIndexOutOfBoundsException ;
4- import java .lang .String ;
54
65public class MyArrayList <AnyType > {
76 private static final int DEFAULT_CAPACITY = 10 ;
87
98 private int size ;
10- private AnyType [] items ;
11-
12- public MyArrayList () {
13- clear ();
14- }
15-
16- public void clear () {
17- this .size = 0 ;
18- this .ensureCapacity (DEFAULT_CAPACITY );
19- }
20-
21- public int size () {
22- return this .size ;
23- }
24-
25- public AnyType get (int index ) {
26- if (index < 0 || index >= this .size ()) {
27- throw new ArrayIndexOutOfBoundsException ();
28- }
29- return this .items [index ];
30- }
31-
32- public void set (int index , AnyType newVal ) {
33- if (index < 0 || index >= this .size ()) {
34- throw new ArrayIndexOutOfBoundsException ();
35- }
36- this .items [index ] = newVal ;
37- }
38-
39- public void add (int index , AnyType item ) {
40- if (this .size () == this .items .length ) {
41- ensureCapacity (2 *this .size ());
42- }
43- for (int i = this .size ; i > index ; i --) {
44- this .items [i ] = this .items [i -1 ];
45- }
46- this .items [index ] = item ;
47-
48- this .size ++;
49- }
50-
51- public void add (AnyType item ) {
52- this .add (this .size (), item );
53- }
54-
55- public AnyType remove (int index ) {
56- if (index < 0 || index >= this .size ()) {
57- throw new ArrayIndexOutOfBoundsException ();
58- }
59- AnyType removedItem = this .items [index ];
60- for (int i = index ; i < this .size (); i ++) {
61- this .items [i ] = this .items [i +1 ];
62- }
63-
64- this .size --;
65- return removedItem ;
66- }
67-
68- public String toString () {
69- return this .items .toString ();
70- }
71-
72- private void ensureCapacity (int newCapacity ) {
73- if (newCapacity < this .size ) return ;
74-
75- AnyType [] old = this .items ;
76- items = new AnyType [newCapacity ];
77- for (int i = 0 ; i < this .size (); i ++) {
78- items [i ] = old [i ];
79- }
80- }
9+ private AnyType [] items ;
10+
11+ public MyArrayList () {
12+ clear ();
13+ }
14+
15+ public void clear () {
16+ this .size = 0 ;
17+ this .ensureCapacity (DEFAULT_CAPACITY );
18+ }
19+
20+ public int size () {
21+ return this .size ;
22+ }
23+
24+ public AnyType get (int index ) {
25+ if (index < 0 || index >= this .size ()) {
26+ throw new ArrayIndexOutOfBoundsException ();
27+ }
28+ return this .items [index ];
29+ }
30+
31+ public void set (int index , AnyType newVal ) {
32+ if (index < 0 || index >= this .size ()) {
33+ throw new ArrayIndexOutOfBoundsException ();
34+ }
35+ this .items [index ] = newVal ;
36+ }
37+
38+ public void add (int index , AnyType item ) {
39+ if (this .size () == this .items .length ) {
40+ ensureCapacity (2 * this .size ());
41+ }
42+
43+ for (int i = this .size ; i > index ; i --) {
44+ this .items [i ] = this .items [i - 1 ];
45+ }
46+ this .items [index ] = item ;
47+
48+ this .size ++;
49+ }
50+
51+ public void add (AnyType item ) {
52+ this .add (this .size (), item );
53+ }
54+
55+ public AnyType remove (int index ) {
56+ if (index < 0 || index >= this .size ()) {
57+ throw new ArrayIndexOutOfBoundsException ();
58+ }
59+ AnyType removedItem = this .items [index ];
60+ for (int i = index ; i < this .size (); i ++) {
61+ this .items [i ] = this .items [i + 1 ];
62+ }
63+
64+ this .size --;
65+ return removedItem ;
66+ }
67+
68+ @ SuppressWarnings ("unchecked" )
69+ private void ensureCapacity (int newCapacity ) {
70+ if (newCapacity < this .size )
71+ return ;
72+
73+ AnyType [] old = this .items ;
74+ items = (AnyType []) new Object [newCapacity ];
75+ for (int i = 0 ; i < this .size (); i ++) {
76+ items [i ] = old [i ];
77+ }
78+ }
8179}
0 commit comments