我发现了一些类似的问题,但没有一个问题符合我的要求。假设我有10个水果值数组:
fruit(1) = "apple"
fruit(2) = "orange"
fruit(3) = "banana"
fruit(4) = "cherry"
fruit(5) = "peach"
fruit(6) = ""
fruit(7) = ""
fruit(8) = ""
fruit(9) = ""
fruit(10) = ""
现在,我有一个声明说水果(6)="苹果",制作阵列:
fruit(1) = "apple"
fruit(2) = "orange"
fruit(3) = "banana"
fruit(4) = "cherry"
fruit(5) = "peach"
fruit(6) = "apple"
fruit(7) = ""
fruit(8) = ""
fruit(9) = ""
fruit(10) = ""
我希望有一个子组,它会像项目一样分组,只存储一次。所以,
fruit(1) = "2 x apple"
fruit(2) = "orange"
fruit(3) = "banana"
fruit(4) = "cherry"
fruit(5) = "peach"
fruit(6) = ""
fruit(7) = ""
fruit(8) = ""
fruit(9) = ""
fruit(10) = ""
然后下次我添加一个苹果,它会转到" 3 x apple"等等。
所以在伪代码中我希望它
look for duplicate values
count how many duplicates
alter the original item
delete all but the newly altered entry
在vb.net中执行此操作的最简单,最优雅的方法是什么?有没有办法在没有LINQ的情况下做到这一点?
答案 0 :(得分:0)
@the_lotus是对的,你可以使用字典
Dim fruits As New Dictionary(Of String, Integer)
Sub AddFruit(fruitname As String)
If fruits.ContainsKey(fruitname) Then
fruits.Item(fruitname) += 1
Else
fruits.Add(fruitname, 1)
End If
End Sub
然后返回
Function NumberOfFruit(fruitname As String) As String
Return fruits.Item(fruitname) & " x " & fruitname
End Function