mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 03:59:38 +08:00
Add javadoc comments and Cleanup DynamicArray.java
This commit is contained in:
@ -1,70 +1,106 @@
|
||||
package DataStructures.DynamicArray;
|
||||
|
||||
import java.util.Arrays;
|
||||
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;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
|
||||
private int capacity = 10;
|
||||
|
||||
private int size = 0;
|
||||
|
||||
private int capacity;
|
||||
private int size;
|
||||
private Object[] elements;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param capacity the starting length of the desired array
|
||||
*/
|
||||
public DynamicArray(final int capacity) {
|
||||
this.size = 0;
|
||||
this.capacity = capacity;
|
||||
this.elements = new Object[this.capacity];
|
||||
}
|
||||
|
||||
/**
|
||||
* No-args constructor
|
||||
*/
|
||||
public DynamicArray() {
|
||||
this.size = 0;
|
||||
this.capacity = 10;
|
||||
this.elements = new Object[this.capacity];
|
||||
}
|
||||
|
||||
/**
|
||||
* Doubles the capacity of the array
|
||||
* @return int the new capacity of the array
|
||||
*/
|
||||
public int newCapacity() {
|
||||
this.capacity <<= 1;
|
||||
|
||||
this.capacity *= 2;
|
||||
//changed from this.capacity <<= 1; now much easier to understand
|
||||
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) {
|
||||
if (this.size == this.elements.length)
|
||||
this.elements = Arrays.copyOf(this.elements, newCapacity());
|
||||
if (this.size == this.elements.length) {
|
||||
this.elements = Arrays.copyOf(this.elements, newCapacity());
|
||||
}
|
||||
|
||||
this.elements[this.size] = element;
|
||||
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) {
|
||||
// Objects.checkIndex(index, this.size);
|
||||
|
||||
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) {
|
||||
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) {
|
||||
final E oldElement = getElement(index);
|
||||
fastRemove(this.elements, index);
|
||||
|
||||
return oldElement;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
|
||||
/**
|
||||
* get method for size field
|
||||
* @return int size
|
||||
*/
|
||||
public int getSize() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* isEmpty helper method
|
||||
* @return boolean true if the array contains no elements, false otherwise
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return this.size == 0;
|
||||
}
|
||||
|
||||
|
||||
public Stream<E> stream() {
|
||||
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) {
|
||||
final int newSize = this.size - 1;
|
||||
|
||||
if (newSize > index)
|
||||
System.arraycopy(elements, index + 1, elements, index, newSize - index);
|
||||
if (newSize > index) {
|
||||
System.arraycopy(elements, index + 1, elements, index, newSize - index);
|
||||
}
|
||||
|
||||
elements[this.size = newSize] = null;
|
||||
}
|
||||
|
||||
private E getElement(final int index) {
|
||||
// Objects.checkIndex(index, this.size);
|
||||
return (E) this.elements[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a String representation of this object
|
||||
* @return String a String representing the array
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
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
|
||||
public Iterator iterator() {
|
||||
return new DynamicArrayIterator();
|
||||
@ -109,7 +153,6 @@ public class DynamicArray<E> implements Iterable<E> {
|
||||
if (this.cursor > DynamicArray.this.elements.length) throw new ConcurrentModificationException();
|
||||
|
||||
final E element = DynamicArray.this.getElement(this.cursor);
|
||||
|
||||
this.cursor++;
|
||||
|
||||
return element;
|
||||
@ -120,7 +163,6 @@ public class DynamicArray<E> implements Iterable<E> {
|
||||
if (this.cursor < 0) throw new IllegalStateException();
|
||||
|
||||
DynamicArray.this.remove(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) {
|
||||
DynamicArray<String> names = new DynamicArray<>();
|
||||
names.add("Peubes");
|
||||
@ -147,7 +193,7 @@ public class DynamicArray<E> implements Iterable<E> {
|
||||
|
||||
System.out.println(names);
|
||||
|
||||
System.out.println(names.size());
|
||||
System.out.println(names.getSize());
|
||||
|
||||
names.remove(0);
|
||||
|
||||
|
Reference in New Issue
Block a user