forked from pandeyprem/DSA-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray List.java
More file actions
208 lines (175 loc) · 5.07 KB
/
Array List.java
File metadata and controls
208 lines (175 loc) · 5.07 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
public class MyArrayList<AnyType> implements Iterable<AnyType>
{
/**
* Construct an empty ArrayList.
*/
public MyArrayList( )
{
doClear( );
}
/**
* Returns the number of items in this collection.
* @return the number of items in this collection.
*/
public int size( )
{
return theSize;
}
/**
* Returns true if this collection is empty.
* @return true if this collection is empty.
*/
public boolean isEmpty( )
{
return size( ) == 0;
}
/**
* Returns the item at position idx.
* @param idx the index to search in.
* @throws ArrayIndexOutOfBoundsException if index is out of range.
*/
public AnyType get( int idx )
{
if( idx < 0 || idx >= size( ) )
throw new ArrayIndexOutOfBoundsException( "Index " + idx + "; size " + size( ) );
return theItems[ idx ];
}
/**
* Changes the item at position idx.
* @param idx the index to change.
* @param newVal the new value.
* @return the old value.
* @throws ArrayIndexOutOfBoundsException if index is out of range.
*/
public AnyType set( int idx, AnyType newVal )
{
if( idx < 0 || idx >= size( ) )
throw new ArrayIndexOutOfBoundsException( "Index " + idx + "; size " + size( ) );
AnyType old = theItems[ idx ];
theItems[ idx ] = newVal;
return old;
}
@SuppressWarnings("unchecked")
public void ensureCapacity( int newCapacity )
{
if( newCapacity < theSize )
return;
AnyType [ ] old = theItems;
theItems = (AnyType []) new Object[ newCapacity ];
for( int i = 0; i < size( ); i++ )
theItems[ i ] = old[ i ];
}
/**
* Adds an item to this collection, at the end.
* @param x any object.
* @return true.
*/
public boolean add( AnyType x )
{
add( size( ), x );
return true;
}
/**
* Adds an item to this collection, at the specified index.
* @param x any object.
* @return true.
*/
public void add( int idx, AnyType x )
{
if( theItems.length == size( ) )
ensureCapacity( size( ) * 2 + 1 );
for( int i = theSize; i > idx; i-- )
theItems[ i ] = theItems[ i - 1 ];
theItems[ idx ] = x;
theSize++;
}
/**
* Removes an item from this collection.
* @param idx the index of the object.
* @return the item was removed from the collection.
*/
public AnyType remove( int idx )
{
AnyType removedItem = theItems[ idx ];
for( int i = idx; i < size( ) - 1; i++ )
theItems[ i ] = theItems[ i + 1 ];
theSize--;
return removedItem;
}
/**
* Change the size of this collection to zero.
*/
public void clear( )
{
doClear( );
}
private void doClear( )
{
theSize = 0;
ensureCapacity( DEFAULT_CAPACITY );
}
/**
* Obtains an Iterator object used to traverse the collection.
* @return an iterator positioned prior to the first element.
*/
public java.util.Iterator<AnyType> iterator( )
{
return new ArrayListIterator( );
}
/**
* Returns a String representation of this collection.
*/
public String toString( )
{
StringBuilder sb = new StringBuilder( "[ " );
for( AnyType x : this )
sb.append( x + " " );
sb.append( "]" );
return new String( sb );
}
/**
* This is the implementation of the ArrayListIterator.
* It maintains a notion of a current position and of
* course the implicit reference to the MyArrayList.
*/
private class ArrayListIterator implements java.util.Iterator<AnyType>
{
private int current = 0;
private boolean okToRemove = false;
public boolean hasNext( )
{
return current < size( );
}
public AnyType next( )
{
if( !hasNext( ) )
throw new java.util.NoSuchElementException( );
okToRemove = true;
return theItems[ current++ ];
}
public void remove( )
{
if( !okToRemove )
throw new IllegalStateException( );
MyArrayList.this.remove( --current );
okToRemove = false;
}
}
private static final int DEFAULT_CAPACITY = 10;
private AnyType [ ] theItems;
private int theSize;
}
class TestArrayList
{
public static void main( String [ ] args )
{
MyArrayList<Integer> lst = new MyArrayList<>( );
for( int i = 0; i < 10; i++ )
lst.add( i );
for( int i = 20; i < 30; i++ )
lst.add( 0, i );
lst.remove( 0 );
lst.remove( lst.size( ) - 1 );
System.out.println( lst );
}
}