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

Comparing arraylist

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
Console.WriteLine( x1.Equals(x2) ); // false
Console.WriteLine( x1.GetLength(0) == 0 ); // true

What is the way to compare arraylist?

What is the way to check for empty arraylist?
GetLength is bad when there is a large arraylist.

This works, but I prefer something like 'x1.IsEmpty()'
try{Console.WriteLine( x1.GetValue(0) );}
catch( System.IndexOutOfRangeException )
{Console.WriteLine( "Empty Arraylist"); }

Robert
Nov 15 '05 #1
2 5963
"Robert Linder" <rl*****@panix.com> wrote:
What is the way to compare arraylist?
I think you need to iterate over the elements and compare them individually.
What is the way to check for empty arraylist?


if (ArrayList.Count == 0) { /* it is empty */ }

P.
Nov 15 '05 #2
x1.Length == 0 will do it.

This is always very fast, even if the array is large, because the length is
stored. It is not recomputed every time.

Also, string[] is an "array", not an "ArrayList". And arrays have
"reference" semantics. So, x1 == x2 and x1.Equals(x2) will compare the
references rather than the contents of the two arrays, and will return false
in your case, unless you assign x2 to x1 (x1 = x2;).

Bruno.

"Robert Linder" <rl*****@panix.com> a écrit dans le message de
news:c1**********@reader2.panix.com...
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
Console.WriteLine( x1.Equals(x2) ); // false
Console.WriteLine( x1.GetLength(0) == 0 ); // true

What is the way to compare arraylist?

What is the way to check for empty arraylist?
GetLength is bad when there is a large arraylist.

This works, but I prefer something like 'x1.IsEmpty()'
try{Console.WriteLine( x1.GetValue(0) );}
catch( System.IndexOutOfRangeException )
{Console.WriteLine( "Empty Arraylist"); }

Robert

Nov 15 '05 #3

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

Similar topics

5
by: Joe Black | last post by:
Hi all, Using Windows CP Pro VS .net 2005 I'm creating an app that allows user to extend its functionality by installing plug in modules, these modules support an interface I have created...
2
by: David | last post by:
Hi all, How would I go about this? I am doing a document management system and I need to write a clean up routine. I will create two arrays. One of them will have a list of filenames I have...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
5
by: paolo.ponzano | last post by:
Hello, I've to implement a IComparer in order to sort an arraylist. I've used reflection to get the object I need to sort, at the moment I'm using the following piece of code: public int...
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...
10
by: netnet | last post by:
I have a collection of objects that I store in the session. Then I look in that collection to see if the collection.contains an object. it answers false. If I dont store that collection in the...
0
by: ccshine via DotNetMonster.com | last post by:
I'm working on an app that implements a Structure to store a recordset in an ArrayList. I used this setup to bind to a DataGrid and it worked out so well, I thought it might be a better solution...
3
by: =?Utf-8?B?Sm9zaFA=?= | last post by:
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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: 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
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,...

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.