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; + } }