Comparing ArrayLists

G

Guest

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.
 
B

Bill Butler

JoshP said:
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
 
P

Peter Duniho

[...]
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, it really depends on the structure of your arrays. But AFAIK
there's no built-in thing to do what you're asking, regardless of what
you're actually asking.

Note that your question as stated is somewhat ambiguous. How best to
solve the problem depends on the rules of the structure of the arrays.
In the sample data you've provided, the second array is exactly the
same as the first, except with additional elements inserted. Is this
how the data will always look?

If so, then you can simply run the comparison until you reach the end
of the reference array, and then run your deletion logic on whatever
was left. For example, put this after your first loop:

while (indexJ < oFound.Count)
{
// file deletion stuff here
indexJ++;
}

If not, then you need to be more specific about what the data can
support. An algorithm that would completely support any two arrays,
where the order of the elements in each doesn't matter would simply
enumerate the first array, using the values in the first array to
remove items from the second array. Then when that part is done, you'd
just go run the deletion logic on anything left in the second array.
For example:

Dictionary<string, object> dictDelete = new Dictionary<string,
object>(oFound.Count);

// Create a dictionary containing all of the found objects
foreach (object obj in oFound)
{
dictDelete.Add(obj.ToString(), obj);
}

// Remove all the approved objects from the dictionary
foreach (object obj in oApproved)
{
dictDelete.Remove(obj.ToString());
}

// Delete all of the files corresponding to the remaining objects
foreach (object obj dictDelete.Values)
{
// file deletion stuff here
}

Pete
 
C

Chris Shepherd

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.
 
J

j1mb0jay

Chris said:
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
 
G

Guest

Peter, I didn't even think about doing that. Removing all the approved
values from the found values and then deleting was is left appears to be an
excellent alternative. Thank you.

Peter Duniho said:
[...]
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, it really depends on the structure of your arrays. But AFAIK
there's no built-in thing to do what you're asking, regardless of what
you're actually asking.

Note that your question as stated is somewhat ambiguous. How best to
solve the problem depends on the rules of the structure of the arrays.
In the sample data you've provided, the second array is exactly the
same as the first, except with additional elements inserted. Is this
how the data will always look?

If so, then you can simply run the comparison until you reach the end
of the reference array, and then run your deletion logic on whatever
was left. For example, put this after your first loop:

while (indexJ < oFound.Count)
{
// file deletion stuff here
indexJ++;
}

If not, then you need to be more specific about what the data can
support. An algorithm that would completely support any two arrays,
where the order of the elements in each doesn't matter would simply
enumerate the first array, using the values in the first array to
remove items from the second array. Then when that part is done, you'd
just go run the deletion logic on anything left in the second array.
For example:

Dictionary<string, object> dictDelete = new Dictionary<string,
object>(oFound.Count);

// Create a dictionary containing all of the found objects
foreach (object obj in oFound)
{
dictDelete.Add(obj.ToString(), obj);
}

// Remove all the approved objects from the dictionary
foreach (object obj in oApproved)
{
dictDelete.Remove(obj.ToString());
}

// Delete all of the files corresponding to the remaining objects
foreach (object obj dictDelete.Values)
{
// file deletion stuff here
}

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top