473,396 Members | 1,804 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,396 software developers and data experts.

searching multi - Item arraylist

how can I search for a value in a multi - item arraylist to prevent adding duplicates ?

Mar 18 '06 #1
4 1508
Hey Jon,

To prevent dups i'll use the Contains Method of the Arraylist like

if(!myArrayList.Contains(obj))
{
myArrayList.Add(obj);
}

Hope thats what u were looking for,
T
"Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
how can I search for a value in a multi - item arraylist to prevent adding
duplicates ?

Mar 18 '06 #2
it's not working for me . I add a new item as multi-dimensional like this:

"arrChartList.Add( New ChartItem (Unit,Amount ) ) "
How do I test for existence of ChartItem ?

I tried but no success with :

If Not arrChartList.Contains(ChartItem ( Unit,Amount ) ) Then
arrChartList.Add( New ChartItem (Unit,Amount ) )
End If

"Anthony Merante" <am******@hotmail.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Hey Jon,

To prevent dups i'll use the Contains Method of the Arraylist like

if(!myArrayList.Contains(obj))
{
myArrayList.Add(obj);
}

Hope thats what u were looking for,
T
"Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
how can I search for a value in a multi - item arraylist to prevent adding duplicates ?


Mar 18 '06 #3
Hi,

Jon Paal wrote:
it's not working for me . I add a new item as multi-dimensional like this:

"arrChartList.Add( New ChartItem (Unit,Amount ) ) "
How do I test for existence of ChartItem ?

I tried but no success with :

If Not arrChartList.Contains(ChartItem ( Unit,Amount ) ) Then
arrChartList.Add( New ChartItem (Unit,Amount ) )
End If


When you use the "Contains" method to look an item up, the item you pass
to the Contains method must be a reference to the *same* object stored
in the ArrayList. In other words, the '==' operator must return true.
For a string, you can use a different object with the same value,
because the '==' operator works this way for strings. But for an object,
it doesnt' work.

MyObject obj1 = new MyObject( 3, 4, 5 );
MyObject obj2 = new MyObject( 3, 4, 5 );
obj1 == obj2; // this is false, unless you override the '==' operator to
return a different value.

So... to solve your problem, there are different ways. One is to save a
reference to the ChartItem you create somewhere in your code, and to use
this reference to check if it was added to the ArrayList already, or you
can use a Hashtable and check for the Key rather than for the value
itself. Since the key can be pretty much anything, you can use for
example a string representation of your ChartItem, which makes things
easier.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Mar 18 '06 #4
that helps, I'll switch to hash tables.

thanks to all
"Laurent Bugnion" <lb******@bluewin.ch> wrote in message news:44********@news.bluewin.ch...
Hi,

Jon Paal wrote:
it's not working for me . I add a new item as multi-dimensional like this:

"arrChartList.Add( New ChartItem (Unit,Amount ) ) "
How do I test for existence of ChartItem ?

I tried but no success with :

If Not arrChartList.Contains(ChartItem ( Unit,Amount ) ) Then
arrChartList.Add( New ChartItem (Unit,Amount ) )
End If


When you use the "Contains" method to look an item up, the item you pass to the Contains method must be a reference to the *same*
object stored in the ArrayList. In other words, the '==' operator must return true. For a string, you can use a different object
with the same value, because the '==' operator works this way for strings. But for an object, it doesnt' work.

MyObject obj1 = new MyObject( 3, 4, 5 );
MyObject obj2 = new MyObject( 3, 4, 5 );
obj1 == obj2; // this is false, unless you override the '==' operator to return a different value.

So... to solve your problem, there are different ways. One is to save a reference to the ChartItem you create somewhere in your
code, and to use this reference to check if it was added to the ArrayList already, or you can use a Hashtable and check for the
Key rather than for the value itself. Since the key can be pretty much anything, you can use for example a string representation
of your ChartItem, which makes things easier.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Mar 18 '06 #5

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

Similar topics

2
by: Randy | last post by:
I'm looking for an easy way to find the value member of an object in a control. I have an ArrayList of a simple class with two properties. One property is "Id" and the other is "CompName" (both...
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...
2
by: Stephen | last post by:
I am trying to delete a row in a datagrid on the onclick of a asp:ButtonColumn. The datagrid is created from the items in an arraylist so what im trying to do is remove the item from the array and...
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
4
by: rmorvay | last post by:
I have a requirement to search a multi-dimensional array for an item, then delete the item and "reset" the array so that their are no gaps in the resulting array. I have been trying to figure out...
4
by: Tad Marshall | last post by:
Hi, I'm reading about arrays in VB.NET and I seem to have a few options for my data structure. I need a multi-dimensional array of structures, and my first thought was Public Structure myStr...
1
by: Greg Larsen | last post by:
How to I populate and reference multi-dimensional ArrayList. I would like to populate an Array list with the Add method something like so: ArrayList a = new ArrayList; ArrayList b = new...
4
by: aaronkmar | last post by:
Hello Bytes, I hope this post finds you well on this wonderful Friday! I've been kicking this code around for over a week now and cannot seem to find the correct syntax to handle all of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
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...
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.