Well, I don't know about "too complex" - but it won't work.
if (itemArray[itemArray.Length] != -1 ...
Since
itemArray
by definition contains
itemArray.Length
items and arrays start at index zero,
itemArray[itemArray.Length]
is outside the bounds of the array and will throw an exception:
itemArray.Length - 1
is the last valid element:
if (itemArray[itemArray.Length - 1] != -1 ...
Why are you reinventing the wheel at all?
There is already a class that can add and remove elements:
List<T> Class (System.Collections.Generic) | Microsoft Docs[
^] And if you look at the Reference Sources:
Reference Source[
^] you will find the full source code.
Quote:
Sorry I'm explaining wrong. I mean my program I'm working on gets values from the user and stores them in an array. The users can add , delete or search values from the array. So can I use List<t> Class (System.Collections.Generic) on an array.All the starting values in the array are -1 , there are 10 array objects.
A List *is* an array - with a wrapper class that provides add, delete, and so forth (and which works with Linq methods to provide searchign and oh, so much more!)
It handles the complexities for you: like moving the elements with a higher index when you delete an element.
So you can have a List of integers, and just add and delete values as needed.
List<int> myList = new List<int>();
myList.Add(3);
myList.Add(13);
myList.Add(23);
myList.Add(33);
myList.Remove(13);
if (myList.Contains(23)) Console.WriteLine("Yes");
if (myList.Contains(13)) Console.WriteLine("No");