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

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

View File

@ -4,72 +4,76 @@
internal class SetAirplaneGeneric<T> internal class SetAirplaneGeneric<T>
where T : class where T : class
{ {
// Массив объектов, которые храним private readonly List<T> _places;
private readonly T[] _places; public int Count => _places.Count;
// Количество объектов в массиве private readonly int _maxCount;
public int Count => _places.Length; // Конструктор
// Конструктор public SetAirplaneGeneric(int count)
public SetAirplaneGeneric(int count)
{ {
_places = new T[count]; _maxCount = count;
} _places = new List<T>();
}
// Добавление объекта в набор // Добавление объекта в набор
public int Insert(T airplane) public int Insert(T airplane)
{ {
// вставка в начало набора if (_places.Count > _maxCount)
return Insert(airplane, 0); {
return -1;
}
// вставка в начало набора
return Insert(airplane, 0);
} }
// Добавление объекта в набор на конкретную позицию // Добавление объекта в набор на конкретную позицию
public int Insert(T airplane, int position) public int Insert(T airplane, int position)
{ {
// проверка позиции if (position >= _maxCount || position < 0) return -1;
if (position >= _places.Length || position < 0) _places.Insert(position, airplane);
return -1; return position;
//проверка, что элемент массива по этой позиции пустой, если нет, то
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;
} }
// Удаление объекта из набора с конкретной позиции // Удаление объекта из набора с конкретной позиции
public T Remove(int position) public T Remove(int position)
{ {
// проверка позиции // проверка позиции
if (position >= _places.Length || position < 0) return null; if (position >= _maxCount || position < 0) return null;
// удаление объекта из массива, присовив элементу массива значение null // удаление объекта из массива, присовив элементу массива значение null
T temp = _places[position]; T temp = _places[position];
_places[position] = null; _places.RemoveAt(position);
return temp; return temp;
} }
// Получение объекта из набора по позиции // Получение объекта из набора по позиции
public T Get(int position) public T this[int position]
{ {
// проверка позиции get
if (position >= _places.Length || position < 0) {
return null; if (position >= _places.Count || position < 0)
return _places[position]; {
} 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;
}
}
}
}
} }