This commit is contained in:
rakhaliullov 2024-05-01 17:20:31 +03:00
parent 44f7ef01e8
commit a272e8dbd0

View File

@ -56,63 +56,68 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
/// <returns></returns> /// <returns></returns>
public T? Get(int position) public T? Get(int position)
{ {
if (position < 0 || position >= _collection.Length) // проверка позиции
throw new PositionOutOfCollectionException(position); if (position >= _collection.Length || position < 0)
if (_collection[position] == null) {
throw new ObjectNotFoundException(position); return null;
}
return _collection[position]; return _collection[position];
} }
public int Insert(T obj) public int Insert(T obj)
{ {
for (int i = 0; i < _collection.Length; i++) // вставка в свободное место набора
int index = 0;
while (index < _collection.Length)
{ {
if (_collection[i] == null) if (_collection[index] == null)
{ {
_collection[i] = obj; _collection[index] = obj;
return i; return index;
} }
index++;
} }
throw new CollectionOverflowException(_collection.Length); return -1;
} }
public bool Insert(T obj, int position) public int Insert(T obj, int position)
{ {
if (position < 0 || position >= _collection.Length) // проверка позиции
throw new PositionOutOfCollectionException(position); if (position >= _collection.Length || position < 0)
if (_collection[position] == null) // Попытка вставить на указанную позицию { return -1; }
if (_collection[position] == null)
{ {
_collection[position] = obj; _collection[position] = obj;
return true; return position;
} }
for (int i = position; i < _collection.Length; i++) // попытка вставить объект на позицию после указанной int index;
for (index = position + 1; index < _collection.Length; ++index)
{ {
if (_collection[i] == null) if (_collection[index] == null)
{ {
_collection[i] = obj; _collection[position] = obj;
return true; return position;
} }
} }
for (int i = 0; i < position; i++) // попытка вставить объект на позицию до указанной
{
if (_collection[i] == null)
{
_collection[i] = obj;
return true;
}
}
throw new CollectionOverflowException(_collection.Length);
}
for (index = position - 1; index >= 0; --index)
{
if (_collection[index] == null)
{
_collection[position] = obj;
return position;
}
}
return -1;
}
public T Remove(int position) public T Remove(int position)
{ {
if (position < 0 || position >= _collection.Length) // проверка позиции if (position >= _collection.Length || position < 0)
throw new PositionOutOfCollectionException(position); { return null; }
if (_collection[position] == null) T DrawningAircraft = _collection[position];
throw new ObjectNotFoundException(position);
T temp = _collection[position];
_collection[position] = null; _collection[position] = null;
return temp; return DrawningAircraft;
} }
public IEnumerable<T?> GetItems() public IEnumerable<T?> GetItems()
{ {
for (int i = 0; i < _collection.Length; ++i) for (int i = 0; i < _collection.Length; ++i)