开发者

How to Find out if an items is not in an array

Ok i just want to know if value x is not in my array

Heres what i have been trying Im using VB.net and just need to know when x isnt in the array so i can take an action. 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

Ok here is where my problems is What i need to do is find out if ad is in seal if yes ignore it If ad isnt in sealed the if it is in open compare T values if ad isn in sealed or open then add it to open and set L as parent of ad The below was a way to test and see if the values were loading into the arrays right

            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

This is suppost to be an a* program but i have been having trouble main with my heuistics if you can tell m开发者_如何学Ce what im doing wrong it would also be a great help

Here is how this work so far

  1. Add my location to open
  2. Create H,G,F for items in open list
  3. Find lowest F
  4. Find adjacent nodes
  5. Loop through nodes
  6. If node is not walkable then ignore
  7. If in sealed ignore (this is where im stuck at)
  8. If not in sealed and is walkable then if in open compare G scores else add to open


try this

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


This need to be for -

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

Also, if a number is found, you can print it and break from the loop. There is no need to iterate further in the loop.

You need to be careful, the variable that is used as a flag( i.e., g) is different from the variable actually setting a value to it. ( i.e., G )


You forgot to assign initial value to g outside of the for loop.

Try this.

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


Use the Enumerable.Except method to get all points in one enumerable collection that are also present in a second collection.

Here is an example taken from the MSDN documentation of the method:

' 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