From 8aa6989be23e4e937ae1446a324f8723bdd34e42 Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Sun, 6 Nov 2022 22:25:36 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B0=D1=81=D1=81=D0=B8=D0=B2=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=20=D0=BD=D0=B0=20=D1=81?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/MapWithSetArmoredCarsGeneric.java | 22 ++++---- src/main/java/SetArmoredCarsGeneric.java | 51 +++++++------------ 2 files changed, 29 insertions(+), 44 deletions(-) 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(); } }