Замена массива на список, исправление ошибок во втором параметризированном классе
This commit is contained in:
parent
24e3b1ac19
commit
20616550bc
@ -90,13 +90,9 @@ namespace ArmoredCar
|
||||
public Bitmap ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
for (int i = 0; i < _setCars.Count; i++)
|
||||
foreach (var armoredCar in _setCars.GetCars())
|
||||
{
|
||||
var car = _setCars.Get(i);
|
||||
if (car != null)
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
|
||||
}
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, armoredCar);
|
||||
}
|
||||
return new(_pictureWidth, _pictureHeight);
|
||||
}
|
||||
@ -121,11 +117,11 @@ namespace ArmoredCar
|
||||
int j = _setCars.Count - 1;
|
||||
for (int i = 0; i < _setCars.Count; i++)
|
||||
{
|
||||
if (_setCars.Get(i) == null)
|
||||
if (_setCars[i] == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var car = _setCars.Get(j);
|
||||
var car = _setCars[i];
|
||||
if (car != null)
|
||||
{
|
||||
_setCars.Insert(car, i);
|
||||
@ -177,14 +173,13 @@ namespace ArmoredCar
|
||||
private void DrawArmoredCars(Graphics g)
|
||||
{
|
||||
int width = _pictureWidth / _placeSizeWidth;
|
||||
|
||||
for (int i = 0; i < _setCars.Count; i++)
|
||||
{
|
||||
if (_setCars.Get(i) != null) {
|
||||
_setCars.Get(i)?.SetObject(i % width * _placeSizeWidth + 5, i / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
||||
_setCars.Get(i)?.DrawningObject(g);
|
||||
}
|
||||
}
|
||||
int k = 0;
|
||||
foreach (var armoredCar in _setCars.GetCars())
|
||||
{
|
||||
armoredCar.SetObject(k % width * _placeSizeWidth + 5, k / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
||||
armoredCar.DrawningObject(g);
|
||||
k++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,20 +10,23 @@ namespace ArmoredCar
|
||||
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 SetArmoredCarsGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
@ -31,8 +34,10 @@ namespace ArmoredCar
|
||||
/// <param name="armoredCar">Добавляемый автомобиль</param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T armoredCar)
|
||||
{
|
||||
return Insert(armoredCar, 0);
|
||||
{
|
||||
if (Count < _maxCount)
|
||||
return Insert(armoredCar, 0);
|
||||
return -1;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
@ -42,31 +47,10 @@ namespace ArmoredCar
|
||||
/// <returns></returns>
|
||||
public int Insert(T armoredCar, int position)
|
||||
{
|
||||
if (position < 0 || position >= Count)
|
||||
if (position < 0 || position >= _maxCount)
|
||||
return -1;
|
||||
|
||||
if (!(_places[position] == null))
|
||||
{
|
||||
int index_empty = -1;
|
||||
// поиск первого пустого элемента
|
||||
for (int i = position + 1; i < Count; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
index_empty = i;
|
||||
}
|
||||
}
|
||||
if (index_empty == -1)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
for (int i = index_empty; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
_places[position] = armoredCar;
|
||||
|
||||
_places.Insert(position, armoredCar);
|
||||
return position;
|
||||
}
|
||||
/// <summary>
|
||||
@ -76,22 +60,55 @@ namespace ArmoredCar
|
||||
/// <returns></returns>
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position < 0 || position >= Count)
|
||||
if (position < 0 || position >= _maxCount)
|
||||
return null;
|
||||
T armoredCar = _places[position];
|
||||
_places[position] = null;
|
||||
_places.RemoveAt(position);
|
||||
return armoredCar;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Получение объекта из набора по позиции
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T Get(int position)
|
||||
public T this[int position]
|
||||
{
|
||||
if (position < 0 || position >= Count)
|
||||
return null;
|
||||
return _places[position];
|
||||
get
|
||||
{
|
||||
if (position < 0 || position >= _maxCount)
|
||||
return null;
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
|
||||
if (position < 0 || position >= _maxCount)
|
||||
return;
|
||||
|
||||
_places.Add(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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