diff --git a/src/main/java/MapWithSetArmoredCarsGeneric.java b/src/main/java/MapWithSetArmoredCarsGeneric.java index d1d7c75..25596e7 100644 --- a/src/main/java/MapWithSetArmoredCarsGeneric.java +++ b/src/main/java/MapWithSetArmoredCarsGeneric.java @@ -47,13 +47,9 @@ public class MapWithSetArmoredCarsGeneric get_setCars() { + return _setCars; + } } diff --git a/src/main/java/SetArmoredCarsGeneric.java b/src/main/java/SetArmoredCarsGeneric.java index f342073..aca2008 100644 --- a/src/main/java/SetArmoredCarsGeneric.java +++ b/src/main/java/SetArmoredCarsGeneric.java @@ -1,16 +1,17 @@ -import java.lang.reflect.Array; -import java.util.ArrayList; +import java.util.*; public class SetArmoredCarsGeneric { - private T[] _places; + private ArrayList _places; + private int maxCount; public SetArmoredCarsGeneric(int count) { - _places = (T[]) new Object[count]; + _places = new ArrayList(); + maxCount = count; } public int getCount() { - return _places != null ? _places.length : 0; + return _places != null ? _places.size() : 0; } public int Insert(T armoredCar) @@ -20,47 +21,31 @@ public class SetArmoredCarsGeneric { public int Insert(T armoredCar, int position) { - if (position < 0 || position >= getCount()) + if (getCount() >= maxCount || position < 0 || position >= maxCount) return -1; - if (!(_places[position] == null)) - { - int index_empty = -1; - // поиск первого пустого элемента - for (int i = position + 1; i < getCount(); i++) - { - if (_places[i] == null) - { - index_empty = i; - } - } - if (index_empty == -1) - return -1; - else - { - for (int i = index_empty; i > position; i--) - { - _places[i] = _places[i - 1]; - } - } - } - _places[position] = armoredCar; + _places.add(position, armoredCar); return position; } public T Remove(int position) { - if (position < 0 || position >= getCount()) + if (position < 0 || position >= _places.size()) return null; - T armoredCar = _places[position]; - _places[position] = null; + T armoredCar = _places.get(position); + _places.remove(position); return armoredCar; } public T Get(int position) { - if (position < 0 || position >= getCount()) + if (position < 0 || position >= maxCount) return null; - return _places[position]; + return _places.get(position); + } + + public Iterable GetArmoredCars() + { + return () -> _places.stream().filter(Objects::nonNull).iterator(); } }