2011年5月30日 星期一

如何過濾重複的陣列元素

參考來源
--
' 方法 1
Private Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button1.Click

Dim ary() As Integer = {1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3}

' 使用預設的相等比較子來比較值,以便從序列傳回獨特的項目。
For Each i As Integer In ary.Distinct.ToArray()
MessageBox.Show(i)
Next

End Sub

' 方法 2
Private Sub Button2_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button2.Click

Dim ary() As Integer = {1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3}

'IEnumerable 泛型介面
Dim ienum As IEnumerable(Of Integer) = ary

' Enumerable類別.Distinct 方法移除重複項目的序列
For Each i As Integer In ienum.Distinct
MessageBox.Show(i)
Next

End Sub

沒有留言:

張貼留言