473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Binarysearch in Arraylist can this be done?

I have an arraylist like the one with the Guitar Class sample in
Q316302.

Dim MycolliCol as arraylist
Private Sub FillArray()
MyColliCol.Add(New Colli(1, "STUK", 0))
MyColliCol.Add(New Colli(16, "ROL", 1))
MyColliCol.Add(New Colli(17, "BOS", 2))
MyColliCol.Add(New Colli(18, "DOOS", 0))
MyColliCol.Add(New Colli(19, "PAK", 0))
MyColliCol.Add(New Colli(20, "PANEEL", 0))
MyColliCol.Add(New Colli(21, "PALLET", 0))
MyColliCol.Add(New Colli(22, "BAK", 0))
MyColliCol.Add(New Colli(23, "PRES", 0))
MyColliCol.Add(New Colli(24, "DEUR", 0))
MyColliCol.Add(New Colli(25, "KAP/RAIL", 0))
End Sub
'I've placed the search in the form1_load for testing...

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FillArray()

Dim myObjectOdd As Object = New Colli(1, "STUK", 0)
FindMyObject(MyColliCol, myObjectOdd)

End Sub

Public Shared Sub FindMyObject(ByVal myList As ArrayList, ByVal
myObject As Object)
Dim myComparer As System.Collections.IComparer
Dim myIndex As Integer = myList.BinarySearch(1, 11, MyObject,
myComparer)
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

Gives me an argument exception (guess because 'value' is not of the
same type as the elements of the ArrayList).

But neither does it work if I use:

FindMyObject(MyColliCol, "STUK")

I just want to be able to search through the arraylist (or a
collection) and use it as a converter between the integers and the
string. For instance when I search for 24 I want "DEUR" to be
returned..

If there is (hopefully an easier) way to do things like this, please
let me know.

I must admit this is getting way above my head...

Thanks in advance,

Mike
Nov 20 '05 #1
4 1504
This may be closer to what you're looking for. The following example adds
integer values (keys) and strings (values) to a Hashtable, which is a
collection type that accepts objects for both keys and values. If you know
the key, you can retrieve the value.

Dim ht As New Hashtable
ht.Add(1, "One")
ht.Add(2, "Two")
ht.Add(3, "Three")
ht.Add(4, "Four")
ht.Add(5, "Five")
ht.Add(6, "Six")
ht.Add(7, "Seven")
ht.Add(8, "Eight")
ht.Add(9, "Nine")
ht.Add(10, "Ten")

