如何确定项目是否不在数组中

时间:2011-03-19 10:18:19

标签: vb.net arrays boolean

好的,我只是想知道值x是否不在我的数组中

这是我一直在努力的事情 我正在使用VB.net 并且只需要知道x不在数组中,这样我就可以采取行动了。 thankx

   Dim L, Path(0) As Integer


    Open = cleara(Open)
    sealed = cleara(sealed)
     Open(0) = Agent
    sealed(0) = Agent
    Finds adjacent nodes
    L = Agent
    Do Until sealed(sealed.GetLength(0) - 1) = Targ Or Open.GetLength(0) = 0
        'Agents(0) = L
        H = Find_H(L, Targ, Open)
        'T = Find_T(L, Targ, Open)
        ReDim F(T.GetLength(0) - 1)
        For lp As Integer = 0 To F.GetLength(0) - 1
            F(lp) = H(lp) '+ H(lp)
        Next
        L = Find_lowest(F, Open)
        Open = Remove_from(Open, L)
        sealed = Add_to(sealed, L)

        Ad = Find_adjacent(L, Targ)
        For lp As Integer = 0 To Ad.GetLength(0) - 1

好的,这就是我的问题所在 我需要做的是 如果是,请查看广告是否在密封中 如果ad不是密封的,那么它是否处于打开比较T值 如果ad没有密封或打开,则将其添加到打开状态并将L设置为广告的父级 下面是一种测试并查看值是否正在加载到数组中的方法

            If Walk(Ad(lp)) <> -1 Then
                Parents(Ad(lp)) = L
                Open = Add_to(Open, Ad(lp))
                For lp2 As Integer = 0 To sealed.GetLength(0) - 1
                    For lp3 As Integer = 0 To Open.GetLength(0) - 1
                        If lp3 < Open.GetLength(0) - 1 Then
                            If Open(lp3) = sealed(lp2) Then
                                Open = Remove_from(Open, sealed(lp2))
                            End If
                        End If
                    Next
                Next
            End If


        Next
        G.Graphics.DrawRectangle(Pens.White, Grid(Targ))
        TempDrawing()




    Loop

这可能是一个*程序,但是如果你能告诉我什么是我做错的话,我一直无法解决我的语言学问题,这也是一个很好的帮助

到目前为止这是如何工作的

  1. 添加我的位置以打开
  2. 为打开列表中的项目创建H,G,F
  3. 找到最低的F
  4. 查找相邻节点
  5. 循环播放节点
  6. 如果节点不可步行,则忽略
  7. 如果密封忽略(这是我坚持的地方)
  8. 如果没有密封并且可以步行,那么如果在开放比较G分数,则加入打开

4 个答案:

答案 0 :(得分:6)

试试这个

If Array.IndexOf(yourArray, x) == -1 Then
    'Do something
End If

答案 1 :(得分:0)

这需要for -

For lp as integer = 0 to array.getlength(0) - 1
                                         ^ error. Needs to array.GetLength()

此外,如果找到一个数字,您可以打印它并从循环中断开。无需在循环中进一步迭代。

您需要注意,用作标志的变量(即g)与实际为其设置值的变量不同。 (即G

答案 2 :(得分:0)

您忘记在for循环之外的g中分配初始值。

试试这个。

Dim g as int 
g = 0

For lp as integer = 0 to array.Length() - 1 
    If X = array(LP) Then
        g = 1  
        Exit For
    End If 
next 

If g = 0 Then
      X is not in array
End If

答案 3 :(得分:0)

使用Enumerable.Except方法获取一个可枚举集合中也存在于第二个集合中的所有点。

以下是该方法的MSDN文档中的示例:

' Create two arrays of doubles.
Dim numbers1() As Double = {2.0, 2.1, 2.2, 2.3, 2.4, 2.5}
Dim numbers2() As Double = {2.2}

' Select the elements from the first array that are not
' in the second array.
Dim onlyInFirstSet As IEnumerable(Of Double) = numbers1.Except(numbers2)

Dim output As New System.Text.StringBuilder
For Each number As Double In onlyInFirstSet
    output.AppendLine(number)
Next

' Display the output.
MsgBox(output.ToString())

' This code produces the following output:
'
' 2
' 2.1
' 2.3
' 2.4
' 2.5