From 2318c8a11f1bca6aaca3b8b581a892278ce5dee9 Mon Sep 17 00:00:00 2001 From: MorozovDanil Date: Mon, 15 Apr 2024 12:00:51 +0400 Subject: [PATCH] =?UTF-8?q?=D1=83=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=BF=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ListGenericObjects.cs | 82 ++++--------------- 1 file changed, 17 insertions(+), 65 deletions(-) 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; } }