Dim searchText As String = Me.TextBox1.Text ' TextBox1 is a textbox on
a form that contains the lookup value (a string from "1" to "10" in this
case
Try
' Get the integer value of the number entered by the user
Dim searchInt As Integer = Integer.Parse(searchText)

' find the matching string value, if available
Dim foundText As String = CType(ht.Item(searchInt), String)
If foundText Is Nothing Then
Console.WriteLine("Couldn't find a text value for the number
{0}.", searchInt)
Else
Console.WriteLine("The text value of {0} is {1}.", searchInt,
foundText)
End If
Catch ex As Exception
' if user enters anything but a valid integer, this error occurs.
Console.WriteLine("Invalid number. Enter an integer number.")
End Try

Note that you have to cast values back to the correct types to use them. In
your case, the code would look more like:
ht.Add (1, New Colli(1, "STUK", 0))
ht.Add(16, New Colli(16, "ROL", 1))

And when you retrieve an item by its integer key, you'll need to cast the
object back to Colli, and extract the 2nd field value of that Colli object
to get the name.
"Mike Dole" <m_******@hotmail.com> wrote in message
news:fd**************************@posting.google.c om...
I have an arraylist like the one with the Guitar Class sample in
Q316302.

Dim MycolliCol as arraylist
Private Sub FillArray()
MyColliCol.Add(New Colli(1, "STUK", 0))
MyColliCol.Add(New Colli(16, "ROL", 1))
MyColliCol.Add(New Colli(17, "BOS", 2))
MyColliCol.Add(New Colli(18, "DOOS", 0))
MyColliCol.Add(New Colli(19, "PAK", 0))
MyColliCol.Add(New Colli(20, "PANEEL", 0))
MyColliCol.Add(New Colli(21, "PALLET", 0))
MyColliCol.Add(New Colli(22, "BAK", 0))
MyColliCol.Add(New Colli(23, "PRES", 0))
MyColliCol.Add(New Colli(24, "DEUR", 0))
MyColliCol.Add(New Colli(25, "KAP/RAIL", 0))
End Sub
'I've placed the search in the form1_load for testing...

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FillArray()

Dim myObjectOdd As Object = New Colli(1, "STUK", 0)
FindMyObject(MyColliCol, myObjectOdd)

End Sub

Public Shared Sub FindMyObject(ByVal myList As ArrayList, ByVal
myObject As Object)
Dim myComparer As System.Collections.IComparer
Dim myIndex As Integer = myList.BinarySearch(1, 11, MyObject,
myComparer)
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

Gives me an argument exception (guess because 'value' is not of the
same type as the elements of the ArrayList).

But neither does it work if I use:

FindMyObject(MyColliCol, "STUK")

I just want to be able to search through the arraylist (or a
collection) and use it as a converter between the integers and the
string. For instance when I search for 24 I want "DEUR" to be
returned..

If there is (hopefully an easier) way to do things like this, please
let me know.

I must admit this is getting way above my head...

Thanks in advance,

Mike

Nov 20 '05 #2
In article <fd**************************@posting.google.com >, Mike Dole wrote:
I have an arraylist like the one with the Guitar Class sample in
Q316302.

Dim MycolliCol as arraylist
Private Sub FillArray()
MyColliCol.Add(New Colli(1, "STUK", 0))
MyColliCol.Add(New Colli(16, "ROL", 1))
MyColliCol.Add(New Colli(17, "BOS", 2))
MyColliCol.Add(New Colli(18, "DOOS", 0))
MyColliCol.Add(New Colli(19, "PAK", 0))
MyColliCol.Add(New Colli(20, "PANEEL", 0))
MyColliCol.Add(New Colli(21, "PALLET", 0))
MyColliCol.Add(New Colli(22, "BAK", 0))
MyColliCol.Add(New Colli(23, "PRES", 0))
MyColliCol.Add(New Colli(24, "DEUR", 0))
MyColliCol.Add(New Colli(25, "KAP/RAIL", 0))
End Sub
'I've placed the search in the form1_load for testing...

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FillArray()

Dim myObjectOdd As Object = New Colli(1, "STUK", 0)
FindMyObject(MyColliCol, myObjectOdd)

End Sub

Public Shared Sub FindMyObject(ByVal myList As ArrayList, ByVal
myObject As Object)
Dim myComparer As System.Collections.IComparer
Dim myIndex As Integer = myList.BinarySearch(1, 11, MyObject,
myComparer)
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

Gives me an argument exception (guess because 'value' is not of the
same type as the elements of the ArrayList).

But neither does it work if I use:

FindMyObject(MyColliCol, "STUK")

I just want to be able to search through the arraylist (or a
collection) and use it as a converter between the integers and the
string. For instance when I search for 24 I want "DEUR" to be
returned..

If there is (hopefully an easier) way to do things like this, please
let me know.

I must admit this is getting way above my head...

Thanks in advance,

Mike


Mike,

You'll want to make your Colli class implement IComparable. You get the
ArgumentException with BinarySearch when (and I quote):

"Neither the value nor the elements of ArrayList implement the
IComparable interface"
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #3
Mike,
Dim myComparer As System.Collections.IComparer
Dim myIndex As Integer = myList.BinarySearch(1, 11, MyObject,
myComparer) You don't initialize myComparer before the call to BinarySearch. Did you
loose something in your example posted?
I just want to be able to search through the arraylist (or a
collection) and use it as a converter between the integers and the
string. For instance when I search for 24 I want "DEUR" to be
returned..
I would recommend using a HashTable, as it can be keyed by the integer

Dim MycolliCol As HashTable
MycolliCol.Add(1, New Colli(1, "STUK", 0))
MyColliCol.Add(16, New Colli(16, "ROL", 1))

Dim item As Colli
item = DirectCast(MycolliCol(1), Colli)
item = DirectCast(MycolliCol(16), Colli)
Hope this helps
Jay

"Mike Dole" <m_******@hotmail.com> wrote in message
news:fd**************************@posting.google.c om... I have an arraylist like the one with the Guitar Class sample in
Q316302.

Dim MycolliCol as arraylist
Private Sub FillArray()
MyColliCol.Add(New Colli(1, "STUK", 0))
MyColliCol.Add(New Colli(16, "ROL", 1))
MyColliCol.Add(New Colli(17, "BOS", 2))
MyColliCol.Add(New Colli(18, "DOOS", 0))
MyColliCol.Add(New Colli(19, "PAK", 0))
MyColliCol.Add(New Colli(20, "PANEEL", 0))
MyColliCol.Add(New Colli(21, "PALLET", 0))
MyColliCol.Add(New Colli(22, "BAK", 0))
MyColliCol.Add(New Colli(23, "PRES", 0))
MyColliCol.Add(New Colli(24, "DEUR", 0))
MyColliCol.Add(New Colli(25, "KAP/RAIL", 0))
End Sub
'I've placed the search in the form1_load for testing...

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FillArray()

Dim myObjectOdd As Object = New Colli(1, "STUK", 0)
FindMyObject(MyColliCol, myObjectOdd)

End Sub

Public Shared Sub FindMyObject(ByVal myList As ArrayList, ByVal
myObject As Object)
Dim myComparer As System.Collections.IComparer
Dim myIndex As Integer = myList.BinarySearch(1, 11, MyObject,
myComparer)
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

Gives me an argument exception (guess because 'value' is not of the
same type as the elements of the ArrayList).

But neither does it work if I use:

FindMyObject(MyColliCol, "STUK")

I just want to be able to search through the arraylist (or a
collection) and use it as a converter between the integers and the
string. For instance when I search for 24 I want "DEUR" to be
returned..

If there is (hopefully an easier) way to do things like this, please
let me know.

I must admit this is getting way above my head...

Thanks in advance,

Mike

Nov 20 '05 #4
Thanks everyone for your help.

Wouldn't know what to do without you guys!

Mike
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dotNetDave | last post by:
I have created my own comparer class using IComparer for use with ArrayList.BinarySearch. My class seems to work with BinarySearch, but the problem is that my ArrayList has three items in it and it...
4
by: Homa | last post by:
I can' believe my own eye, but it happens...there is a bug in ArrayList.BinarySearch!! It should be such a simple function...... Here is the detail. (I'm using C#, don't know if this is C#'s...
4
by: Pete Z | last post by:
Does anyone know why this snippet of code continues to return x with a value of -1? Is there an issue with ArrayList.BinarySearch? ArrayList myAL = new ArrayList(); myAL .Add(1); myAL...
2
by: Henry Padilla | last post by:
I have a list of strings and I would like to insert them in order as they come up. I am trying to use ArrayList.BinarySearch which (theoretically) returns the negative bitwise compliment. And I...
1
by: illegal.prime | last post by:
So I see from the documentation here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp That the code uses the...
43
by: tshad | last post by:
Which is better to use with an ArrayList: BinarySearch or Contains? The list is only going to have strings in it and it will be sorted. Thanks, Tom
1
by: garyusenet | last post by:
My Array list contains a collection of InternetExplorer object. One of properties of this object is HWND. I'm trying to search my arraylist for the InternetExplorer object that has a certain...
3
by: Justin | last post by:
Here's a quick rundown of what I'm doing. I'm filling an arraylist with data. Then I loop through a dataset and grab a field to perform a search on the arraylist. Every time I find a match I...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.