Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 76 additions & 29 deletions DataStructures/DynamicArray/DynamicArray.java
Original file line number Diff line number Diff line change
@@ -1,93 +1,138 @@
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);
}

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();
Expand All @@ -109,7 +154,6 @@ public E next() {
if (this.cursor > DynamicArray.this.elements.length) throw new ConcurrentModificationException();

final E element = DynamicArray.this.getElement(this.cursor);

this.cursor++;

return element;
Expand All @@ -120,7 +164,6 @@ public void remove() {
if (this.cursor < 0) throw new IllegalStateException();

DynamicArray.this.remove(this.cursor);

this.cursor--;
}

Expand All @@ -134,6 +177,10 @@ public void forEachRemaining(Consumer<? super E> action) {
}
}

/**
* 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");
Expand All @@ -147,7 +194,7 @@ public static void main(String[] args) {

System.out.println(names);

System.out.println(names.size());
System.out.println(names.getSize());

names.remove(0);

Expand Down