473,657 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search Arraylist in Arraylist.

Hi all,

I've an arraylist A1 that contains {0,1,2,3,4,5,9, 8,7}
I've another arraylist A2 that contains {4,5,9}

I would like to search whether if A2 is present in A1 and maybe return the
first occurance index. Can anyone advise on this?

Thanks,
Simon
Feb 3 '06 #1
11 16248
Simon,

I would use this method.

http://msdn2.microsoft.com/en-us/lib...t.indexof.aspx

I hope this helps,

Cor
Feb 3 '06 #2
Cor,

I've tried this but doesn't seem to be working. I think what this function
does is that it searches through each items in my A1 for the exact matches
of the whole of A2. That is to say:

Items in A1: Items in A2:
0 {4,5,9}
1
2
3
4
5
9
8
7

It searches each items in A1 for an arraylist of {4,5,9}. Instead, I need
something like

Items in A1: Items in A2:
0 4
1 5
2 9
3
4
5
9
8
7

Sorry I can't explain too well, but any other ways?

--Simon

"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Simon,

I would use this method.

http://msdn2.microsoft.com/en-us/lib...t.indexof.aspx

I hope this helps,

Cor

Feb 3 '06 #3
Simon Says wrote:
Hi all,

I've an arraylist A1 that contains {0,1,2,3,4,5,9, 8,7}
I've another arraylist A2 that contains {4,5,9}

I would like to search whether if A2 is present in A1 and maybe
return the first occurance index. Can anyone advise on this?


Are you saying that the elements in A1 and A2 are unique and in order? Are
A1 and A2 of trivial length?

if count(A2)>count (A1) then no match
take first element of A2 and find it in A1
check there are enough elements remaining in A1 for a match to be possible
take subsequent elements of A2 and compare them to subsequent elements of A1

Andrew
Feb 3 '06 #4
Simon,

First of all, I do probably not understand what you want.

A try
\\\
Dim a1 As New ArrayList
a1.AddRange(New Object() {0, 1, 2, 3, 4, 5, 9, 6, 8, 7})
Dim a2 As New ArrayList
a2.AddRange(New Object() {4, 5, 9})
For i As Integer = 0 To a2.Count - 1
Dim ind As Integer = a1.IndexOf(a2(i ))
If ind > -1 Then
MessageBox.Show (ind.ToString)
End If
Next
///

I hope this helps,

cor
Feb 3 '06 #5
Can't understand myself too :-) Anyway, I've tried something like,

//
dim a1 as new ArrayList
dim a2 as new ArrayList

a1.AddRange(New Integer() {0,1,2,3,4,5,9, 8,7})
a2.AddRange(New Integer() {4,5,9})

if a1.IndexOf(a2) > -1 then
call msgbox("Found")
else
call msgbox("Not Found")
End if
//

The result I got is "Not Found". I think with IndexOf function it searches
throught a1 in following sequence:

0 ----> {4,5,9}
1 ----> {4,5,9}
2 ----> {4,5,9}
....
7 ----> {4,5,9}

Therefore, I will not be able to get a match. If Cor's method is the only
way, I will try to change my implementation as O(n*m) performance isn't
quite impressive - O(n) for IndexOf function call and O(m) for looping
through a2.

Any more insights?

--Simon

"Andrew Morton" <ak*@in-press.co.uk.inv alid> wrote in message
news:Ow******** *****@TK2MSFTNG P12.phx.gbl...
Simon Says wrote:
Hi all,

I've an arraylist A1 that contains {0,1,2,3,4,5,9, 8,7}
I've another arraylist A2 that contains {4,5,9}

I would like to search whether if A2 is present in A1 and maybe
return the first occurance index. Can anyone advise on this?


Are you saying that the elements in A1 and A2 are unique and in order? Are
A1 and A2 of trivial length?

if count(A2)>count (A1) then no match
take first element of A2 and find it in A1
check there are enough elements remaining in A1 for a match to be possible
take subsequent elements of A2 and compare them to subsequent elements of
A1

Andrew

Feb 3 '06 #6
> "Andrew Morton" wrote
Are A1 and A2 of trivial length?


If so, convert them to strings (somehow) and use string functions on them.

Using ArrayList.Index Of(someArrayLis t) won't work because it looks for an
ArrayList entity within your ArrayList, but the entities in your array list
are numbers.

Andrew
Feb 6 '06 #7
Hi Andrew,

I can't convert them to string as the data I'm working on is network
streaming bytes that contains compressed data. Converting them to string
will sort of mess up my compressed data. All my streaming bytes will be
stored in A1 which will varies in size, and I will check for delimiter -
A2 - which will be of fixed length.

I've already worked out a helper function as suggested by Cor, but the
O(n*m) performance just worries me a little.

--Simon

"Andrew Morton" <ak*@in-press.co.uk.inv alid> wrote in message
news:e%******** ********@TK2MSF TNGP14.phx.gbl. ..
"Andrew Morton" wrote

Are A1 and A2 of trivial length?


If so, convert them to strings (somehow) and use string functions on them.

