search in a array

  • Thread starter Thread starter Jan
  • Start date Start date
J

Jan

I've got the following array: FileIn(Counter)
How can i search the array for example "window"

TIA
 
Jan said:
I've got the following array: FileIn(Counter)
How can i search the array for example "window"


\\\
Dim FileIn() As String = ...
Array.Sort(FileIn)
Dim Index As Integer = Array.BinarySearch(FileIn, "window")
///

'BinarySearch' will only make sense for a sorted array. For unsorted
arrays, a linear search would be the "best" choice, but this would mean that
the item you are looking for is compared to all items in the list until the
item is found. This algorithm has a runtime of O(n) for n items and thus is
very slow for large arrays. Nevertheless, it's faster than sorting and then
using binary search. Binary search makes sense if you are searching several
times.
 
Jan,

There are in dotnet so many arrays, when it is created as this (a fixed
array )

dim FileIn(Counter) as String

Than it can be something as
\\\
dim i as integer
For i = 0 to FileIn.length
if FiliIn(i) = "window" then
exit for
Next
///
i is the place where it is, assuming you loaded it with a zero index as
starter.

I hope this helps?

Cor
 
In addition to the other responses, you can use the IndexOf method of
the Array class, but it would not be suitable if your array had
duplicate entries:
MsgBox(Array.IndexOf(MyArrayVar,"window").ToString)

Chris
 
Chris,

The problem you tell, has my sample as well, so yours is in my opinion
better, I always skip that one you shows, I don't know why.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top