Add javadoc comments and Cleanup DynamicArray.java

This commit is contained in:
Ray
2020-08-06 22:32:49 -04:00
parent 4be90d8afa
commit 851df781dd

View File

@ -1,70 +1,106 @@
package DataStructures.DynamicArray; package DataStructures.DynamicArray;
import java.util.Arrays; import java.util.*;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* This class implements a dynamic array
* @param <E> the type that each index of the array will hold
*/
public class DynamicArray<E> implements Iterable<E> { public class DynamicArray<E> implements Iterable<E> {
private int capacity = 10; private int capacity;
private int size;
private int size = 0;
private Object[] elements; private Object[] elements;
/**
* constructor
* @param capacity the starting length of the desired array
*/
public DynamicArray(final int capacity) { public DynamicArray(final int capacity) {
this.size = 0;
this.capacity = capacity; this.capacity = capacity;
this.elements = new Object[this.capacity]; this.elements = new Object[this.capacity];
} }
/**
* No-args constructor
*/
public DynamicArray() { public DynamicArray() {
this.size = 0;
this.capacity = 10;
this.elements = new Object[this.capacity]; this.elements = new Object[this.capacity];
} }
/**
* Doubles the capacity of the array
* @return int the new capacity of the array
*/
public int newCapacity() { public int newCapacity() {
this.capacity <<= 1; this.capacity *= 2;
//changed from this.capacity <<= 1; now much easier to understand
return this.capacity; return this.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 <E> to be added to the array
*/
public void add(final E element) { public void add(final E element) {
if (this.size == this.elements.length) if (this.size == this.elements.length) {
this.elements = Arrays.copyOf(this.elements, newCapacity()); this.elements = Arrays.copyOf(this.elements, newCapacity());
}
this.elements[this.size] = element; this.elements[this.size] = element;
size++; size++;
} }
/**
* Places element of type <E> at the desired index
* @param index the index for the element to be placed
* @param element the element to be inserted
*/
public void put(final int index, E element) { public void put(final int index, E element) {
// Objects.checkIndex(index, this.size);
this.elements[index] = 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 <E> the element at the specified index
*/
public E get(final int index) { public E get(final int index) {
return getElement(index); return getElement(index);
} }
/**
* Removes an element from the array
* @param index the index of the element to be removed
* @return <E> the element removed
*/
public E remove(final int index) { public E remove(final int index) {
final E oldElement = getElement(index); final E oldElement = getElement(index);
fastRemove(this.elements, index); fastRemove(this.elements, index);
return oldElement; return oldElement;
} }
public int size() { /**
* get method for size field
* @return int size
*/
public int getSize() {
return this.size; return this.size;
} }
/**
* isEmpty helper method
* @return boolean true if the array contains no elements, false otherwise
*/
public boolean isEmpty() { public boolean isEmpty() {
return this.size == 0; return this.size == 0;
} }
public Stream<E> stream() { public Stream<E> stream() {
return StreamSupport.stream(spliterator(), false); return StreamSupport.stream(spliterator(), false);
} }
@ -72,22 +108,30 @@ public class DynamicArray<E> implements Iterable<E> {
private void fastRemove(final Object[] elements, final int index) { private void fastRemove(final Object[] elements, final int index) {
final int newSize = this.size - 1; final int newSize = this.size - 1;
if (newSize > index) if (newSize > index) {
System.arraycopy(elements, index + 1, elements, index, newSize - index); System.arraycopy(elements, index + 1, elements, index, newSize - index);
}
elements[this.size = newSize] = null; elements[this.size = newSize] = null;
} }
private E getElement(final int index) { private E getElement(final int index) {
// Objects.checkIndex(index, this.size);
return (E) this.elements[index]; return (E) this.elements[index];
} }
/**
* returns a String representation of this object
* @return String a String representing the array
*/
@Override @Override
public String toString() { public String toString() {
return Arrays.toString(Arrays.stream(this.elements).filter(Objects::nonNull).toArray()); return Arrays.toString(Arrays.stream(this.elements).filter(Objects::nonNull).toArray());
} }
/**
* Creates and returns a new Dynamic Array Iterator
* @return Iterator a Dynamic Array Iterator
*/
@Override @Override
public Iterator iterator() { public Iterator iterator() {
return new DynamicArrayIterator(); return new DynamicArrayIterator();
@ -109,7 +153,6 @@ public class DynamicArray<E> implements Iterable<E> {
if (this.cursor > DynamicArray.this.elements.length) throw new ConcurrentModificationException(); if (this.cursor > DynamicArray.this.elements.length) throw new ConcurrentModificationException();
final E element = DynamicArray.this.getElement(this.cursor); final E element = DynamicArray.this.getElement(this.cursor);
this.cursor++; this.cursor++;
return element; return element;
@ -120,7 +163,6 @@ public class DynamicArray<E> implements Iterable<E> {
if (this.cursor < 0) throw new IllegalStateException(); if (this.cursor < 0) throw new IllegalStateException();
DynamicArray.this.remove(this.cursor); DynamicArray.this.remove(this.cursor);
this.cursor--; this.cursor--;
} }
@ -134,6 +176,10 @@ public class DynamicArray<E> implements Iterable<E> {
} }
} }
/**
* This class is the driver for the DynamicArray<E> class
* it tests a variety of methods and prints the output
*/
public static void main(String[] args) { public static void main(String[] args) {
DynamicArray<String> names = new DynamicArray<>(); DynamicArray<String> names = new DynamicArray<>();
names.add("Peubes"); names.add("Peubes");
@ -147,7 +193,7 @@ public class DynamicArray<E> implements Iterable<E> {
System.out.println(names); System.out.println(names);
System.out.println(names.size()); System.out.println(names.getSize());
names.remove(0); names.remove(0);