Using ArrayList.Index Of(someArrayLis t) won't work because it looks for an
ArrayList entity within your ArrayList, but the entities in your array
list are numbers.

Andrew

Feb 7 '06 #8
Simon Says wrote:
I can't convert them to string as the data I'm working on is network
streaming bytes that contains compressed data. Converting them to
string will sort of mess up my compressed data. All my streaming
bytes will be stored in A1 which will varies in size, and I will
check for delimiter - A2 - which will be of fixed length.


Imagine streem is your incoming data and eos is the sequence indicating the
end of the packet in the stream. In your case they would be bytes, but for
my convenience I've used strings, so remember that VB strings have their
first character at position 1, not 0.

Dim streem As String = "46324682492147 47473773"
Dim eos As String = "324"
Dim c As String

Dim eosPos As Integer = 1 ' position in eos
Dim streemPos As Integer = 1 ' position in streem
Dim dataEnd As Integer ' the length of the data, less the eos marker

While streemPos <= Len(streem)
streemPos += 1
c = Mid(streem, streemPos, 1)
If c = Mid(eos, eosPos, 1) Then
eosPos += 1
If eosPos > Len(eos) Then
' we have found the end of the data
dataEnd = streemPos - Len(eos)
Exit While
End If
Else
eosPos = 1
End If
End While

Andrew
Feb 7 '06 #9
Since my data is in byte, my values are in the range of 0-255. By converting
my stream of data to string (also tried based64String) will totally messed
up my data.

What I've tried was convert my byte() to string (using
Convert.ToBased 64String) then find the delimiter position and extract out
the data portion, then convert the data portion back to byte(). It just
messed up my compressed data.
"Andrew Morton" <ak*@in-press.co.uk.inv alid> wrote in message
news:uL******** ******@TK2MSFTN GP11.phx.gbl...
Simon Says wrote:
I can't convert them to string as the data I'm working on is network
streaming bytes that contains compressed data. Converting them to
string will sort of mess up my compressed data. All my streaming
bytes will be stored in A1 which will varies in size, and I will
check for delimiter - A2 - which will be of fixed length.


Imagine streem is your incoming data and eos is the sequence indicating
the end of the packet in the stream. In your case they would be bytes, but
for my convenience I've used strings, so remember that VB strings have
their first character at position 1, not 0.

Dim streem As String = "46324682492147 47473773"
Dim eos As String = "324"
Dim c As String

Dim eosPos As Integer = 1 ' position in eos
Dim streemPos As Integer = 1 ' position in streem
Dim dataEnd As Integer ' the length of the data, less the eos marker

While streemPos <= Len(streem)
streemPos += 1
c = Mid(streem, streemPos, 1)
If c = Mid(eos, eosPos, 1) Then
eosPos += 1
If eosPos > Len(eos) Then
' we have found the end of the data
dataEnd = streemPos - Len(eos)
Exit While
End If
Else
eosPos = 1
End If
End While

Andrew

Feb 8 '06 #10

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

Similar topics

0
2466
by: Alessandro Rossi | last post by:
I've an arraylist of strings. I need to search a partial string in the items of this arraylist. e.g i've 3 elements (frank, marc, steve). i want find frank having only "fra", like the "like" instruction in SQL. thank you so much Alessandro Rossi
4
7107
by: Ben Fidge | last post by:
Hi What is the most efficient way to gather a list of files under a given folder recursively with the ability to specify exclusion file masks? For example, say I wanted to get a list of all files under \Program Files and it's subdirectories that meet the following criteria:
18
4727
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
10
9678
by: JohnR | last post by:
I have an arraylist of string values. I would like to search the arraylist to find the index of a particular string and I would like the search to be case insensitive. dim al as new arraylist al.add("one") al.add("two") dim x as integer = al.indexof("ONE") I would like this to find the match and return 0
0
911
by: Matt | last post by:
I have a class that was generated from a XSD schema. The class has a collection of items or children. In this child collection I have a field named "ID". I would like to implement a search like ArrayList.BinarSearch in this class so I can search for a specific "ID". I am also planning on searching on other fields but I wanted to state this as simplistically as possible. I tried to implement the arraylist search but have been...
4
1509
by: Jon | last post by:
Hi, I have an arrayList that holds an ArrayObject with (Qty & ItemCode). e.g. arrayList.Add(New ArrayObject(Qty, ItemCode) However, I want to search the arrayList and check if the ItemCode already exist on the arrayList, then simply increase the Qty by 1. For example: I have in my arraylist:
0
1964
by: DarkBlaZeR | last post by:
Hi all, This steps I made: 1. Read a file in 2. Put each line of the file in an arraylist 3. Make a collection of the arraylist so you can sort them 4. Search for a word Now my problem is, the text file will be something like this:
1
1893
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
3094
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...
5
1469
by: blisspikle | last post by:
I figure that someone good at dotnet can look at this and give me a clue on how to easily organize this code? If there is a unique identifier like "Publisher" with a bunch of "Book" that are published under them (I used the arraylist class in the publisher class). How should the code be organized, and how can the books properties like "Name" be easily called in the main code, or searched for in the main code?
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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
8605
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
7324
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
6163
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
5632
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();...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.