- Home /
generic list how to add item at specific point C#
ok I've figured that I'm able to remove item at specific point but how do I add it back to specific point?
and equip.count doesn't seem to work some said that like that I'm able to tell what's the max number of items in specific generic list
code is C#
Item is class
public List<Item> equip ;
void Start () {
equip.Capacity = 2;
for (int g = 0; g < 8; g++) {
equip.Add(new Item(g));
}
// equip.RemoveAt(5);
equip[5] = (new Item(5));
}
ok I've worked around a bit made that equip at 5 is = to what I want instead to delete it and putting new one in it
but if someone knows how to put 1 inside of in the middle of list I'd be happy to hear it
like deleting at 5 and giving 5 at 5 so 6 isn't named as 5
Note the code you posted does nothing special with element 5 - you start from an empty list, keep adding items that are placed at the index of their "identifier". So after the "for" loop, equip[5] will already contain Item(5). The assigment does nothing except replacing Item(5) with a new copy of Item(5).
Also note that using equip[5] = (new Item(5)); overwrites whatever is in position 5. To insert and increment all the indices and their values you'll have to use the insert like below or override the [] operator. But I'm not sure if that's possible with lists.
Ins$$anonymous$$d of your usage of Capacity, create/initialize a new list with:
equip=new List< Item>();
Answer by AndrewGrayGames · Jan 25, 2013 at 11:02 PM
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!
Answer by raja-bala · Jan 25, 2013 at 10:04 PM
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.
Have a look at: http://www.dotnetperls.com/list-insert and http://www.dotnetperls.com/list
hm thanks works perfectly as I wanted
but for Equip I don't care for performance as it won't be often copyed and thanks for notifying me about performance
used:
equip.Insert(5,new Item(5));
Deleting and inserting entries will still mess with the remaining indices, which you would need to keep track of to reinsert elements at their correctly ordered position. For example, if you removed entries 2 and 4, equip[5] will point to Item(7); and how do you know at which index you'll need to reinsert a new Item(4)? (which would be at 3, in this case)
What are you actually trying to accomplish?
I'm trying to accomplish learning
IF I'd post my full code 2 pages you'll be bugging your self for 2 days what I want
if I understand simple stuff I can do complicated as well
if I'm not able doing simple stuff than I'll always need to workaround the complicated ones
That's not what I meant. What are you trying to do with your equipment list, how should it behave? Do the Items have parameters that can change, or are they always the same? Can you only have exactly one item of each type? And so on.
The approach you are taking may work in the end, once you solved your open question - but it may not be the best approach to accomplish what you actually want to do. If you explain that to us, we can help you find the best approach ins$$anonymous$$d.
Answer by ATMEthan · Jan 25, 2013 at 11:03 PM
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?)));
Answer by Wolfram · Jan 25, 2013 at 10:47 PM
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.