- Home /
List Management. Removing from list and making sure item is not null
This is the way I have been removing objects from a list and making sure that they are not null:
public void RemoveFromList(Transform targ){
List<Transform> tempList = targetList;
for(int i = 0; i < targetList.Count; i++) {
if(targ == targetList[i]){
tempList.Remove(tempList[i]);
break;
}
}
targetList = new List<Transform>();
for(int i = 0; i < tempList.Count; i++) {
if(tempList[i] != null){
targetList.Add(tempList[i]);
}
}
}
My question is: Is this alright to do? How taxing is it to recreate lists on the fly like this? These lists do not contain many objects(yet) and I havent noticed any performance issues. I would like to plan for adding more objects to these lists and multiple calls to this function from different objects, but I am unsure if this is the correct method to do this or if there is a better way.
Thanks
Answer by GameVortex · Nov 06, 2014 at 09:06 AM
Seems to me like you can simplify that entire thing to: targetList.Remove(targ);
The Remove function of list removes the first occurrence of the object it finds and re-sizes the list.
You will most likely not see any immediate performance improvements until you have a lot of items in the list though.
I am either crazy or doing something wrong. I had it set to: targetList.Remove(targ); before but I was getting "null" objects in my lists. I wrote the above code to make sure I wouldnt receive null objects
EDIT: I remember why I wrote this. At one point I was trying to remove multiple objects from a list. When I iterated through the targetlist and called Remove(targ) it would throw the forloop off. You are right, removing a single object from a list using targetList.Remove(targ) should work.
An example of removing a list of objects from a list:
public void RemoveList(List<Transform> targ){
List<Transform> tempList = targetList;
for(int i = 0; i < targetList.Count; i++) {
for(int j = 0; j < targ.Count; j++) {
if(targ[j] == targetList[i]){
tempList.Remove(tempList[i]);
break;
}
}
}
targetList = new List<Transform>();
//I am pretty sure I could just do this...
// targetList = tempList;
//but I want to be 100% that an item isnt NULL...
for(int i = 0; i < tempList.Count; i++) {
if(tempList[i] != null){
targetList.Add(tempList[i]);
}
}
}
new code for single object removal:
public void RemoveFromList(Transform targ){
// I want to make sure the target actually exists in the list
for(int i = 0; i < targetList.Count; i++) {
if(targetList[i] == targ){
targetList.Remove(targ);
return;
}
}
}
When removing one, you only need:
targetList.Remove(targ);
If you for some reason need to know if anything was removed:
public void RemoveFromList(Transform targ){
bool isRemoved = targetList.Remove(targ);
if(isRemoved == true){
Debug.Log("Removed");
} else {
Debug.Log("Nothing to remove");
}
}
If you remove form the same list that you are looping, you will get errors.
Here is how I solved that problem (tips for improvment is welcome):
public void CancelPickupTasks( PickUpAble PUA)
{
List<WorkTask> markedForRemoval = new List<WorkTask>();
foreach (WorkTask task in WorkTask$$anonymous$$anager.instance.allWorkTasks)
{
if (task.type == WorkTaskType.PickupItem)
{
PickupTask pickupTask = task as PickupTask;
if (pickupTask.objectToPickup.GetComponent<PickUpAble>() == PUA) {
markedForRemoval.Add(task);
}
}
}
foreach (WorkTask task in markedForRemoval)
{
task.CancelTask(); //Basicly just WorkTask$$anonymous$$anager.instance.allWorkTasks.Remove() But I might want to do stuff if a task is cancelled
}
}
An easy way to remove from list while iterating through is to move the iterator back by one each time you are removing an object. This makes sure the iterator still matches the list and indices.
Example:
for(int i = 0; i < list.Count; i++)
{
if(list[i] == target)
{
list.RemoveAt(i);
i--;
}
}
Funny I went to the $$anonymous$$S docs and found a link at the buttom the page http://kristofverbiest.blogspot.no/2008/10/removing-items-from-collection.html Reversing seems like a good solution. Here is the $$anonymous$$S documentation for .Remove as well http://msdn.microsoft.com/en-us/library/cd666k3e%28VS.80%29.aspx
Your answer