Смена массива на список.

This commit is contained in:
Denis 2022-10-22 16:53:12 +04:00
parent d12fbd832b
commit 35353ac409
2 changed files with 64 additions and 64 deletions

View File

@ -50,13 +50,9 @@
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setAirplane.Count; i++)
foreach (var ship in _setAirplane.GetAirplane())
{
var airplane = _setAirplane.Get(i);
if (airplane != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, airplane);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
return new(_pictureWidth, _pictureHeight);
}
@ -75,11 +71,11 @@
int j = _setAirplane.Count - 1;
for (int i = 0; i < _setAirplane.Count; i++)
{
if (_setAirplane.Get(i) == null)
if (_setAirplane[i] == null)
{
for (; j > i; j--)
{
var airplane = _setAirplane.Get(j);
var airplane = _setAirplane[j];
if (airplane != null)
{
_setAirplane.Insert(airplane, i);
@ -119,11 +115,11 @@
int curWidth = 0;
int curHeight = 0;
for (int i = _setAirplane.Count; i >= 0; i--)
foreach (var airplane in _setAirplane.GetAirplane())
{
_setAirplane.Get(i)?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 80,
airplane?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 80,
curHeight * _placeSizeHeight + 30, _pictureWidth, _pictureHeight);
_setAirplane.Get(i)?.DrawningObject(g);
airplane?.DrawningObject(g);
if (curWidth < widthEl)
curWidth++;

View File

@ -4,72 +4,76 @@
internal class SetAirplaneGeneric<T>
where T : class
{
// Массив объектов, которые храним
private readonly T[] _places;
// Количество объектов в массиве
public int Count => _places.Length;
// Конструктор
public SetAirplaneGeneric(int count)
private readonly List<T> _places;
public int Count => _places.Count;
private readonly int _maxCount;
// Конструктор
public SetAirplaneGeneric(int count)
{
_places = new T[count];
}
_maxCount = count;
_places = new List<T>();
}
// Добавление объекта в набор
public int Insert(T airplane)
{
// вставка в начало набора
return Insert(airplane, 0);
if (_places.Count > _maxCount)
{
return -1;
}
// вставка в начало набора
return Insert(airplane, 0);
}
// Добавление объекта в набор на конкретную позицию
public int Insert(T airplane, int position)
{
// проверка позиции
if (position >= _places.Length || position < 0)
return -1;
//проверка, что элемент массива по этой позиции пустой, если нет, то
if (_places[position] == null)
{
_places[position] = airplane;
return position;
}
//проверка, что после вставляемого элемента в массиве есть пустой элемент
int findEmptyPos = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
findEmptyPos = i;
break;
}
}
if (findEmptyPos < 0) return -1;
//сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
for (int i = findEmptyPos; i > position; i--)
{
_places[i] = _places[i - 1];
}
// вставка по позиции
_places[position] = airplane;
return position;
if (position >= _maxCount || position < 0) return -1;
_places.Insert(position, airplane);
return position;
}
// Удаление объекта из набора с конкретной позиции
public T Remove(int position)
{
// проверка позиции
if (position >= _places.Length || position < 0) return null;
// проверка позиции
if (position >= _maxCount || position < 0) return null;
// удаление объекта из массива, присовив элементу массива значение null
T temp = _places[position];
_places[position] = null;
return temp;
_places.RemoveAt(position);
return temp;
}
// Получение объекта из набора по позиции
public T Get(int position)
{
// проверка позиции
if (position >= _places.Length || position < 0)
return null;
return _places[position];
}
}
// Получение объекта из набора по позиции
public T this[int position]
{
get
{
if (position >= _places.Count || position < 0)
{
return null;
}
return _places[position];
}
set
{
if (position >= _places.Count || position < 0)
{
return;
}
Insert(value, position);
}
}
public IEnumerable<T> GetAirplane()
{
foreach (var airplane in _places)
{
if (airplane != null)
{
yield return airplane;
}
else
{
yield break;
}
}
}
}
}