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+ * Removes an element from the array
78+ * @param index the index of the element to be removed
79+ * @return <E> the element removed
80+ */
5381 public E remove (final int index ) {
5482 final E oldElement = getElement (index );
5583 fastRemove (this .elements , index );
5684
5785 return oldElement ;
5886 }
59-
60- public int size () {
87+
88+ /**
89+ * get method for size field
90+ * @return int size
91+ */
92+ public int getSize () {
6193 return this .size ;
6294 }
6395
96+ /**
97+ * isEmpty helper method
98+ * @return boolean true if the array contains no elements, false otherwise
99+ */
64100 public boolean isEmpty () {
65101 return this .size == 0 ;
66102 }
67-
103+
68104 public Stream <E > stream () {
69105 return StreamSupport .stream (spliterator (), false );
70106 }
71107
72108 private void fastRemove (final Object [] elements , final int index ) {
73109 final int newSize = this .size - 1 ;
74110
75- if (newSize > index )
76- System .arraycopy (elements , index + 1 , elements , index , newSize - index );
111+ if (newSize > index ) {
112+ System .arraycopy (elements , index + 1 , elements , index , newSize - index );
113+ }
77114
78115 elements [this .size = newSize ] = null ;
79116 }
80117
81118 private E getElement (final int index ) {
82- // Objects.checkIndex(index, this.size);
83119 return (E ) this .elements [index ];
84120 }
85121
122+ /**
123+ * returns a String representation of this object
124+ * @return String a String representing the array
125+ */
86126 @ Override
87127 public String toString () {
88128 return Arrays .toString (Arrays .stream (this .elements ).filter (Objects ::nonNull ).toArray ());
89129 }
90130
131+ /**
132+ * Creates and returns a new Dynamic Array Iterator
133+ * @return Iterator a Dynamic Array Iterator
134+ */
91135 @ Override
92136 public Iterator iterator () {
93137 return new DynamicArrayIterator ();
@@ -109,7 +153,6 @@ public E next() {
109153 if (this .cursor > DynamicArray .this .elements .length ) throw new ConcurrentModificationException ();
110154
111155 final E element = DynamicArray .this .getElement (this .cursor );
112-
113156 this .cursor ++;
114157
115158 return element ;
@@ -120,7 +163,6 @@ public void remove() {
120163 if (this .cursor < 0 ) throw new IllegalStateException ();
121164
122165 DynamicArray .this .remove (this .cursor );
123-
124166 this .cursor --;
125167 }
126168
@@ -134,6 +176,10 @@ public void forEachRemaining(Consumer<? super E> action) {
134176 }
135177 }
136178
179+ /**
180+ * This class is the driver for the DynamicArray<E> class
181+ * it tests a variety of methods and prints the output
182+ */
137183 public static void main (String [] args ) {
138184 DynamicArray <String > names = new DynamicArray <>();
139185 names .add ("Peubes" );
@@ -147,7 +193,7 @@ public static void main(String[] args) {
147193
148194 System .out .println (names );
149195
150- System .out .println (names .size ());
196+ System .out .println (names .getSize ());
151197
152198 names .remove (0 );
153199
0 commit comments