C# list removeat then collapses.
I have a array of slots that I want to remove from a list. Using RemoveAt() that is..
When I remove an item the remaining list collapses, therefore the values of my array are off after one removal.
I can think of ways around this but I was wondering if anyone knows if there is a right way or best way?
Can I make some sort of Que, then just remove everything at once, then have it collapse afterward?
as @incorrect asks, what are you trying to achieve? a queue might be the answer, but we need to know the question ;)
So I store slot numbers to a triangle array to later sort through and remove from my mesh. If I have a giant flat surface im gonna only want two triangles not 150. I've posted something like this but my data structure has changed so I have to "rethink" my code.
Answer by Eno-Khaon · May 04, 2016 at 11:47 PM
I'm guessing you're going in ascending order through the elements removing values from the List.
When you do this, removing one means that every other after it is offset by one, then remove another and they're off by another.
If you want to remove values from a List without worrying about that, go from the end to the beginning when removing them.
// C#
for(int i = myList.Count - 1; i >= 0; i--)
{
myList.RemoveAt(i);
}
http://answers.unity3d.com/questions/527571/c-using-lists-and-removeat.html
I see what you are saying but I can't do that necessarily as I'm dealing with triangles and vertices on a grid and they are not in an order.
Could you elaborate on that?
Are you working with existing meshes and extracting vertex/triangle data from them?
Are you collapsing them down based around removing vertices/triangles relative to each other? (Like, remove a vertex and it eli$$anonymous$$ates, say, 4 triangles sharing that vertex)
Ins$$anonymous$$d of running through marching cubes then simplifying the large surfaces I'm thinking of finding them while marching cubes is doing its thing.
http://answers.unity3d.com/questions/586474/mesh-simplification-for-marching-cubes-algorithm.html
I'm essentially revising my marching cubes implementation at that link. I have a way to remove the verts, and i have an unorganized array of ints that point to the triangle list slots I want to remove. I can't just iterate through the list and call removeat() otherwise my "pointer" ints will no longer point to the correct triangle.
Yeah, that does make things a bit more complicated...
Either way, you'll need to appropriately assess which triangles reference each vertex. At that point, removing them would still make the most sense to do in order from back to front of the lists after sorting the elements to be removed from each at a time.
Actually keeping everything organized to that end is a different matter entirely, though. I haven't had a chance to work with the marching cubes algorithms, so I can't offer any first-hand experience on how to manage and improve efficiency of that yet.
God, yet another wierd way. Here is how it's done properly.
Removeall removes elements based on each items value vs an expression?
Answer by incorrect · May 04, 2016 at 09:53 PM
Create a copy of your array. Remove elements from it. Assign the result in place of initial array.
$$anonymous$$y work around is similar to this. I just change the value of slots I want to delete to something way off like 99999 then make a new array discluding anything with that value.
I can't post code till at least tomorrow. I figured its not a good way but at least its better then using remove() over and over.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080159im_/https://answers.unity.com/themes/thub/images/avi.jpg)