From a272e8dbd0a1fcdb6bd3ce6bc64cb02f3e76f294 Mon Sep 17 00:00:00 2001 From: rakhaliullov Date: Wed, 1 May 2024 17:20:31 +0300 Subject: [PATCH] 3 --- .../MassiveGenericObjects.cs | 83 ++++++++++--------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs b/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs index fa61f9e..bfd8836 100644 --- a/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs @@ -56,63 +56,68 @@ public class MassiveGenericObjects : ICollectionGenericObjects /// public T? Get(int position) { - if (position < 0 || position >= _collection.Length) - throw new PositionOutOfCollectionException(position); - if (_collection[position] == null) - throw new ObjectNotFoundException(position); + // проверка позиции + if (position >= _collection.Length || position < 0) + { + return null; + } return _collection[position]; } 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; - return i; + _collection[index] = obj; + 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 (_collection[position] == null) // Попытка вставить на указанную позицию + + if (position >= _collection.Length || position < 0) + { return -1; } + + if (_collection[position] == null) { _collection[position] = obj; - return true; + return position; } - for (int i = position; i < _collection.Length; i++) // попытка вставить объект на позицию после указанной - { - if (_collection[i] == null) - { - _collection[i] = obj; - return true; - } - } - for (int i = 0; i < position; i++) // попытка вставить объект на позицию до указанной - { - if (_collection[i] == null) - { - _collection[i] = obj; - return true; - } - } - throw new CollectionOverflowException(_collection.Length); - } + int index; + for (index = position + 1; index < _collection.Length; ++index) + { + if (_collection[index] == null) + { + _collection[position] = obj; + return position; + } + } + + for (index = position - 1; index >= 0; --index) + { + if (_collection[index] == null) + { + _collection[position] = obj; + return position; + } + } + return -1; + } public T Remove(int position) { - if (position < 0 || position >= _collection.Length) // проверка позиции - throw new PositionOutOfCollectionException(position); - if (_collection[position] == null) - throw new ObjectNotFoundException(position); - T temp = _collection[position]; + if (position >= _collection.Length || position < 0) + { return null; } + T DrawningAircraft = _collection[position]; _collection[position] = null; - return temp; + return DrawningAircraft; } - public IEnumerable GetItems() { for (int i = 0; i < _collection.Length; ++i)