//метод увеличивает размер массива на 1 private void IncreaseLength() { TCollectionItem[] tmp = new TCollectionItem[PCollection.Length + 1]; Array.Copy(PCollection, 0, tmp, 0, PCollection.Length); PCollection = tmp; Length = PCollection.Length - 1; }
//поиск индекса элемента по значению элемента public int Search(TCollectionItem elem) { for (int i = 0; i < PCollection.Length; i++) { if (PCollection[i].Equals(elem)) { return(i); } } Console.WriteLine("Элемент не найден :("); return(-1); }
//удаление по индексу public void Delete(int index) { TCollectionItem[] tmp = new TCollectionItem[PCollection.Length - 1]; int j = 0; for (int i = 0; i < PCollection.Length; i++) { if (i != index) { tmp[j].Value = PCollection[i].Value; j++; } } PCollection = tmp; Length--; }
//вставка по индексу public void Add(TCollectionItem elem, int index) { IncreaseLength(); if (!IsInside(index)) index = PCollection.Length; if (PCollection.Length > 1) { for (int i = PCollection.Length - 2; i > index + 1; i--) { PCollection[i + 1].Value = PCollection[i].Value; } PCollection[index + 1].Value = elem; } else { PCollection[index].Value = elem; } }
//вставка по индексу public void Add(TCollectionItem elem, int index) { IncreaseLength(); if (!IsInside(index)) { index = PCollection.Length; } if (PCollection.Length > 1) { for (int i = PCollection.Length - 2; i > index + 1; i--) { PCollection[i + 1].Value = PCollection[i].Value; } PCollection[index + 1].Value = elem; } else { PCollection[index].Value = elem; } }
//поиск индекса элемента по значению элемента public int Search(TCollectionItem elem) { for (int i = 0; i < PCollection.Length; i++) { if (PCollection[i].Equals(elem)) { return i; } } Console.WriteLine("Элемент не найден :("); return -1; }