From 340744510b60ca139c18745d52d2bd1727aaa459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Fri, 30 Sep 2022 15:44:29 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=BF=D0=B8=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=B2=20SetAir?= =?UTF-8?q?planesGeneric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/SetAirplanesGeneric.cs | 36 +++++++++++++++------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/AirBomber/AirBomber/SetAirplanesGeneric.cs b/AirBomber/AirBomber/SetAirplanesGeneric.cs index ab32adf..0848595 100644 --- a/AirBomber/AirBomber/SetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/SetAirplanesGeneric.cs @@ -36,8 +36,12 @@ namespace AirBomber /// public bool Insert(T airplane) { - // TODO вставка в начало набора - return true; + return Insert(airplane, 0); + } + + private bool isCorrectPosition(int position) + { + return 0 < position && position <= Count; } /// /// Добавление объекта в набор на конкретную позицию @@ -47,11 +51,21 @@ namespace AirBomber /// public bool Insert(T airplane, int position) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции + int positionNullElement = position; + while (Get(positionNullElement) != null) + { + positionNullElement++; + } + // Если изначальная позиция была некорректной или пустых элементов справа не оказалось возвращаем false + if (!isCorrectPosition(positionNullElement)) + { + return false; + } + while (positionNullElement != position) // Смещение вправо + { + _places[positionNullElement] = _places[positionNullElement - 1]; + positionNullElement--; + } _places[position] = airplane; return true; } @@ -62,8 +76,9 @@ namespace AirBomber /// public bool Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присовив элементу массива значение null + if (!isCorrectPosition(position)) + return false; + _places[position] = null; return true; } /// @@ -73,8 +88,7 @@ namespace AirBomber /// public T Get(int position) { - // TODO проверка позиции - return _places[position]; + return isCorrectPosition(position) ? _places[position] : null; } } }