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

This commit is contained in:
Володя 2022-10-10 19:23:32 +03:00
parent e086076f00
commit e6d43c4765
2 changed files with 46 additions and 69 deletions

View File

@ -55,13 +55,9 @@ namespace AirPlaneWithRadar
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setPlains.Count; i++)
foreach (var plain in _setPlains.GetPlains())
{
var plain = _setPlains.Get(i);
if (plain != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, plain);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, plain);
}
return new(_pictureWidth, _pictureHeight);
}
@ -80,11 +76,11 @@ namespace AirPlaneWithRadar
int j = _setPlains.Count - 1;
for (int i = 0; i < _setPlains.Count; i++)
{
if (_setPlains.Get(i) == null)
if (_setPlains[i] == null)
{
for (; j > i; j--)
{
var plain = _setPlains.Get(j);
var plain = _setPlains[i];
if (plain != null)
{
_setPlains.Insert(plain, i);
@ -117,32 +113,34 @@ namespace AirPlaneWithRadar
}
private void DrawPlains(Graphics g)
{
int CountWidth = _pictureWidth / _placeSizeWidth;
int x = _pictureWidth - _placeSizeWidth - _placeSizeWidth / 2 - _placeSizeWidth / 4;
int x = _pictureWidth - _placeSizeWidth;
int y = _placeSizeHeight / 4;
for (int k = 0; k < _setPlains.Count; k++)
{
if (_setPlains.Get(k) != null)
if (_setPlains[k] != null)
{
if ((k + 1) % CountWidth != 0 || k == 0)
{
_setPlains.Get(k)?.SetObject(x, y, _pictureWidth, _pictureHeight);
_setPlains.Get(k)?.DrawningObject(g);
_setPlains[k]?.SetObject(x, y, _pictureWidth, _pictureHeight);
_setPlains[k]?.DrawningObject(g);
x -= _placeSizeWidth;
}
else
{
_setPlains.Get(k)?.SetObject(x, y, _pictureWidth, _pictureHeight);
_setPlains.Get(k)?.DrawningObject(g);
_setPlains[k]?.SetObject(x, y, _pictureWidth, _pictureHeight);
_setPlains[k]?.DrawningObject(g);
x = _pictureWidth - _placeSizeWidth - _placeSizeWidth / 2 - _placeSizeWidth / 4;
y += _placeSizeHeight;
}
}
if (_setPlains.Get(k) == null)
if (_setPlains[k] == null)
{
if ((k + 1) % CountWidth != 0 || k == 0)
x -= _placeSizeWidth;

View File

@ -10,73 +10,52 @@ namespace AirPlaneWithRadar
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
private readonly List<T> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetPlaneGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
public int Insert(T plain)
{
int i;
for (i = 0; i < Count; i++)
{
if (_places[i] == null)
{
for (int j = i; j >= 1; j--)
{
_places[j] = _places[j - 1];
}
_places[0] = plain;
return i;
}
}
return -1;
return 1;
}
public int Insert(T plain, int position)
public bool Insert(T plain, int position)
{
if (position > Count || position < 0)
return -1;
if (_places[position] == null)
{
_places[position] = plain;
return position;
}
else
{
for (int i = position; i < Count; i++)
{
if (_places[i] == null)
{
for (int j = i; j >= position + 1; j--)
{
_places[j] = _places[j - 1];
}
_places[position] = plain;
return position;
}
}
}
return -1;
return true;
}
public T Remove(int position)
{
T mid;
if (_places[position] != null && position < _places.Length)
return _places[position];
}
public T this[int position]
{
get
{
return _places[position];
}
set
{
mid = _places[position];
_places[position] = null;
return mid;
}
else
return null;
}
public T Get(int position)
public IEnumerable<T> GetPlains()
{
return _places[position];
foreach (var plain in _places)
{
if (plain != null)
yield return plain;
else
yield break;
}
}
}