mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
@ -11,6 +11,7 @@ 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;
|
||||||
@ -27,22 +28,11 @@ public class DynamicArray<E> implements Iterable<E> {
|
|||||||
this.elements = new Object[this.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
|
* No-args constructor
|
||||||
*
|
|
||||||
* @return int the new capacity of the array
|
|
||||||
*/
|
*/
|
||||||
public int newCapacity() {
|
public DynamicArray() {
|
||||||
this.capacity *= 2;
|
this(DEFAULT_CAPACITY);
|
||||||
// changed from this.capacity <<= 1; now much easier to understand
|
|
||||||
return this.capacity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,7 +42,7 @@ public class DynamicArray<E> implements Iterable<E> {
|
|||||||
*/
|
*/
|
||||||
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(2 * this.capacity));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.elements[this.size] = element;
|
this.elements[this.size] = element;
|
||||||
@ -89,6 +79,8 @@ public class DynamicArray<E> implements Iterable<E> {
|
|||||||
final E oldElement = getElement(index);
|
final E oldElement = getElement(index);
|
||||||
fastRemove(this.elements, 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;
|
return oldElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,6 +120,11 @@ public class DynamicArray<E> implements Iterable<E> {
|
|||||||
return (E) this.elements[index];
|
return (E) this.elements[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int newCapacity(int capacity) {
|
||||||
|
this.capacity = capacity;
|
||||||
|
return this.capacity;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns a String representation of this object
|
* returns a String representation of this object
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user