Skip to content

Commit 14ff3e7

Browse files
author
Никита
committed
Prepared the basis for a custom collection with basic methods and constructors.
1 parent 7d7a977 commit 14ff3e7

3 files changed

Lines changed: 63 additions & 24 deletions

File tree

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,83 @@
11
package input;
22

3-
public class CustomCollection implements CollectionInterface{
3+
import java.util.Iterator;
4+
import java.util.Spliterator;
5+
import java.util.function.Consumer;
46

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;
910

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];
1317
}
1418

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];
1825
}
1926

20-
@Override
21-
public void clear() {
27+
// TODO Конструктор из существующей коллекции, копирует все элементы с ёмкостью равной ёмкости коллекции
2228

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;
2345
}
2446

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;
2863
}
2964

65+
public int size() { return size; }
66+
67+
public boolean isEmpty() { return size == 0; }
68+
3069
@Override
31-
public boolean add(Object car, int index) {
32-
return false;
70+
public Iterator iterator() {
71+
return null;
3372
}
3473

3574
@Override
36-
public int size() {
37-
return 0;
75+
public void forEach(Consumer action) {
76+
Iterable.super.forEach(action);
3877
}
3978

4079
@Override
41-
public boolean contains(Object car) {
42-
return false;
80+
public Spliterator spliterator() {
81+
return Iterable.super.spliterator();
4382
}
4483
}

0 commit comments

Comments
 (0)