Замена массива на список
This commit is contained in:
parent
1d3cf33ca3
commit
8a247dcc9a
@ -90,7 +90,7 @@ namespace AirBomber
|
||||
Shaking();
|
||||
for (int i = 0; i < _setAirplanes.Count; i++)
|
||||
{
|
||||
var airplane = _setAirplanes.Get(i);
|
||||
var airplane = _setAirplanes[i];
|
||||
if (airplane != null)
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, airplane);
|
||||
@ -119,11 +119,11 @@ namespace AirBomber
|
||||
int j = _setAirplanes.Count - 1;
|
||||
for (int i = 0; i < _setAirplanes.Count; i++)
|
||||
{
|
||||
if (_setAirplanes.Get(i) == null)
|
||||
if (_setAirplanes[i] == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var airplane = _setAirplanes.Get(j);
|
||||
var airplane = _setAirplanes[j];
|
||||
if (airplane != null)
|
||||
{
|
||||
_setAirplanes.Insert(airplane, i);
|
||||
@ -175,7 +175,7 @@ namespace AirBomber
|
||||
int maxLeft = (countInLine - 1) * _placeSizeWidth;
|
||||
for (int i = 0; i < _setAirplanes.Count; i++)
|
||||
{
|
||||
var airplane = _setAirplanes.Get(i);
|
||||
var airplane = _setAirplanes[i];
|
||||
airplane?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight);
|
||||
airplane?.DrawningObject(g);
|
||||
}
|
||||
|
@ -14,20 +14,23 @@ namespace AirBomber
|
||||
where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Массив объектов, которые храним
|
||||
/// Список объектов, которые храним
|
||||
/// </summary>
|
||||
private readonly T[] _places;
|
||||
private readonly List<T> _places;
|
||||
/// <summary>
|
||||
/// Количество объектов в массиве
|
||||
/// </summary>
|
||||
public int Count => _places.Length;
|
||||
public int Count => _places.Count;
|
||||
|
||||
private readonly int _maxcount;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
public SetAirplanesGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
_maxcount = count;
|
||||
_places = new List<T>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
@ -41,7 +44,7 @@ namespace AirBomber
|
||||
|
||||
private bool isCorrectPosition(int position)
|
||||
{
|
||||
return 0 <= position && position < Count;
|
||||
return 0 <= position && position < _maxcount;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
@ -51,22 +54,11 @@ namespace AirBomber
|
||||
/// <returns></returns>
|
||||
public bool Insert(T airplane, int position)
|
||||
{
|
||||
int positionNullElement = position;
|
||||
while (Get(positionNullElement) != null)
|
||||
{
|
||||
positionNullElement++;
|
||||
}
|
||||
// Если изначальная позиция была некорректной или пустых элементов справа не оказалось возвращаем false
|
||||
if (!isCorrectPosition(positionNullElement))
|
||||
if (!isCorrectPosition(position))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
while (positionNullElement != position) // Смещение вправо
|
||||
{
|
||||
_places[positionNullElement] = _places[positionNullElement - 1];
|
||||
positionNullElement--;
|
||||
}
|
||||
_places[position] = airplane;
|
||||
_places.Insert(position, airplane);
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
@ -78,7 +70,7 @@ namespace AirBomber
|
||||
{
|
||||
if (!isCorrectPosition(position))
|
||||
return false;
|
||||
_places[position] = null;
|
||||
_places.RemoveAt(position);
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
@ -86,9 +78,35 @@ namespace AirBomber
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T Get(int position)
|
||||
public T this[int position]
|
||||
{
|
||||
return isCorrectPosition(position) ? _places[position] : null;
|
||||
get
|
||||
{
|
||||
return isCorrectPosition(position) && position < Count ? _places[position] : null;
|
||||
}
|
||||
set
|
||||
{
|
||||
Insert(value, position);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Проход по набору до первого пустого
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<T> GetCars()
|
||||
{
|
||||
foreach (var car in _places)
|
||||
{
|
||||
if (car != null)
|
||||
{
|
||||
yield return car;
|
||||
}
|
||||
else
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user