473,397 Members | 2,077 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,397 software developers and data experts.

Compare part of object in an arraylist

Sam
Hi Everyone,

I have a stucture below stored in an arraylist and I want to check user's
input (point x,y) to make sure there is no duplicate point x,y entered
(string label can be duplicated). Is there a way to compare the new input
point x,y without having to a loop and compare the whole object in the array
list. I can't use the indexof method because it would compare the whole
object.

Public Structure
Dim x, y as integer
Dim label as string
End Structure
Sam
Nov 21 '05 #1
3 2283
Hi Sam

Why don't you use a Hashtable, with a String key formed by concatenating x ,
y, that way you can test with ContainsKey?

HTH

Nigel Armstrsong

"Sam" wrote:
Hi Everyone,

I have a stucture below stored in an arraylist and I want to check user's
input (point x,y) to make sure there is no duplicate point x,y entered
(string label can be duplicated). Is there a way to compare the new input
point x,y without having to a loop and compare the whole object in the array
list. I can't use the indexof method because it would compare the whole
object.

Public Structure
Dim x, y as integer
Dim label as string
End Structure
Sam

Nov 21 '05 #2
Sam,
In addition to Nigel's suggestion. I would define a Coordinate structure
that contained the X & Y components. Then I would either use this structure
as the key to the hashtable or use it ArrayList.IndexOf method. I would
favor the HashTable.

Something like:
Public Structure Coordinate

Public ReadOnly X, Y As Integer

Public Sub New(ByVal x As Integer, ByVal y As Integer)
Me.X = x
Me.Y = y
End Sub

#Region " Hashtable sample code "

Public Overrides Function GetHashCode() As Integer
Return Me.X.GetHashCode() Xor Me.Y.GetHashCode()
End Function

Public Overloads Function Equals(ByVal other As Coordinate) As
Boolean
Return Me.X = other.X AndAlso Me.Y = other.Y
End Function

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is Coordinate Then
Return Equals(DirectCast(obj, Coordinate))
Else
Return False
End If
End Function

#End Region

End Structure

Public Structure Node

ReadOnly Coordinate As Coordinate

Dim Label As String

Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal label
As String)
Me.Coordinate = New Coordinate(x, y)
Me.Label = label
End Sub

#Region " Arraylist sample code "

Public Overloads Function Equals(ByVal other As Node) As Boolean
Return Me.Coordinate.Equals(other.Coordinate)
End Function

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is Node Then
Return Equals(DirectCast(obj, Node))
Else
Return False
End If
End Function
#End Region

End Structure

Public Shared Sub Main()
' Hashtable sample code
Dim table As New Hashtable
table.Add(New Coordinate(1, 1), New Node(1, 1, "a"))
table.Add(New Coordinate(2, 3), New Node(2, 3, "b"))
table.Add(New Coordinate(5, 4), New Node(5, 4, "c"))

Dim value As Node = table.Item(New Coordinate(2, 3))
' Arraylist sample code
Dim list As New ArrayList
list.Add(New Node(1, 1, "a"))
list.Add(New Node(2, 3, "b"))
list.Add(New Node(5, 4, "c"))

Dim index As Integer = list.IndexOf(New Node(2, 3, String.Empty))

End Sub

I would favor the HashTable, as you can do the lookup via Coordinate itself,
you don't need to create an entire Node just to do the lookup...

Rather then using a HashTable directly I would create a NodeCollection class
that inherits DictionaryBase that encapsulates the creation of the
Coordinate class.

Something like:
Public Structure
Dim x, y as integer
Dim label as string
Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal label
As String)
Me.x = x
Me.y = y
Me.Label = label
End Sub
End Structure
Public Class NodeCollection
Inherits DictionaryBase

Public Sub Add(ByVal value As Node)
Dim key As New Coordinate(value.x, value.y)
Me.InnerHashtable.Add(key, value)
End Sub

Default Public Property Item(ByVal x As Integer, ByVal y As Integer)
As Node
Get
Dim key As New Coordinate(x, y)
Return DirectCast(Me.InnerHashtable.Item(key), Node)
End Get
Set(ByVal value As Node)
Dim key As New Coordinate(x, y)
Me.InnerHashtable.Item(key) = value
End Set
End Property
End Class
Dim collection As New NodeCollection
collection.Add(New Node(1, 1, "a"))
collection.Add(New Node(2, 3, "b"))
collection.Add(New Node(5, 4, "c"))
value = collection.Item(2, 3)
Hope this helps
Jay

"Sam" <qd*@datawave.ca> wrote in message
news:en***************@TK2MSFTNGP11.phx.gbl... Hi Everyone,

I have a stucture below stored in an arraylist and I want to check user's
input (point x,y) to make sure there is no duplicate point x,y entered
(string label can be duplicated). Is there a way to compare the new input
point x,y without having to a loop and compare the whole object in the
array
list. I can't use the indexof method because it would compare the whole
object.

Public Structure
Dim x, y as integer
Dim label as string
End Structure
Sam

Nov 21 '05 #3
Sam
Thanks so lots guys. This solves my problem

Sam

"Sam" <qd*@datawave.ca> wrote in message
news:en***************@TK2MSFTNGP11.phx.gbl...
Hi Everyone,

I have a stucture below stored in an arraylist and I want to check user's
input (point x,y) to make sure there is no duplicate point x,y entered
(string label can be duplicated). Is there a way to compare the new input
point x,y without having to a loop and compare the whole object in the array list. I can't use the indexof method because it would compare the whole
object.

Public Structure
Dim x, y as integer
Dim label as string
End Structure
Sam

Nov 21 '05 #4

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

Similar topics

19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
0
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
17
by: bengamin | last post by:
Hi, I have a C# class and two instance of the class; the class have some property. I want to compare the property value of the two instance How should i do? override == ? use delegate ?
1
by: bengamin | last post by:
Hi, I declare two ArrayList variables.How can I compare if the two ArrayList are Value Equal. Thanks! Ben
3
by: Frank | last post by:
Hi! I'm kinda stuck here. I have an Hashtable with with my custom "Item" Object. The key used in the Hashtable is the "Item.Id", i have selected this key because with a lot of functions i can...
4
by: Gaby | last post by:
Hi all, What is the best way to compare 2 (large) ArrayLists filled with an object. Can you please help me? Gaby
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
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,...

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.