Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between size and length methods?

What is the difference between .size() and .length ? Is .size() only for arraylists and .length only for arrays?

like image 625
BeLambda Avatar asked Nov 25 '13 12:11

BeLambda


People also ask

What is the difference between length () and size () of the list?

Difference between length of array and size() of ArrayList in Java. ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection. Array has length property which provides the length or capacity of the Array.

What is the difference between the length field for an array and the size () method for an ArrayList?

ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection. Array has length property which provides the length or capacity of the Array. It is the total space allocated during the initialization of the array.

What is the difference between length () and size () in C++?

The size() function is consistent with other STL containers (like vector, map, etc.) and length() is consistent with most peoples intuitive notion of character strings like a word, sentence or paragraph. We say a paragraph'ss length not its size, so length() is to make things more readable.

What is the length method?

length() : length() method is a final variable which is applicable for string objects. The length() method returns the number of characters present in the string. 1. The length variable is applicable to an array but not for string objects whereas the length() method is applicable for string objects but not for arrays.


1 Answers

size() is a method specified in java.util.Collection, which is then inherited by every data structure in the standard library. length is a field on any array (arrays are objects, you just don't see the class normally), and length() is a method on java.lang.String, which is just a thin wrapper on a char[] anyway.

Perhaps by design, Strings are immutable, and all of the top-level Collection subclasses are mutable. So where you see "length" you know that's constant, and where you see "size" it isn't.

like image 188
MattPutnam Avatar answered Sep 30 '22 09:09

MattPutnam