2011-05-31 270 views
1
Dim ItemList As New ArrayList() 

For i = 0 To dgExtract.Items.Count - 1 
     gRow = dgExtract.Items(i) 
     chk = gRow.FindControl("chkSelect") 
     If chk.Checked Then 
      sEmail = gRow.Cells(7).Text 
      dim number as string = Regex.Replace(sEmail,"[^0-9]","") 
      if number.length = 11 then 
       ItemList.Add(number) 
      end if 
     end if    
Next 

我用上面的代碼構建了ItemList數組。我如何刪除此數組中的任何重複項?Vb.net如何從數組中刪除重複項?

+2

我不會永遠使用ArrayList(好吧,也許會有一些奇怪的情況,我將)。在這種情況下最好使用List(Of String)。 – Neverbirth 2011-05-31 09:39:15

+0

不幸的是,有時候我們會堅持支持遺留代碼,即.Net 1.1。 – Roman 2013-08-08 19:59:53

回答

7

設置:

Dim number As Integer 
Dim num As String 
Dim al As New ArrayList() 
If Not (al.Contains(number)) Then 
    al.Add(number) 
End If 

獲取:

For Each number In al 
    num = number.ToString() 
Next 
4

而不是檢查和刪除重複的元素,你可以檢查它是否在數組中,如果它不存在,你可以添加到數組,否則什麼都不做。

聲明一個List<string>對象,例如list。在循環:

If Not list.Contains(number) Then 
    list.Add(number) 
4

你會宣佈一些數組(或列表,或任何收集你可能更喜歡),並會做這樣的事情:

Array.Resize(numberArray, numberArray.Length + 1) 
numberArray[numberArray.Length - 1] = number 

然後你可以使用LINQ:

numberArray.Distinct() 

然後,迭代數組並執行任何您需要的操作。

編輯:更好什麼Srinivasan__說,檢查項目是否存在,如果它不添加它。要檢查它,你可以使用Exists()。或者如果使用類似List的東西,Contains()。

+0

爲什麼這不是#1? – 2017-10-08 09:22:21

0

你爲什麼不檢查這樣

if number.length = 11 then 
    if Not ItemList.contains(number) 
     ItemList.Add(number) 
1
 int i; 
     int j; 
     int count = 0; 
     int[] list = new int[5]; 
     //to add 5 data with duplicate 
     list[0] = 1; 
     list[1] = 5; 
     list[2] = 2; 
     list[3] = 4; 
     list[4] = 5; 
     #region toremovetheduplicate 
     int c = 0, flag = 0; 
     int[] list1 = new int[5]; 
     for (i = 0; i < list.Length; i++) 
     { 
      flag = 0; 
      for (j = i + 1; j < list.Length; j++) 
      { 

       if (i != j) 
       { 
        if (list[i] == list[j]) 
        { 
         flag = 1; 
        } 
       } 
      } 
      if (flag == 0) 
      { 
       list1[c] = list[i]; 
       c++; 
      } 
     } 
0

我剛纔看到這一點,我嘗試下面的代碼;它工作正常,我使用CheckedListBox來查看結果。 有2個arraylist使用。 'Darray'擁有重複字符串的原始列表。然後,「FinArray」轉儲不重複的字符串,然後在列表框中顯示「FinArray」內容:

Sub CleanDupes() 
    ' Clear listbox 
    CheckedListBox1.Items.Clear() 
    ' Create Final Array for non-duped data 
    Dim FinArray As New ArrayList 
    Dim InitFinarray, DarrayN, FinArrayN As String 
    ' Add first record from original array into new array 
    FinArray.Add(Darray.Item(0)) 
    InitFinarray = FinArray.Item(0) 
    CheckedListBox1.Items.Add("Select/Unselect All") 
    CheckedListBox1.Items.Add(InitFinarray) 
    ' Loop into Orig Array and compare each record with strings in new array, 
    ' if exist in new array, then skip, else add it 
    For n As Integer = 0 To Darray.Count - 1 
     DarrayN = Darray.Item(n) 
     For n2 As Integer = 0 To FinArray.Count - 1 
      If FinArray.Contains(DarrayN) Then 
      Else 
       FinArray.Add(DarrayN) 
       FinArray.Sort(1, FinArray.Count - 1, Nothing) 
      End If 
     Next 
    Next 
    'Display New Non-Duped Array in listbox 
    For n3 As Integer = 1 To FinArray.Count - 1 
     CheckedListBox1.Items.Add(FinArray(n3)) 
    Next 
    End Sub