Array

bobo_5_2 Messages postés 5 Date d'inscription mardi 13 juillet 2004 Statut Membre Dernière intervention 28 juillet 2004 - 20 juil. 2004 à 17:09
jesusonline Messages postés 6814 Date d'inscription dimanche 15 décembre 2002 Statut Membre Dernière intervention 13 octobre 2010 - 20 juil. 2004 à 19:16
est ce qu'il existe une fonction pour chercher une valeur dans un tableau(array)?
merci d'avance

1 réponse

jesusonline Messages postés 6814 Date d'inscription dimanche 15 décembre 2002 Statut Membre Dernière intervention 13 octobre 2010 29
20 juil. 2004 à 19:16
tu travailles en asp.net ou en asp ?

si c'est en asp.net :

Array.BinarySearch(Array, Object) As Integer

Return Value
The index of the specified value in the specified array, if value is found.

-or-

A negative number, which is the bitwise complement of the index of the first element that is larger than value, if value is not found and value is less than one or more elements in array.

-or-

A negative number, which is the bitwise complement of (the index of the last element + 1), if value is not found and value is greater than any of the elements in array.

Example
The following code example shows how to use BinarySearch to locate a specific object in an Array.

[Visual Basic] 
Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array.
        Dim myIntArray As Array =  Array.CreateInstance(GetType(Int32), 5)
        Dim i As Integer
        For i = myIntArray.GetLowerBound(0) To myIntArray.GetUpperBound(0)
            myIntArray.SetValue(i * 2, i)
        Next i 
        ' Displays the values of the Array.
        Console.WriteLine("The Int32 array contains the following:")
        PrintValues(myIntArray)
        
        ' Locates a specific object that does not exist in the Array.
        Dim myObjectOdd As Object = 3
        FindMyObject(myIntArray, myObjectOdd)
        
        ' Locates an object that exists in the Array.
        Dim myObjectEven As Object = 6
        FindMyObject(myIntArray, myObjectEven)
    End Sub
    
    
    Public Shared Sub FindMyObject(myArr As Array, myObject As Object)
        Dim myIndex As Integer = Array.BinarySearch(myArr, myObject)
        If myIndex < 0 Then
            Console.WriteLine("The object to search for ({0}) is not found. " _
               + "The next larger object is at index {1}.", myObject, Not(myIndex))
        Else
            Console.WriteLine("The object to search for ({0}) is at index " _
               + "{1}.", myObject, myIndex)
        End If
    End Sub
     
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The Int32 array contains the following:
'     0    2    4    6    8
' The object to search for (3) is not found. The next larger object is at index 2.
' The object to search for (6) is at index 3.

Tiré du MSDN

une methode qui marche aussi en asp

for each obj as object in mArray
if obj.Tostring = key then
'on l'a trouvé
exit for
end if
next

Cyril
0
Rejoignez-nous