diff --git a/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ListGenericObjects.cs b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ListGenericObjects.cs index 7ca3490..e1f1b2a 100644 --- a/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ListGenericObjects.cs @@ -16,8 +16,7 @@ public class ListGenericObjects : ICollectionGenericObjects /// /// Список объектов, которые храним /// - //private readonly List _collection; - private readonly Dictionary _collection; + private readonly List _collection; /// /// Максимально допустимое число объектов в списке @@ -33,8 +32,7 @@ public class ListGenericObjects : ICollectionGenericObjects /// public ListGenericObjects() { - //_collection = new(); - _collection = new Dictionary(); + _collection = new(); } public T? Get(int position) @@ -49,78 +47,32 @@ public class ListGenericObjects : ICollectionGenericObjects } } - public bool Insert(T obj) + public int Insert(T obj) { - if (Count == _maxCount) { return false; } - //_collection.Add(obj); - //return true; - - //допка - int position = FindFirstNullPosition(); - if (position == -1) - { - return false; - } + if (Count == _maxCount) { return -1; } + _collection.Add(obj); + return Count; - _collection[position] = obj; - return true; } - public bool Insert(T obj, int position) + public int Insert(T obj, int position) { - //if (position < 0 || position >= Count || Count == _maxCount) - //{ - // return false; - //} - //_collection.Insert(position, obj); - - //return false; - - //допка - if (position < 0 || position >= _maxCount || Count == _maxCount || _collection.ContainsKey(position)) + if (position < 0 || position >= Count || Count == _maxCount) { - return false; + return -1; } + _collection.Insert(position, obj); + + return position; - _collection[position] = obj; - return true; } - public bool Remove(int position) + public T Remove(int position) { - // if (position < 0 || position >= Count) - // { - // return false; - // } - // _collection.RemoveAt(position); - // return true; - - //допка - if (!_collection.ContainsKey(position)) - { - return false; - } - - _collection.Remove(position); - return true; - } - /// - /// Находит первую пустую позицию в словаре - /// - /// Индекс первой пустой позиции или -1, если такой не найдено - - - //допка - private int FindFirstNullPosition() - { - for (int i = 0; i < _maxCount; i++) - { - if (!_collection.ContainsKey(i) || _collection[i] == null) - { - return i; - } - } - return -1; + if (position >= Count || position < 0) return null; + T obj = _collection[position]; + _collection.RemoveAt(position); + return obj; } }