|
1 | 1 | package input; |
2 | 2 |
|
3 | | -public class CustomCollection implements CollectionInterface{ |
| 3 | +import java.util.Iterator; |
| 4 | +import java.util.Spliterator; |
| 5 | +import java.util.function.Consumer; |
4 | 6 |
|
5 | | - @Override |
6 | | - public boolean add(Object car) { |
7 | | - return false; |
8 | | - } |
| 7 | +public class CustomCollection<T> implements Iterable<String> { |
| 8 | + private static final float GROWTH_FACTOR = 1.5f; |
| 9 | + private static final int DEFAULT_CAPACITY = 10; |
9 | 10 |
|
10 | | - @Override |
11 | | - public boolean remove(Object car) { |
12 | | - return false; |
| 11 | + private Object[] elements; |
| 12 | + private int size; |
| 13 | + |
| 14 | + // Стандартный конструктор |
| 15 | + public CustomCollection() { |
| 16 | + this.elements = new Object[DEFAULT_CAPACITY]; |
13 | 17 | } |
14 | 18 |
|
15 | | - @Override |
16 | | - public boolean removeAt(int index) { |
17 | | - return false; |
| 19 | + // Конструктор с заданной величиной |
| 20 | + public CustomCollection(int initialCapacity) { |
| 21 | + if (initialCapacity < 0) { |
| 22 | + throw new IllegalArgumentException("Capacity cannot be less then 0"); |
| 23 | + } |
| 24 | + this.elements = new Object[initialCapacity]; |
18 | 25 | } |
19 | 26 |
|
20 | | - @Override |
21 | | - public void clear() { |
| 27 | + // TODO Конструктор из существующей коллекции, копирует все элементы с ёмкостью равной ёмкости коллекции |
22 | 28 |
|
| 29 | + public boolean add(Object element) { |
| 30 | + // Проверяем достаточно ли места для добавления |
| 31 | + if (size == elements.length) { |
| 32 | + // Если места не осталось применяем формулу увеличения длинны массива |
| 33 | + int newCapacity = (int) (elements.length * GROWTH_FACTOR + 1); |
| 34 | + // Создаем новый массив с увеличенной длинной |
| 35 | + Object[] newElements = new Object[newCapacity]; |
| 36 | + // Копируем в него элементы из старого массива |
| 37 | + System.arraycopy(elements,0, newElements, 0, elements.length); |
| 38 | + this.elements = newElements; |
| 39 | + } |
| 40 | + // Добавляем новый элемент на последнюю позицию |
| 41 | + elements[size] = element; |
| 42 | + // Увеличиваем число элементов в массиве |
| 43 | + size++; |
| 44 | + return true; |
23 | 45 | } |
24 | 46 |
|
25 | | - @Override |
26 | | - public Object get(int index) { |
27 | | - return null; |
| 47 | + public T get(int index) { |
| 48 | + if(index < 0 || index > size) { |
| 49 | + throw new IndexOutOfBoundsException("Index cannot be less then 0 or more then " + size); |
| 50 | + } |
| 51 | + return (T) elements[index]; |
| 52 | + } |
| 53 | + |
| 54 | + // Проверить почему надо вернуть старый элемент |
| 55 | + public T set(int index, T element) { |
| 56 | + if(index < 0 || index > size) { |
| 57 | + throw new IndexOutOfBoundsException("Index cannot be less then 0 or more then " + size); |
| 58 | + } |
| 59 | + |
| 60 | + T old = (T) elements[index]; |
| 61 | + elements[index] = element; |
| 62 | + return old; |
28 | 63 | } |
29 | 64 |
|
| 65 | + public int size() { return size; } |
| 66 | + |
| 67 | + public boolean isEmpty() { return size == 0; } |
| 68 | + |
30 | 69 | @Override |
31 | | - public boolean add(Object car, int index) { |
32 | | - return false; |
| 70 | + public Iterator iterator() { |
| 71 | + return null; |
33 | 72 | } |
34 | 73 |
|
35 | 74 | @Override |
36 | | - public int size() { |
37 | | - return 0; |
| 75 | + public void forEach(Consumer action) { |
| 76 | + Iterable.super.forEach(action); |
38 | 77 | } |
39 | 78 |
|
40 | 79 | @Override |
41 | | - public boolean contains(Object car) { |
42 | | - return false; |
| 80 | + public Spliterator spliterator() { |
| 81 | + return Iterable.super.spliterator(); |
43 | 82 | } |
44 | 83 | } |
0 commit comments