11package DataStructures .DynamicArray ;
22
3- import java .util .Arrays ;
4- import java .util .ConcurrentModificationException ;
5- import java .util .Iterator ;
6- import java .util .NoSuchElementException ;
7- import java .util .Objects ;
8- import java .util .function .Consumer ;
9- import java .util .stream .Stream ;
10- import java .util .stream .StreamSupport ;
3+ import java .util .*;
114
5+ /**
6+ * This class implements a dynamic array
7+ * @param <E> the type that each index of the array will hold
8+ */
129public class DynamicArray <E > implements Iterable <E > {
1310
14- private int capacity = 10 ;
15-
16- private int size = 0 ;
17-
11+ private int capacity ;
12+ private int size ;
1813 private Object [] elements ;
1914
15+ /**
16+ * constructor
17+ * @param capacity the starting length of the desired array
18+ */
2019 public DynamicArray (final int capacity ) {
20+ this .size = 0 ;
2121 this .capacity = capacity ;
2222 this .elements = new Object [this .capacity ];
2323 }
2424
25+ /**
26+ * No-args constructor
27+ */
2528 public DynamicArray () {
29+ this .size = 0 ;
30+ this .capacity = 10 ;
2631 this .elements = new Object [this .capacity ];
2732 }
2833
34+ /**
35+ * Doubles the capacity of the array
36+ * @return int the new capacity of the array
37+ */
2938 public int newCapacity () {
30- this .capacity <<= 1 ;
31-
39+ this .capacity *= 2 ;
40+ //changed from this.capacity <<= 1; now much easier to understand
3241 return this .capacity ;
3342 }
3443
44+ /**
45+ * Adds an element to the array
46+ * If full, creates a copy array twice the size of the current one
47+ * @param element the element of type <E> to be added to the array
48+ */
3549 public void add (final E element ) {
36- if (this .size == this .elements .length )
37- this .elements = Arrays .copyOf (this .elements , newCapacity ());
50+ if (this .size == this .elements .length ) {
51+ this .elements = Arrays .copyOf (this .elements , newCapacity ());
52+ }
3853
3954 this .elements [this .size ] = element ;
4055 size ++;
4156 }
42-
57+
58+ /**
59+ * Places element of type <E> at the desired index
60+ * @param index the index for the element to be placed
61+ * @param element the element to be inserted
62+ */
4363 public void put (final int index , E element ) {
44- // Objects.checkIndex(index, this.size);
45-
4664 this .elements [index ] = element ;
4765 }
4866
67+ /**
68+ * get method for element at a given index
69+ * returns null if the index is empty
70+ * @param index the desired index of the element
71+ * @return <E> the element at the specified index
72+ */
4973 public E get (final int index ) {
5074 return getElement (index );
5175 }
52-
76+
77+ /**
78+ * Removes an element from the array
79+ * @param index the index of the element to be removed
80+ * @return <E> the element removed
81+ */
5382 public E remove (final int index ) {
5483 final E oldElement = getElement (index );
5584 fastRemove (this .elements , index );
5685
5786 return oldElement ;
5887 }
59-
60- public int size () {
88+
89+ /**
90+ * get method for size field
91+ * @return int size
92+ */
93+ public int getSize () {
6194 return this .size ;
6295 }
6396
97+ /**
98+ * isEmpty helper method
99+ * @return boolean true if the array contains no elements, false otherwise
100+ */
64101 public boolean isEmpty () {
65102 return this .size == 0 ;
66103 }
67-
104+
68105 public Stream <E > stream () {
69106 return StreamSupport .stream (spliterator (), false );
70107 }
71108
72109 private void fastRemove (final Object [] elements , final int index ) {
73110 final int newSize = this .size - 1 ;
74111
75- if (newSize > index )
76- System .arraycopy (elements , index + 1 , elements , index , newSize - index );
112+ if (newSize > index ) {
113+ System .arraycopy (elements , index + 1 , elements , index , newSize - index );
114+ }
77115
78116 elements [this .size = newSize ] = null ;
79117 }
80118
81119 private E getElement (final int index ) {
82- // Objects.checkIndex(index, this.size);
83120 return (E ) this .elements [index ];
84121 }
85122
123+ /**
124+ * returns a String representation of this object
125+ * @return String a String representing the array
126+ */
86127 @ Override
87128 public String toString () {
88129 return Arrays .toString (Arrays .stream (this .elements ).filter (Objects ::nonNull ).toArray ());
89130 }
90131
132+ /**
133+ * Creates and returns a new Dynamic Array Iterator
134+ * @return Iterator a Dynamic Array Iterator
135+ */
91136 @ Override
92137 public Iterator iterator () {
93138 return new DynamicArrayIterator ();
@@ -109,7 +154,6 @@ public E next() {
109154 if (this .cursor > DynamicArray .this .elements .length ) throw new ConcurrentModificationException ();
110155
111156 final E element = DynamicArray .this .getElement (this .cursor );
112-
113157 this .cursor ++;
114158
115159 return element ;
@@ -120,7 +164,6 @@ public void remove() {
120164 if (this .cursor < 0 ) throw new IllegalStateException ();
121165
122166 DynamicArray .this .remove (this .cursor );
123-
124167 this .cursor --;
125168 }
126169
@@ -134,6 +177,10 @@ public void forEachRemaining(Consumer<? super E> action) {
134177 }
135178 }
136179
180+ /**
181+ * This class is the driver for the DynamicArray<E> class
182+ * it tests a variety of methods and prints the output
183+ */
137184 public static void main (String [] args ) {
138185 DynamicArray <String > names = new DynamicArray <>();
139186 names .add ("Peubes" );
@@ -147,7 +194,7 @@ public static void main(String[] args) {
147194
148195 System .out .println (names );
149196
150- System .out .println (names .size ());
197+ System .out .println (names .getSize ());
151198
152199 names .remove (0 );
153200
0 commit comments