From 650b42c43a8baa7519a147e8593aa3c2aaeec384 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Tue, 24 Oct 2023 12:36:12 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20Se?= =?UTF-8?q?tGeneric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PlanesGenericCollection.java | 2 +- ProjectStormtrooper/SetGeneric.java | 61 +++++++++---------- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/ProjectStormtrooper/PlanesGenericCollection.java b/ProjectStormtrooper/PlanesGenericCollection.java index 9142cf1..3b8417a 100644 --- a/ProjectStormtrooper/PlanesGenericCollection.java +++ b/ProjectStormtrooper/PlanesGenericCollection.java @@ -55,7 +55,7 @@ public class PlanesGenericCollection { - private T[] _places; - public int Count; + private final ArrayList _places; + + public int Count() { + return _places.size(); + } + + private final int _maxCount; public SetGeneric(int count) { - _places = (T[]) new DrawingPlane[count]; - Count = count; + _maxCount = count; + _places = new ArrayList<>(count); } public int Insert(T plane) { @@ -15,51 +23,40 @@ public class SetGeneric { public int Insert(T plane, int position) { // Проверка позиции - if (position < 0 || position >= Count) { + if (position < 0 || position >= _maxCount) { return -1; } - // Проверка, что элемент массива по этой позиции пустой - if (_places[position] != null) { - // Проверка, что после вставляемого элемента в массиве есть пустой элемент - int nullIndex = -1; - for (int i = position + 1; i < Count; i++) { - if (_places[i] == null) { - nullIndex = i; - break; - } - } - // Если пустого элемента нет, то выходим - if (nullIndex < 0) { - return -1; - } - // Сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - int j = nullIndex - 1; - while (j >= position) { - _places[j + 1] = _places[j]; - j--; - } - } // Вставка по позиции - _places[position] = plane; + _places.add(position, plane); return position; } public T Remove(int position) { // Проверка позиции - if (position < 0 || position >= Count) { + if (position < 0 || position >= Count()) { return null; } // Удаление объекта из массива, присвоив элементу массива значение null - T plane = _places[position]; - _places[position] = null; + T plane = _places.get(position); + _places.set(position, null); return plane; } public T Get(int position) { // Проверка позиции - if (position < 0 || position >= Count) { + if (position < 0 || position >= Count()) { return null; } - return _places[position]; + return _places.get(position); + } + + public void Set(int position, T plane) { + // Проверка позиции + // Проверка свободных мест в списке + if (position < 0 || position >= _maxCount || Count() == _maxCount) { + return; + } + // Вставка в список по позиции + _places.set(position, plane); } }