Another array/list/dictionairy question.

Hello every1

i am having trouble with lists and array’s and dictionaries.

i have a this piece of code

for(m = 0; m < amountOfPrefabs ; m++)		// loop trew the objects
        {
		   if (arrayOfPrefabObjects[m].activeInHierarchy == false)
			{	
									
				key = listOfGameObjectNames[m];
				print ("key = " + key);
		// i use this to acces a dictionarie but i removed that part
				
				
					}
			}

this results in ; “cubes1,cubes2,cubes3,cubes4,cubes5” as expected since they are in my list.

then i do this

while( n < amountOfPrefabs )
			{
				print ("n = " + n);
				print ("listOfGameObjectNames = " + listOfGameObjectNames[n]);
				listOfGameObjectNames.RemoveAt(n);
				n++;
			}

BUT now i get a result that say’s ; “cubes1,cubes3,cubes5,cubes7”

if i remove

listOfGameObjectNames.RemoveAt(n);

then i get the same result again (1,2,3,4,5,6,7)

does anybody have ANY idea why it will skip the even numbers??

Thanks in advance!

III

Sure. To solve problems like this, you have to sort of “be the computer” and think about what your code would do line by line (rather than what you intended it to do). In this case, the while loop starts at 0. It removes item 0 (cubes1). It’s a list, so all the others shift down; now cubes2 is item 0, cubes3 is item 1, etc. Then you increment n, so now n=1, and we remove item 1 (cubes3), and the others shift down so cubes2 is item 0, cubes4 is item 1, cubes5 is item 2, etc. On the next pass, n=2, so we remove item 2 (cubes5), and so on.

Basically, the code is doing exactly what you told it to. :slight_smile:

If you want to remove all the items, just use the Clear method. If you want to iterate over them while removing them (because you’re also going to do some other stuff, I presume), then your current loop will work fine if you simply remove the “n++” line so that you’re always examining and removing the first item in the list. (Or you could work backwards through the list, removing things from the end.)

Have fun,

  • Joe

Hello Joe

Thanks for your reply.
It seems you are a better “computer” then i am. I tried to think like you described. but i turned out to be more human.
Your idea seems to be the solution, ill try it as soon as i can and let you know.

III

You are removing things from a list while you are traveling along it. The index of each item following the removed item changes.

You need to go backwards.

Well, he can go backwards, or he can just keep removing element 0. Given his current code, removing element 0 is probably easier for him.

And III, don’t worry about it too much — it takes practice to think about what you’re actually saying, rather than what you mean by what you’re saying. Unfortunately, computers are very literal!

Cheers,

  • Joe

Joe and Jackmot

Thanks again for your replies.
I got it almost running. But now i am getting new problems. However i still need to figure out what causes that problem. (it turns out my test scene was to small scale for what i want to do with it)
But your insights have helped me getting a step further.

So thanks again!

III

List has a very usefull method called “RemoveAll” which removes all the items from list that satisfy a certain condition. For example:

var list = new List<int> { 5, -3, 0, 10, -7, 6, 4, -4 };
list.RemoveAll(item => item > 0);
foreach (var item in list)
{
    Debug.Log(item);
}

The second line removes all the positive numbers from the list.