I'm having difficulty searching an arraylist for certain values that are in
another arraylist. Essentially, I want to load the second arraylist with
only one of each of the possible elements in the first arraylist. Thus if
singlearraylist contains "ww", "l2", "s1", "ww", "cc", "s1" I only want
marraylist to have "ww", "l2", "s1", "cc". The search seems to fail
regardless what I do, and the result is that marraylist has virtually all of
the elements from singlearraylist. Here's my code:
For i = 0 To singlearraylist.Count - 1
searchobj = singlearraylist(i)
ifind = marraylist.BinarySearch(searchobj)
If ifind < 0 Then ' ie, not found
marraylist.Add(singlearraylist(i))
End If
Next
Tx for any help.
Bernie Yaeger 4 1294
OK, figured it out - I have to continually sort the arraylist as I add
items - binarysearch only works against a sorted arraylist.
Tx - Bernie
"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... I'm having difficulty searching an arraylist for certain values that are
in another arraylist. Essentially, I want to load the second arraylist with only one of each of the possible elements in the first arraylist. Thus if singlearraylist contains "ww", "l2", "s1", "ww", "cc", "s1" I only want marraylist to have "ww", "l2", "s1", "cc". The search seems to fail regardless what I do, and the result is that marraylist has virtually all
of the elements from singlearraylist. Here's my code: For i = 0 To singlearraylist.Count - 1
searchobj = singlearraylist(i)
ifind = marraylist.BinarySearch(searchobj)
If ifind < 0 Then ' ie, not found
marraylist.Add(singlearraylist(i))
End If
Next
Tx for any help.
Bernie Yaeger
Hi Bernie,
There's a more efficient way of doing this which doesn't requires any
searching or sorting (except once at the start).
Sort the singlearraylist. Then go through the sorted array copying values
unless they have already been copied.
Dim sLastValue As String = ""
For Each sValue As String In singlearraylist
If sValue <> sLastValue Then
marraylist.Add (sValue)
sLastValue = sValue
End If
Next
Regards,
Fergus
Bernie,
In addition to Fergus's comments.
Have you considered using a SortedList instead of an ArrayList.
As SortedLists are automatically sorted and support finding the item by a
key.
The problem is if the objects you are dealing with do not have a good 'key'
or do not make good 'keys'.
If the objects don't really have keys I would use Fergus's idea.
Hope this helps
Jay
"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message
news:Oc**************@TK2MSFTNGP11.phx.gbl... OK, figured it out - I have to continually sort the arraylist as I add items - binarysearch only works against a sorted arraylist.
Tx - Bernie
"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl... I'm having difficulty searching an arraylist for certain values that are in another arraylist. Essentially, I want to load the second arraylist
with only one of each of the possible elements in the first arraylist. Thus
if singlearraylist contains "ww", "l2", "s1", "ww", "cc", "s1" I only want marraylist to have "ww", "l2", "s1", "cc". The search seems to fail regardless what I do, and the result is that marraylist has virtually
all of the elements from singlearraylist. Here's my code: For i = 0 To singlearraylist.Count - 1
searchobj = singlearraylist(i)
ifind = marraylist.BinarySearch(searchobj)
If ifind < 0 Then ' ie, not found
marraylist.Add(singlearraylist(i))
End If
Next
Tx for any help.
Bernie Yaeger
Tx Fergus, Jay,
A sortedlist would not apply here, correct, as making good keys would be a
problem. Tx to you both.
Bernie
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:uy**************@TK2MSFTNGP11.phx.gbl... Bernie, In addition to Fergus's comments.
Have you considered using a SortedList instead of an ArrayList.
As SortedLists are automatically sorted and support finding the item by a key.
The problem is if the objects you are dealing with do not have a good
'key' or do not make good 'keys'.
If the objects don't really have keys I would use Fergus's idea.
Hope this helps Jay
"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message news:Oc**************@TK2MSFTNGP11.phx.gbl... OK, figured it out - I have to continually sort the arraylist as I add items - binarysearch only works against a sorted arraylist.
Tx - Bernie
"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl... I'm having difficulty searching an arraylist for certain values that
are in another arraylist. Essentially, I want to load the second arraylist with only one of each of the possible elements in the first arraylist.
Thus if singlearraylist contains "ww", "l2", "s1", "ww", "cc", "s1" I only
want marraylist to have "ww", "l2", "s1", "cc". The search seems to fail regardless what I do, and the result is that marraylist has virtually all of the elements from singlearraylist. Here's my code: For i = 0 To singlearraylist.Count - 1
searchobj = singlearraylist(i)
ifind = marraylist.BinarySearch(searchobj)
If ifind < 0 Then ' ie, not found
marraylist.Add(singlearraylist(i))
End If
Next
Tx for any help.
Bernie Yaeger
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex Ting |
last post by:
Hi Everybody,
I have an issue about deleting an object from an arrayList.
I've bounded a datagrid using this code
where it will first run through all of the code in loadQuestions() and bind
a...
|
by: Stephen |
last post by:
I was wondering if someone can help me with an web application design
problem. I have a aspx page which builds up an arraylist called addresses
and outputs the values in the arraylist items to a...
|
by: gane kol |
last post by:
Hi,
I have a code that creates a datatable from an arraylist, but i am getting
an error in casting in
for (int intRow = 0; intRow < alLCPlist.Count; intRow++)
{
DataRow drow =...
|
by: Peter |
last post by:
I run into this situation all the time and I'm wondering what is the
most efficient way to handle this issue:
I'll be pulling data out of a data source and want to load the data
into an array so...
|
by: JohnR |
last post by:
From reading the documentation, this should be a relatively easy thing. I
have an arraylist of custom class instances which I want to search with
an"indexof" where I'm passing an instance if the...
|
by: Sam |
last post by:
Hi All
I'm planing to write an application which allows users dynamically add
their points (say you can add upto 30,000) and then draw xy graph. Should I
use an array for my coordinate point...
|
by: fniles |
last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to
clients.
As I receive data in 1 thread, I put it into an arraylist, and then I remove
the data from arraylist and send it...
|
by: Christopher H |
last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as
value, but I still can't get my program to work like I want it to.
Simple example:
...
|
by: =?Utf-8?B?SkI=?= |
last post by:
Hello
My pgm1 (User Interface Level) passes an empty ArrayList to pgm2
(Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate
the ArrayList.
Question1: When pgm2 gets...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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: 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...
| |