Переход с массива на список, небольшое изменение некоторых методов, добавлен GetLocomotives

This commit is contained in:
Данила Мочалов 2022-11-05 13:03:36 +04:00
parent 80e51d1ecf
commit 0fb5dbace9
2 changed files with 38 additions and 37 deletions

View File

@ -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
{

View File

@ -1,13 +1,20 @@
import java.util.ArrayList;
public class SetLocomotivesGeneric <T>
{
private final T[] _places;
/// Список хранимых объектов
private final ArrayList<T> _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 <T>
}
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<T> GetLocomotives()
{
/*for (var locomotive : _places)
{
if (locomotive != null)
{
yield return locomotive;
}
else
{
yield break;
}
}*/
return _places;
}
}