For the List generic, the key operations I use most often:
List<T> someList = new List();
someList.Add(x) // Adds x to the end of the list
someList.Insert(0, x) // Adds x at the given index
someList.Remove(x) // Removes the first x observed
someList.RemoveAt(0) // Removes the item at the given index
someList.Count() // Always good to know how many elements you have!
You should use the Insert method of the List class to be able to add an item at a particular position.
Basic syntax is:
listName.Insert(position, item);
A few caveats:
When you insert the item at a particular position, the items following it have to be adjusted which results in a lot of copy operations. You should use LinkedList instead for the performance benefit.
Do not use capacity if you don’t know the number of elements your list will have beforehand.
I think you are looking for the insert method. You’ll have to save the index of where you want to insert the new Item. Something like this:
int magicNumbersAreBad = 8;
int desiredPosition;
for(int index = 0; index < magicNumbersAreBad; index ++) {
equip.Add(new Item(null, null, "sadda", 1, 10, 1, g));
if(index == 5)//the specific spot you want to insert your new Item
desiredPosition = index;
}
equip.RemoveAt(5);
equip.Insert(desiredPosition, (ew Item(null,null,"asdf",1,10,1,desiredPosition?)));
To answer your question, replace the reference in your list with null. This will “remove” the item from the list, but won’t change the list itself or the order of the remaining entries. When processing the list, just test the enries for null before trying to access them. To “reinsert” an entry, you can use the assignment you made above, equip[5] = new Item(5);
Otherwise, see @rajabala’s answer.
If the items themselves are not modified, you could also use a fixed array as a “pool” of objects, which you would only have to create once at initialization.
Then use a parallel bool[] to flag whether a particular item is available or not.