|
| 1 | +package DataStructures.DynamicArray; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.ConcurrentModificationException; |
| 5 | +import java.util.Iterator; |
| 6 | +import java.util.NoSuchElementException; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.function.Consumer; |
| 9 | +import java.util.stream.Stream; |
| 10 | +import java.util.stream.StreamSupport; |
| 11 | + |
| 12 | +public class DynamicArray<E> implements Iterable<E> { |
| 13 | + |
| 14 | + private int capacity = 10; |
| 15 | + |
| 16 | + private int size = 0; |
| 17 | + |
| 18 | + private Object[] elements; |
| 19 | + |
| 20 | + public DynamicArray(final int capacity) { |
| 21 | + this.capacity = capacity; |
| 22 | + this.elements = new Object[this.capacity]; |
| 23 | + } |
| 24 | + |
| 25 | + public DynamicArray() { |
| 26 | + this.elements = new Object[this.capacity]; |
| 27 | + } |
| 28 | + |
| 29 | + public int newCapacity() { |
| 30 | + this.capacity <<= 1; |
| 31 | + |
| 32 | + return this.capacity; |
| 33 | + } |
| 34 | + |
| 35 | + public void add(final E element) { |
| 36 | + if (this.size == this.elements.length) |
| 37 | + this.elements = Arrays.copyOf(this.elements, newCapacity()); |
| 38 | + |
| 39 | + this.elements[this.size] = element; |
| 40 | + size++; |
| 41 | + } |
| 42 | + |
| 43 | + public void put(final int index, E element) { |
| 44 | + Objects.checkIndex(index, this.size); |
| 45 | + |
| 46 | + this.elements[index] = element; |
| 47 | + } |
| 48 | + |
| 49 | + public E get(final int index) { |
| 50 | + return getElement(index); |
| 51 | + } |
| 52 | + |
| 53 | + public E remove(final int index) { |
| 54 | + final E oldElement = getElement(index); |
| 55 | + fastRemove(this.elements, index); |
| 56 | + |
| 57 | + return oldElement; |
| 58 | + } |
| 59 | + |
| 60 | + public int size() { |
| 61 | + return this.size; |
| 62 | + } |
| 63 | + |
| 64 | + public boolean isEmpty() { |
| 65 | + return this.size == 0; |
| 66 | + } |
| 67 | + |
| 68 | + public Stream<E> stream() { |
| 69 | + return StreamSupport.stream(spliterator(), false); |
| 70 | + } |
| 71 | + |
| 72 | + private void fastRemove(final Object[] elements, final int index) { |
| 73 | + final int newSize = this.size - 1; |
| 74 | + |
| 75 | + if (newSize > index) |
| 76 | + System.arraycopy(elements, index + 1, elements, index, newSize - index); |
| 77 | + |
| 78 | + elements[this.size = newSize] = null; |
| 79 | + } |
| 80 | + |
| 81 | + private E getElement(final int index) { |
| 82 | + Objects.checkIndex(index, this.size); |
| 83 | + return (E) this.elements[index]; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String toString() { |
| 88 | + return Arrays.toString(Arrays.stream(this.elements).filter(Objects::nonNull).toArray()); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Iterator iterator() { |
| 93 | + return new DynamicArrayIterator(); |
| 94 | + } |
| 95 | + |
| 96 | + private class DynamicArrayIterator implements Iterator<E> { |
| 97 | + |
| 98 | + private int cursor; |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean hasNext() { |
| 102 | + return this.cursor != size; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public E next() { |
| 107 | + if (this.cursor > DynamicArray.this.size) throw new NoSuchElementException(); |
| 108 | + |
| 109 | + if (this.cursor > DynamicArray.this.elements.length) throw new ConcurrentModificationException(); |
| 110 | + |
| 111 | + final E element = DynamicArray.this.getElement(this.cursor); |
| 112 | + |
| 113 | + this.cursor++; |
| 114 | + |
| 115 | + return element; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void remove() { |
| 120 | + if (this.cursor < 0) throw new IllegalStateException(); |
| 121 | + |
| 122 | + DynamicArray.this.remove(this.cursor); |
| 123 | + |
| 124 | + this.cursor--; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void forEachRemaining(Consumer<? super E> action) { |
| 129 | + Objects.requireNonNull(action); |
| 130 | + |
| 131 | + for (int i = 0; i < DynamicArray.this.size; i++) { |
| 132 | + action.accept(DynamicArray.this.getElement(i)); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public static void main(String[] args) { |
| 138 | + DynamicArray<String> names = new DynamicArray<>(); |
| 139 | + names.add("Peubes"); |
| 140 | + names.add("Marley"); |
| 141 | + |
| 142 | + for (String name : names) { |
| 143 | + System.out.println(name); |
| 144 | + } |
| 145 | + |
| 146 | + names.stream().forEach(System.out::println); |
| 147 | + |
| 148 | + System.out.println(names); |
| 149 | + |
| 150 | + System.out.println(names.size()); |
| 151 | + |
| 152 | + names.remove(0); |
| 153 | + |
| 154 | + for (String name : names) { |
| 155 | + System.out.println(name); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments