diff --git a/laba 0/laba 0/CollectionGenericObjects/ICollectionGenericObjects.cs b/laba 0/laba 0/CollectionGenericObjects/ICollectionGenericObjects.cs index 643de81..df715aa 100644 --- a/laba 0/laba 0/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/laba 0/laba 0/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -35,7 +35,7 @@ public interface ICollectionGenericObjects /// /// Добавляемый объект /// Позиция - /// true - вставка прошла удачно, false - вставка неудалась + /// true - вставка прошла удачно, false - вставка не удалась int Insert(T obj, int position); /// diff --git a/laba 0/laba 0/CollectionGenericObjects/MassiveGenericObjects.cs b/laba 0/laba 0/CollectionGenericObjects/MassiveGenericObjects.cs index 0f9e3ee..5e8dc5b 100644 --- a/laba 0/laba 0/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/laba 0/laba 0/CollectionGenericObjects/MassiveGenericObjects.cs @@ -28,61 +28,66 @@ public class MassiveGenericObjects : ICollectionGenericObjects public T? Get(int position) { - if (position < 0 || position >= _collection.Length) return null; + // TODO проверка позиции + if (position >= _collection.Length || position < 0) + { return null; } return _collection[position]; } public int Insert(T obj) - { - for (int i = 0; i < Count; i++) + {// TODO вставка в свободное место набора + 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++; } return -1; } public int Insert(T obj, int position) { - if (position < 0 || position >= Count) - { - return -1; - } + if (position >= _collection.Length || position < 0) + { return -1; } + if (_collection[position] == null) { _collection[position] = obj; return position; } + int index; - for (int i = position + 1; i < Count; i++) + for (index = position + 1; index < _collection.Length; ++index) { - if (_collection[i] == null) + if (_collection[index] == null) { - _collection[i] = obj; - return i; - } - } - for (int i = position - 1; i >= 0; i--) - { - if (_collection[i] == null) - { - _collection[i] = obj; - return i; + _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 >= Count) - { - return null; - } + // TODO проверка позиции + // TODO удаление объекта из массива, присвоив элементу массива значение null + if (position >= _collection.Length || position < 0) + { return null; } T? obj = _collection[position]; _collection[position] = null; return obj; diff --git a/laba 0/laba 0/FormBoatCollection.cs b/laba 0/laba 0/FormBoatCollection.cs index a19bb24..2320d17 100644 --- a/laba 0/laba 0/FormBoatCollection.cs +++ b/laba 0/laba 0/FormBoatCollection.cs @@ -210,7 +210,7 @@ public partial class FormBoatCollection : Form /// private void buttonCollectionDel_Click(object sender, EventArgs e) { - if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItems == null) + if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) { MessageBox.Show("Коллекция не выбрана"); return;