英文:
What's the best algorithm to remove items from a list that is reindexed when an item is removed?
问题
我处理的可能是一个常见情况,我甚至不知道如何用搜索来表达。
我决定寻求指导,不一定要得到任何代码片段,而是询问在这种情况下最佳的处理方法。
我有一个名为arrIntIndexesOfJobsThatWillBeKilled
的列表,其中包含必须从另外5个列表中移除的项目的原始索引。
然而,当我从其他5个列表中移除一个项目时,它们被重新索引,我曾经在arrIntIndexesOfJobsThatWillBeKilled
中拥有的索引现在无效了。
我想要仅删除与原始索引相关联的项目...
如何处理这种情况的最佳算法是什么?
arrIntIndexesOfJobsThatWillBeKilled = arrIntIndexesOfJobsThatWillBeKilled.Distinct.ToList
If arrIntIndexesOfJobsThatWillBeKilled.Count > 0 Then
For Each Job As Integer In arrIntIndexesOfJobsThatWillBeKilled
arrStrJobDescription2.RemoveAt(Job)
arrStrPNDirectories2.RemoveAt(Job)
arrIntJobIDDirectories2.RemoveAt(Job)
arrStrSubDirectories2.RemoveAt(Job)
arrStrNCFile2.RemoveAt(Job)
Next
End If
英文:
I'm dealing with a probably common situation that I don't even know how to formulate in a search.
I decided to ask for guidance, not necessarily to get any piece of code but to ask about what is the best approach for this kind of situation.
I have a list named arrIntIndexesOfJobsThatWillBeKilled
containing the original indexes of the items that must be removed from another 5 lists.
However, when I remove an item from the other 5 lists, they are reindexed, and the indexes I once had in arrIntIndexesOfJobsThatWillBeKilled
are now invalid.
I want to remove only the items linked to the original indexes...
What is the best algorithm to handle this situation?
arrIntIndexesOfJobsThatWillBeKilled = arrIntIndexesOfJobsThatWillBeKilled.Distinct.ToList
If arrIntIndexesOfJobsThatWillBeKilled.Count > 0 Then
For Each Job As Integer In arrIntIndexesOfJobsThatWillBeKilled
arrStrJobDescription2.RemoveAt(Job)
arrStrPNDirectories2.RemoveAt(Job)
arrIntJobIDDirectories2.RemoveAt(Job)
arrStrSubDirectories2.RemoveAt(Job)
arrStrNCFile2.RemoveAt(Job)
Next
End If
答案1
得分: 3
你可以更改移除顺序以反转删除较大索引的顺序。我不太熟悉VB,但在C#中,它看起来会像这样:
arrIntIndexesOfJobsThatWillBeKilled = arrIntIndexesOfJobsThatWillBeKilled
.OrderByDescending(i => i) // 假设这里是整数列表,或者 .OrderDescending() 适用于较新的框架
.Distinct()
.ToList();
然后继续执行删除。
P.S.
If arrIntIndexesOfJobsThatWillBeKilled.Count > 0 Then
的检查是多余的 - foreach
可以很好地处理空集合。
英文:
You can change the order of removal to reverse one - delete bigger indexes first. Not fluent with VB, but in C# it will look like:
arrIntIndexesOfJobsThatWillBeKilled = arrIntIndexesOfJobsThatWillBeKilled
.OrderByDescending(i => i) // assuming list of integers here, or .OrderDescending() for later framework
.Distinct()
.ToList();
And then proceed with deletion.
P.S.
If arrIntIndexesOfJobsThatWillBeKilled.Count > 0 Then
check is redundant - foreach
handles empty collections just fine.
答案2
得分: 0
以下是已翻译好的代码部分:
如果 arrIntIndexesOfJobsThatWillBeKilled.Count > 0 Then
arrIntIndexesOfJobsThatWillBeKilled = arrIntIndexesOfJobsThatWillBeKilled.OrderByDescending(Function(i) i).Distinct.ToList
For Each Job As Integer In arrIntIndexesOfJobsThatWillBeKilled
arrStrJobDescription2.RemoveAt(Job)
arrStrPNDirectories2.RemoveAt(Job)
arrIntJobIDDirectories2.RemoveAt(Job)
arrStrSubDirectories2.RemoveAt(Job)
arrStrNCFile2.RemoveAt(Job)
Next
End If
英文:
After the guidance of the collaborators here, this is the code that works:
If arrIntIndexesOfJobsThatWillBeKilled.Count > 0 Then
arrIntIndexesOfJobsThatWillBeKilled = arrIntIndexesOfJobsThatWillBeKilled.OrderByDescending(Function(i) i).Distinct.ToList
For Each Job As Integer In arrIntIndexesOfJobsThatWillBeKilled
arrStrJobDescription2.RemoveAt(Job)
arrStrPNDirectories2.RemoveAt(Job)
arrIntJobIDDirectories2.RemoveAt(Job)
arrStrSubDirectories2.RemoveAt(Job)
arrStrNCFile2.RemoveAt(Job)
Next
End If
Many thanks for once again educating this mediocre coder...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论