From 0fb5dbace9910ee8864eb349eec31a5979258d13 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 5 Nov 2022 13:03:36 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4=20?= =?UTF-8?q?=D1=81=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA,=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=B1=D0=BE=D0=BB=D1=8C=D1=88=D0=BE=D0=B5=20=D0=B8=D0=B7=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B5=D0=BA=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D1=8B=D1=85=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= =?UTF-8?q?=D0=BE=D0=B2,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20GetLocomotives?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MapWithSetLocomotivesGeneric.java | 9 ++--- SetLocomotivesGeneric.java | 66 ++++++++++++++++--------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java index d6bd589..699eb1d 100644 --- a/MapWithSetLocomotivesGeneric.java +++ b/MapWithSetLocomotivesGeneric.java @@ -52,9 +52,8 @@ public class MapWithSetLocomotivesGeneric public BufferedImage ShowOnMap() { Shaking(); - for (int i = 0; i < _setLocomotives.Count(); i++) + for (var locomotive : _setLocomotives.GetLocomotives()) { - var locomotive = _setLocomotives.Get(i); if (locomotive != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive); @@ -151,11 +150,11 @@ public class MapWithSetLocomotivesGeneric int curWidth = 0; int curHeight = 0; - for (int i = 0; i < _setLocomotives.Count(); i++) + for (var locomotive : _setLocomotives.GetLocomotives()) { // установка позиции - if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight); - if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).DrawningObject(g); + if (locomotive != null) locomotive.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight); + if (locomotive != null) locomotive.DrawningObject(g); if (curWidth < width) curWidth++; else { diff --git a/SetLocomotivesGeneric.java b/SetLocomotivesGeneric.java index 7256512..4a8a13f 100644 --- a/SetLocomotivesGeneric.java +++ b/SetLocomotivesGeneric.java @@ -1,13 +1,20 @@ +import java.util.ArrayList; + public class SetLocomotivesGeneric { - private final T[] _places; + /// Список хранимых объектов + private final ArrayList _places; public int Count() { - return _places.length; + return _places.size(); } + // Ограничение на количество + private final int _maxCount; + public SetLocomotivesGeneric(int count) { - _places = (T[]) new Object[count]; + _maxCount = count; + _places = new ArrayList<>(); } public int Insert (T locomotive) { @@ -15,47 +22,42 @@ public class SetLocomotivesGeneric } public int Insert (T locomotive, int position) { - if (position >= _places.length || position < 0) return -1; - if (_places[position] == null) { - _places[position] = locomotive; - return position; - } - - int emptyEl = -1; - for (int i = position + 1; i < Count(); i++) - { - if (_places[i] == null) - { - emptyEl = i; - break; - } - } - if (emptyEl == -1) - { - return -1; - } - for (int i = emptyEl; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = locomotive; + if (position >= _maxCount|| position < 0) return -1; + _places.add(position, locomotive); return position; } public T Remove (int position) { - if (position >= _places.length || position < 0) return null; - T result = _places[position]; - _places[position] = null; + if (position >= _maxCount || position < 0) return null; + T result = _places.get(position); + _places.remove(position); return result; } public T Get(int position) { - if (position >= _places.length || position < 0) + if (position >= _maxCount || position < 0) { return null; } - return _places[position]; + return _places.get(position); } + + /// Проход по набору до первого пустого + public Iterable GetLocomotives() + { + /*for (var locomotive : _places) + { + if (locomotive != null) + { + yield return locomotive; + } + else + { + yield break; + } + }*/ + return _places; + } }