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

Comparing ArrayLists

Hi All, I am attempting to compare values in two arraylists to make sure all
the values are the same. I am running into trouble with my code if both
arraylists compare okay up until a point and I am looping using the length of
the primary arraylist. For example:

oApproved (3 elements here):
[0]Red
[1]Green
[2]Yellow

oFound (5 elements here):
[0]Red
[1]Green
[2]Yellow
[3]Orange
[4]Blue

If using my loop below, the comparison will exit before it sees the last two
elements of arraylist 2 should not be there and should be removed.

for (indexI = 0; indexI <= oApproved.Count - 1; )
{
if (oFound[indexJ].ToString() ==
oApproved[indexI].ToString())
{
//TODO: remove debug
//RaiseVerificationEvent("Debug++File found: " +
oFound[indexJ].ToString());
//System.Diagnostics.Debug.WriteLine("File Approved:
" + oFound[indexJ].ToString());
indexI += 1;
indexJ += 1;
RaiseVerificationEvent(indexI.ToString() + " ------
" + indexJ.ToString());
}
else
{
CFileIO.DeleteFile(oFound[indexJ].ToString());
RaiseVerificationEvent("Unapproved file: " +
oFound[indexJ].ToString() + " deleted.");
//TODO: remove debug
System.Diagnostics.Debug.WriteLine("File Unapproved:
" + oFound[indexJ].ToString());
indexJ += 1;
}
}
I can see the flaw in my logic is the "for (indexI = 0; indexI <=
oApproved.Count - 1; )" because it will stop comparing when it reaches the
length of the primary ArrayList (in this case oApproved). Is there a better
way to compare arraylists? Or maybe a proven looping structure for comparing
arrays?

Thank You.
Oct 31 '07 #1
3 2045

"JoshP" <Jo***@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Hi All, I am attempting to compare values in two arraylists to make
sure all
the values are the same. I am running into trouble with my code if
both
arraylists compare okay up until a point and I am looping using the
length of
the primary arraylist. For example:

oApproved (3 elements here):
[0]Red
[1]Green
[2]Yellow

oFound (5 elements here):
[0]Red
[1]Green
[2]Yellow
[3]Orange
[4]Blue

If using my loop below, the comparison will exit before it sees the
last two
elements of arraylist 2 should not be there and should be removed.
<snip>

You could first check to see if the two Arrays are the same length.
If they are of different length they obviously are not the same.
Of course, if you allow duplicates in your lists then this will not
work.

Bill
Oct 31 '07 #2
JoshP wrote:
[...]
oApproved (3 elements here):
[0]Red
[1]Green
[2]Yellow

oFound (5 elements here):
[0]Red
[1]Green
[2]Yellow
[3]Orange
[4]Blue

If using my loop below, the comparison will exit before it sees the last two
elements of arraylist 2 should not be there and should be removed.
[...]
I can see the flaw in my logic is the "for (indexI = 0; indexI <=
oApproved.Count - 1; )" because it will stop comparing when it reaches the
length of the primary ArrayList (in this case oApproved). Is there a better
way to compare arraylists? Or maybe a proven looping structure for comparing
arrays?
Well, this question is kind of vague.

If you're looking to ensure that the elements in oApproved exist in
oFound then you should use the Contains method to verify an element
exsists in the oFound list.

Chris.
Nov 1 '07 #3
Chris Shepherd wrote:
JoshP wrote:
[...]
>oApproved (3 elements here):
[0]Red
[1]Green
[2]Yellow

oFound (5 elements here):
[0]Red
[1]Green
[2]Yellow
[3]Orange
[4]Blue

If using my loop below, the comparison will exit before it sees the
last two elements of arraylist 2 should not be there and should be
removed.
[...]
>I can see the flaw in my logic is the "for (indexI = 0; indexI <=
oApproved.Count - 1; )" because it will stop comparing when it reaches
the length of the primary ArrayList (in this case oApproved). Is
there a better way to compare arraylists? Or maybe a proven looping
structure for comparing arrays?

Well, this question is kind of vague.

If you're looking to ensure that the elements in oApproved exist in
oFound then you should use the Contains method to verify an element
exsists in the oFound list.

Chris.
try looking at the big O theroy

j1mb0jay
Nov 1 '07 #4

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

Similar topics

2
by: Robert Linder | last post by:
I am trying to compare arraylists in csharp. I setup a simple test with empty arraylists. string x1 = {}; string x2 = {}; Console.WriteLine( x1 == x2 ); // false...
1
by: DOTNETGUY | last post by:
Is there a way to compare the elements in two arraylists for the similiarities? I know i can do a loop and check but i was wondering if there is an easier way in .NET. Thank you.
5
by: drdave | last post by:
I would like to have ten arraylists created within a loop.. is there a conversion or something I can do to acheive this.. pseudo: Dim counter As Integer = 0 Dim ArrName As ArrayList ...
2
by: darrel | last post by:
I have two comma delimted strings that I need to compare individual values between the two. I assume the solution is likely to put them into an array? If so, do I need to loop through one,...
0
by: steve | last post by:
I'm looking for a code example how to compare the values in a given record in different arraylists two arraylists, two fields in each record, both defined as string I'm thinking that it's...
3
by: steve | last post by:
I need to compare the value of a field in a row on an arraylist with the value of a field on a second arraylist I have this bit of code working for arrays but cant get it working for arraylists The...
4
by: Andy in S. Jersey | last post by:
I would like to create an unknown number of Arraylists, meaning, I don't know how many I should create until runtime. I will be reading a table, and 0,1,2, or more fields have to be put into...
1
by: Newbie19 | last post by:
I'm just learning java arrays/arraylists and was wondering what are the best books for learning java arrays/arraylists? I know practice is the best way to learn, but I have a hard time...
3
by: chanshaw | last post by:
Alright I need a little help here I want to compare to arraylists and if a match occurs return the value of whatever item matched as a string. Thanks alot.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.