473,761 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ByVa l sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load
FillArray()

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

End Sub

Public Shared Sub FindMyObject(By Val myList As ArrayList, ByVal
myObject As Object)
Dim myComparer As System.Collecti ons.IComparer
Dim myIndex As Integer = myList.BinarySe arch(1, 11, MyObject,
myComparer)
If myIndex < 0 Then
Console.WriteLi ne("The object to search for ({0}) is not
found. " _
+ "The next larger object is at index {1}.", myObject,
_
Not myIndex)
Else
Console.WriteLi ne("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(My ColliCol, "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 1528
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.Tex t ' 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(s earchText)

' find the matching string value, if available
Dim foundText As String = CType(ht.Item(s earchInt), String)
If foundText Is Nothing Then
Console.WriteLi ne("Couldn't find a text value for the number
{0}.", searchInt)
Else
Console.WriteLi ne("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.WriteLi ne("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_******@hotma il.com> wrote in message
news:fd******** *************** ***@posting.goo gle.com...
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(ByVa l sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load
FillArray()

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

End Sub

Public Shared Sub FindMyObject(By Val myList As ArrayList, ByVal
myObject As Object)
Dim myComparer As System.Collecti ons.IComparer
Dim myIndex As Integer = myList.BinarySe arch(1, 11, MyObject,
myComparer)
If myIndex < 0 Then
Console.WriteLi ne("The object to search for ({0}) is not
found. " _
+ "The next larger object is at index {1}.", myObject,
_
Not myIndex)
Else
Console.WriteLi ne("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(My ColliCol, "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(ByVa l sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load
FillArray()

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

End Sub

Public Shared Sub FindMyObject(By Val myList As ArrayList, ByVal
myObject As Object)
Dim myComparer As System.Collecti ons.IComparer
Dim myIndex As Integer = myList.BinarySe arch(1, 11, MyObject,
myComparer)
If myIndex < 0 Then
Console.WriteLi ne("The object to search for ({0}) is not
found. " _
+ "The next larger object is at index {1}.", myObject,
_
Not myIndex)
Else
Console.WriteLi ne("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(My ColliCol, "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
ArgumentExcepti on 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.Collecti ons.IComparer
Dim myIndex As Integer = myList.BinarySe arch(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(Myco lliCol(1), Colli)
item = DirectCast(Myco lliCol(16), Colli)
Hope this helps
Jay

"Mike Dole" <m_******@hotma il.com> wrote in message
news:fd******** *************** ***@posting.goo gle.com... 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(ByVa l sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load
FillArray()

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

End Sub

Public Shared Sub FindMyObject(By Val myList As ArrayList, ByVal
myObject As Object)
Dim myComparer As System.Collecti ons.IComparer
Dim myIndex As Integer = myList.BinarySe arch(1, 11, MyObject,
myComparer)
If myIndex < 0 Then
Console.WriteLi ne("The object to search for ({0}) is not
found. " _
+ "The next larger object is at index {1}.", myObject,
_
Not myIndex)
Else
Console.WriteLi ne("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(My ColliCol, "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
1506
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 seems that BinarySearch only searches the first two items. So if I happen to be looking for the last item in the ArrayList, BinarySearch returns that it's not there. Any ideas? David McCarter
4
4527
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 problem or the whole framework's problem. ArrayList test = new ArrayList(); test.Add(10); test.Add(7);
4
6801
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 .Add(2);
2
1514
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 should be able to find the index of the element one higher than where I should insert. How? I'm not getting it.
1
4825
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 less than operator: int myIndex=myList.BinarySearch( myObject ); if ( myIndex < 0 ) when using the BinarySearch method and in my own experience I see that sometimes it returns other negative values (other than -1).
43
7030
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
1899
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 value for its HWND property. But i don't know the syntax to use. I have tried many things, the latest I have tried is: -
3
3101
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 update another field with a 1. If I don't find a match I update it with a 0. I then remove that item from the arraylist and move on. The end result of this is I know which items ARE in the dataset, which items ARE NOT in the dataset and which...
8
2596
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; m_ResultArray.Add ( aFoundObject);
0
9377
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.