3 Replies - 5343 Views - Last Post: 26 March 2012 - 08:22 AM Rate Topic: -----

#1 JavaBeanie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-October 10

C# equivalent of java ArrayList.size

Posted 26 March 2012 - 04:21 AM

Hi,

Can I check that

ArrayList.length


in C# is the same as

ArrayList.size 


in Java please?

Many thanks
Is This A Good Question/Topic? 0
  • +

Replies To: C# equivalent of java ArrayList.size

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: C# equivalent of java ArrayList.size

Posted 26 March 2012 - 05:15 AM

ArrayList.Count is what you are looking for.

However, I will note that the ArrayList in C# is not the same as the ArrayList in Java. The ArrayList in C# is not type safe. If you want type safety, use generic lists. Generic lists were added in .Net 2.0. It's commonly frowned upon to still use ArrayList in C# if you are using .Net 2.0 or greater.
Was This Post Helpful? 0
  • +
  • -

#3 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: C# equivalent of java ArrayList.size

Posted 26 March 2012 - 05:17 AM

.Length .Size and .count are all the same in C#, I can't speak for Java, despite the different naming which is just there because it makes more sense in context.

For example, list makes more sense to call the tally of items "count" than "length" as it's length is no limited like with an array.

This post has been edited by negligible: 26 March 2012 - 05:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: C# equivalent of java ArrayList.size

Posted 26 March 2012 - 08:22 AM

View Postnegligible, on 26 March 2012 - 01:17 PM, said:

.Length .Size and .count are all the same in C#, I can't speak for Java, despite the different naming which is just there because it makes more sense in context.


Say what?

.Net list based collections provide two main properties to determine the number of elements, and current maximum size of the list. Neither provide a .Length property. That is reserved for the general array. In C# -

.Count - Gets the number of elements actually contained in the ArrayList (which is what the op wants instead of .length)

.Capacity - Gets or sets the number of elements that the ArrayList can contain.

The size of an ArrayList is limited in a sense at a point in time. As the elements are still stored in a conventional array behind the scenes, at any one time there is a maximum size of the array. The length of the current array inside the ArrayList is what the .Capacity returns. As more and more elements are added to the ArrayList however (or added to the array behind the scenes), the length of the array is changed to accommodate the new elements. Again the .Capacity property will show the changes.

On the other hand the .Count property will merely return the number of elements currently inside the array (although the actual length of the array may be larger).

The ArrayList in Java however only provides access to the number of elements currently in the array through the .size() method. The class only allows the user to explicityly define the size of the background array through the .ensurecapacity() method.

There are a lot of people who get confused with the different identifiers between frameworks. .size() in Java is perhaps a bit misleading as size is not really the same as the actual number of elements in the ArrayList itself.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1