доработка базовой части (Insert, Remove)

This commit is contained in:
Мельников Игорь 2022-12-07 17:47:51 +04:00
parent 51262a3eb0
commit 6f8d738bf3

View File

@ -29,7 +29,7 @@
/// <returns></returns> /// <returns></returns>
public int Insert(T locomotive, int position) public int Insert(T locomotive, int position)
{ {
if (position < 0 || position > _maxCount) if (position < 0 || position > Count + 1)
{ {
return position; return position;
} }
@ -38,22 +38,8 @@
_places.Add(locomotive); _places.Add(locomotive);
return 0; return 0;
} }
if (position == 0) _places.Insert(position, locomotive);
{ return position;
for (int i = Count; i >= 1; i--)
{
_places.Insert(i, _places[i - 1]);
_places.RemoveAt(i - 1);
}
_places.Insert(0, locomotive);
return 0;
}
if (_places[position] == null)
{
_places.Insert(position, locomotive);
return position;
}
return -1;
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
@ -62,12 +48,12 @@
/// <returns></returns> /// <returns></returns>
public T Remove(int position) public T Remove(int position)
{ {
if (_places[position] == null) if (position > Count || _places[position] == null)
{ {
return null; return null;
} }
var result = _places[position]; var result = _places[position];
_places[position] = null; _places.RemoveAt(position);
return result; return result;
} }
/// <summary> /// <summary>