比较vb.net中的2个arraylist

时间:2016-01-14 09:22:06

标签: vb.net arraylist

我想野营2名士兵并展示价值观。

arraylist1 = {1,2,3,4,5,6,7,8,9}

arraylist2 = {2,4,5}

我有比较和listview的价值..像这样

1  Not available
2  available
3  Not available
4  available
5  available
6  Not available

我这样编写程序但循环多次..

 For position As Integer = 0 To arraylist1.Count - 1
  Dim words As String() = arraylist2(position).Split(New Char() {" "c})
    arr(1) = words(3)
    For i = 0 To arr.Length - 1
     If arraylist1(i).Contains(arr(1)) Then
        arr(0) = i
        arr(2) = "working"
        itm = New ListViewItem(arr)
        lv1.Items.Add(itm)
     Else
        arr1(0) = i
        arr1(1) = arrproc(i)
        arr1(2) = "NOT working"
        itm = New ListViewItem(arr1)
        lv1.Items.Add(itm)
     End If
   Next
 Next

3 个答案:

答案 0 :(得分:2)

蒂姆建议使用List(Of Integer)

有点像这样

Dim list1 As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim list2 As New List(Of Integer) From {2, 4, 5}

        For Each i As Integer In list1

            If list2.Contains(i) Then

                Console.WriteLine(i & " available")
            Else

                Console.WriteLine(i & " Not available")

            End If

        Next

同样在第一个循环的代码中,你使用arraylist2中arraylist1的索引来获取如果两个列表没有相同数量的项目并且将遇到越界异常的话,这将失败

修改

所以还有一些问题要解决我猜

  • 你循环遍历arraylist1以从arraylist2
  • 中检索项目
  • 您使用该项目的第四个字并将其分配给随机数组(arr())
  • 然后循环遍历该随机数组并比较第二项 该数组包含arraylist1中所有项目的索引 随机数组

所以我的建议是再次审核您的代码

这可能有用,但仍有一些问题要回答

For Each item_list1 As String In arraylist2
            Dim words As String() = item_list1.Split(New Char() {" "c})
            If arraylist1.contains(words(3)) Then
                itm = New ListViewItem(arraylist1.IndexOf(words(3)))
                itm.SubItems.Add(words(3))
                itm.SubItems.Add("working")
            Else
                itm = New ListViewItem(arraylist1.IndexOf(words(3)))
                itm.SubItems.Add(arrproc(arraylist1.IndexOf(words(3))))
                itm.SubItems.Add("NOT working")
            End If
            lv1.items.add(itm)
        Next

答案 1 :(得分:1)

您可以使用此类linq查询来比较这些列表并返回第二个列表中第一个列表项的外观状态:

Dim list1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim list2 = {2, 4, 5}
Dim data = list1.Select(Function(item)
                 Return New With
                 {
                    .Value = item,
                    .Status = String.Format("{0} {1}", item, IIf(list2.Contains(item), "Available", "Not Available"))
                 }
                 End Function).ToList()

然后你可以这样简单地将它们添加到ListView

For Each item In data
    Me.ListView1.Items.Add(item.Value.ToString()).SubItems.Add(item.Status)
Next

答案 2 :(得分:1)

完整示例

Sub Main()

        Dim list1 As New List(Of Integer)() From {1, 2, 3, 4, 5, 6, 7, 8, 9}
        Dim list2 As New List(Of Integer)() From {2, 4, 5}

        Dim rows = From i In list1
                    Group Join j In list2
                    On j Equals i Into g = Group
                    From j In g.DefaultIfEmpty()
                    Select i, j


        For Each r In rows
            Console.WriteLine("{0} {1}", r.i, If(r.i = r.j, "exist", "not exist"))
        Next


        Console.ReadLine()
    End Sub