This commit is contained in:
Huayang Sun
2021-07-15 22:03:07 +08:00
committed by GitHub
parent 2cf749d653
commit d3a6ba2437

View File

@ -11,206 +11,203 @@ import java.util.stream.StreamSupport;
* @param <E> the type that each index of the array will hold * @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 static final int DEFAULT_CAPACITY = 16;
private int capacity; private int capacity;
private int size; private int size;
private Object[] elements; private Object[] elements;
/** /**
* constructor * constructor
* *
* @param capacity the starting length of the desired array * @param capacity the starting length of the desired array
*/ */
public DynamicArray(final int capacity) { public DynamicArray(final int capacity) {
this.size = 0; 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() { * No-args constructor
this.size = 0; */
this.capacity = 10; public DynamicArray() {
this.elements = new Object[this.capacity]; this(DEFAULT_CAPACITY);
} }
/** /**
* Doubles the capacity of the array * Adds an element to the array If full, creates a copy array twice the size of the current one
* *
* @return int the new capacity of the array * @param element the element of type <E> to be added to the array
*/ */
public int newCapacity() { public void add(final E element) {
this.capacity *= 2; if (this.size == this.elements.length) {
// changed from this.capacity <<= 1; now much easier to understand this.elements = Arrays.copyOf(this.elements, newCapacity(2 * this.capacity));
return this.capacity; }
}
/** this.elements[this.size] = element;
* Adds an element to the array If full, creates a copy array twice the size of the current one size++;
* }
* @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());
}
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) {
this.elements[index] = element;
}
/** /**
* Places element of type <E> at the desired index * get method for element at a given index returns null if the index is empty
* *
* @param index the index for the element to be placed * @param index the desired index of the element
* @param element the element to be inserted * @return <E> the element at the specified index
*/ */
public void put(final int index, E element) { public E get(final int index) {
this.elements[index] = element; return getElement(index);
} }
/** /**
* get method for element at a given index returns null if the index is empty * Removes an element from the array
* *
* @param index the desired index of the element * @param index the index of the element to be removed
* @return <E> the element at the specified index * @return <E> the element removed
*/ */
public E get(final int index) { public E remove(final int index) {
return getElement(index); final E oldElement = getElement(index);
} fastRemove(this.elements, index);
/** if (this.capacity > DEFAULT_CAPACITY && size * 4 <= this.capacity)
* Removes an element from the array this.elements = Arrays.copyOf(this.elements, newCapacity(this.capacity / 2));
* return oldElement;
* @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; /**
} * get method for size field
*
* @return int size
*/
public int getSize() {
return this.size;
}
/** /**
* get method for size field * isEmpty helper method
* *
* @return int size * @return boolean true if the array contains no elements, false otherwise
*/ */
public int getSize() { public boolean isEmpty() {
return this.size; return this.size == 0;
} }
/** public Stream<E> stream() {
* isEmpty helper method return StreamSupport.stream(spliterator(), false);
* }
* @return boolean true if the array contains no elements, false otherwise
*/
public boolean isEmpty() {
return this.size == 0;
}
public Stream<E> stream() { private void fastRemove(final Object[] elements, final int index) {
return StreamSupport.stream(spliterator(), false); final int newSize = this.size - 1;
}
private void fastRemove(final Object[] elements, final int index) { if (newSize > index) {
final int newSize = this.size - 1; System.arraycopy(elements, index + 1, elements, index, newSize - index);
}
if (newSize > index) { elements[this.size = newSize] = null;
System.arraycopy(elements, index + 1, elements, index, newSize - index); }
}
elements[this.size = newSize] = null; private E getElement(final int index) {
} return (E) this.elements[index];
}
private E getElement(final int index) { private int newCapacity(int capacity) {
return (E) this.elements[index]; this.capacity = capacity;
} return this.capacity;
}
/** /**
* returns a String representation of this object * returns a String representation of this object
* *
* @return String a String representing the array * @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 * Creates and returns a new Dynamic Array Iterator
* *
* @return Iterator a Dynamic Array Iterator * @return Iterator a Dynamic Array Iterator
*/ */
@Override @Override
public Iterator iterator() { public Iterator iterator() {
return new DynamicArrayIterator(); return new DynamicArrayIterator();
} }
private class DynamicArrayIterator implements Iterator<E> { private class DynamicArrayIterator implements Iterator<E> {
private int cursor; private int cursor;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return this.cursor != size; return this.cursor != size;
} }
@Override @Override
public E next() { public E next() {
if (this.cursor > DynamicArray.this.size) throw new NoSuchElementException(); if (this.cursor > DynamicArray.this.size) throw new NoSuchElementException();
if (this.cursor > DynamicArray.this.elements.length) if (this.cursor > DynamicArray.this.elements.length)
throw new ConcurrentModificationException(); 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;
} }
@Override @Override
public void remove() { public void remove() {
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--;
} }
@Override @Override
public void forEachRemaining(Consumer<? super E> action) { public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action); Objects.requireNonNull(action);
for (int i = 0; i < DynamicArray.this.size; i++) { for (int i = 0; i < DynamicArray.this.size; i++) {
action.accept(DynamicArray.this.getElement(i)); action.accept(DynamicArray.this.getElement(i));
} }
} }
} }
/** /**
* This class is the driver for the DynamicArray<E> class it tests a variety of methods and prints * This class is the driver for the DynamicArray<E> class it tests a variety of methods and prints
* the output * 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");
names.add("Marley"); names.add("Marley");
for (String name : names) { for (String name : names) {
System.out.println(name); System.out.println(name);
} }
names.stream().forEach(System.out::println); names.stream().forEach(System.out::println);
System.out.println(names); System.out.println(names);
System.out.println(names.getSize()); System.out.println(names.getSize());
names.remove(0); names.remove(0);
for (String name : names) { for (String name : names) {
System.out.println(name); System.out.println(name);
} }
} }
} }