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数组。如何删除此数组中的任何重复项?
答案 0 :(得分: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
答案 1 :(得分:4)
不是检查和删除重复元素,而是检查它是否在数组中,如果它不存在,则可以添加到数组中,否则什么都不做。
声明为实例List<string>
命名的list
对象。在循环中:
If Not list.Contains(number) Then
list.Add(number)
答案 2 :(得分:4)
你会声明一些数组(或List,或者你喜欢的任何集合),并会做类似的事情:
Array.Resize(numberArray, numberArray.Length + 1)
numberArray[numberArray.Length - 1] = number
然后你可以使用LINQ:
numberArray.Distinct()
然后,迭代数组并做任何你需要的事情。
编辑:更好的是Srinivasan__所说的,检查项目是否存在,以及是否不添加。要检查它,您可以使用Exists()。或者如果使用像List,Contains()这样的东西。
答案 3 :(得分: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++;
}
}
答案 4 :(得分:0)
为什么不像这样检查
if number.length = 11 then
if Not ItemList.contains(number)
ItemList.Add(number)
答案 5 :(得分: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