packagecom.thealgorithms.datastructures.dynamicarray;importjava.util.*;importjava.util.function.Consumer;importjava.util.stream.Stream;importjava.util.stream.StreamSupport;/**
* This class implements a dynamic array
*
* @param the type that each index of the array will hold
*/publicclassDynamicArray<E>implementsIterable<E>{privatestaticfinalint DEFAULT_CAPACITY =16;privateint capacity;privateint size;privateObject[] elements;/**
* constructor
*
* @param capacity the starting length of the desired array
*/publicDynamicArray(finalint capacity){this.size =0;this.capacity = capacity;this.elements =newObject[this.capacity];}/**
* No-args constructor
*/publicDynamicArray(){this(DEFAULT_CAPACITY);}/**
* Adds an element to the array If full, creates a copy array twice the size
* of the current one
*
* @param element the element of type to be added to the array
*/publicvoidadd(finalE element){if(this.size ==this.elements.length){this.elements =Arrays.copyOf(this.elements,newCapacity(2*this.capacity));}this.elements[this.size]= element;
size++;}/**
* Places element of type at the desired index
*
* @param index the index for the element to be placed
* @param element the element to be inserted
*/publicvoidput(finalint index,E element){this.elements[index]= element;}/**
* get method for element at a given index returns null if the index is
* empty
*
* @param index the desired index of the element
* @return the element at the specified index
*/publicEget(finalint index){returngetElement(index);}/**
* Removes an element from the array
*
* @param index the index of the element to be removed
* @return the element removed
*/publicEremove(finalint index){finalE oldElement =getElement(index);fastRemove(this.elements, index);if(this.capacity > DEFAULT_CAPACITY && size *4<=this.capacity){this.elements =Arrays.copyOf(this.elements,newCapacity(this.capacity /2));}return oldElement;}/**
* get method for size field
*
* @return int size
*/publicintgetSize(){returnthis.size;}/**
* isEmpty helper method
*
* @return boolean true if the array contains no elements, false otherwise
*/publicbooleanisEmpty(){returnthis.size ==0;}publicStream<E>stream(){returnStreamSupport.stream(spliterator(),false);}privatevoidfastRemove(finalObject[] elements,finalint index){finalint newSize =this.size -1;if(newSize > index){System.arraycopy(elements, index +1, elements, index, newSize - index);}
elements[this.size = newSize]=null;}privateEgetElement(finalint index){return(E)this.elements[index];}privateintnewCapacity(int capacity){this.capacity = capacity;returnthis.capacity;}/**
* returns a String representation of this object
*
* @return String a String representing the array
*/@OverridepublicStringtoString(){returnArrays.toString(Arrays.stream(this.elements).filter(Objects::nonNull).toArray());}/**
* Creates and returns a new Dynamic Array Iterator
*
* @return Iterator a Dynamic Array Iterator
*/@OverridepublicIteratoriterator(){returnnewDynamicArrayIterator();}privateclassDynamicArrayIteratorimplementsIterator<E>{privateint cursor;@OverridepublicbooleanhasNext(){returnthis.cursor != size;}@OverridepublicEnext(){if(this.cursor >DynamicArray.this.size){thrownewNoSuchElementException();}if(this.cursor >DynamicArray.this.elements.length){thrownewConcurrentModificationException();}finalE element =DynamicArray.this.getElement(this.cursor);this.cursor++;return element;}@Overridepublicvoidremove(){if(this.cursor <0){thrownewIllegalStateException();}DynamicArray.this.remove(this.cursor);this.cursor--;}@OverridepublicvoidforEachRemaining(Consumer<?superE> action){Objects.requireNonNull(action);for(int i =0; i <DynamicArray.this.size; i++){
action.accept(DynamicArray.this.getElement(i));}}}/**
* This class is the driver for the DynamicArray class it tests a variety
* of methods and prints the output
*/publicstaticvoidmain(String[] args){DynamicArray<String> names =newDynamicArray<>();
names.add("Peubes");
names.add("Marley");for(String name : names){System.out.println(name);}
names.stream().forEach(System.out::println);System.out.println(names);System.out.println(names.getSize());
names.remove(0);for(String name : names){System.out.println(name);}}}