Is there a short an beautiful way to delete empty Lists of an empty List in VB.Net?

I got a List of List of Integers. While some nested Lists contain Integers, some do not.

Dim innerList1 As New List(Of Integer)
Dim innerList2 As New List(Of Integer)
Dim innerList3 As New List(Of Integer)
Dim outerList As New List(Of List(Of Integer))

innerList1.add(1)
innerList1.add(2)
innerList3.add(25)

outerList.add(innerList1)
outerList.add(innerList2)
outerList.add(innerList3)

The Output could look like this:

outerList:

innerList1: 1 2

innerList2:

innerList3: 25

Now I want to delete the empty innerList with following code:

For i = 0 to outerList.Count() - 1
    If (outerList(i).Count() = 0) Then
        outerList.remove(outerList(i))
    End If
Next

The problem is, that the index will be out of Range after some Iterations once an innerList is deleted.

I tried also while-statements and do loops. But nothing worked.

I would be very thankful, if someone could help me :)



Read more here: https://stackoverflow.com/questions/68468501/is-there-a-short-an-beautiful-way-to-delete-empty-lists-of-an-empty-list-in-vb-n

Content Attribution

This content was originally published by Dawo at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: