Difference between size and length methods?
Asked Answered
G

7

68

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

Girlfriend answered 25/11, 2013 at 12:15 Comment(2)
This seems programming language specific, and there's no tag.Skewback
The canonical answer: #301022 This one is also good: https://mcmap.net/q/224420/-length-and-length-in-javaEtty
P
100

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.

Prying answered 14/2, 2015 at 3:6 Comment(4)
"which is just a thin wrapper on a String[] anyway". did you mean char[]?Dawes
I know this thread is mad old, but I think Matt is right and the difference has to do with how they are stored in memory. Array elements are stored sequentially, and the way you access them is base + offset where base is the start (index 0); this also makes arrays faster, relatively speaking. ArrayLists are in the linked list family, and are not stored sequentially, which is why they are "resizable." So, even though conceptually, linked lists have a "length," they are not stored in memory as a "list" and so they use the more generic "size" instead.Azole
string[] should be char[]Sammysamoan
@MattPuntam: as you say and Java also; size() is a method specified in java.util.Collection, but I didn't get where length is defined. in Array. it's my interview question. can you please demonstrate it to clarify mine douts?Cockadoodledoo
T
46

length is constant which is used to find out the array storing capacity not the number of elements in the array

Example:

int[] a = new int[5]

a.length always returns 5, which is called the capacity of an array. But

number of elements in the array is called size

Example:

int[] a = new int[5]
a[0] = 10

Here the size would be 1, but a.length is still 5. Mind that there is no actual property or method called size on an array so you can't just call a.size or a.size() to get the value 1.

The size() method is available for collections, length works with arrays in Java.

Taverner answered 1/4, 2017 at 7:38 Comment(4)
You cannot invoke size() for a primitive array such as int[]. Thus a.size=1 doesn't make sense.Georgianngeorgianna
See https://mcmap.net/q/282106/-java-array-attribute-size for a discussion of this answer.Megasporangium
@Chinmayjain: that wasn't an assignment; that was just saying what the result would be.Govea
@Govea even if it's not assignment, it's still wrong. There's simply no such thing as a.size. It just doesn't exist, so to claim that it "would be" 1 is meaningless.Kyrakyriako
O
10
  • .length is a field, containing the capacity (NOT the number of elements the array contains at the moment) of arrays.

  • length() is a method used by Strings (amongst others), it returns the number of chars in the String; with Strings, capacity and number of containing elements (chars) have the same value.

  • size() is a method implemented by all members of Collection (lists, sets, stacks,...). It returns the number of elements (NOT the capacity; some collections even don´t have a defined capacity) the collection contains.
Oscillation answered 7/1, 2020 at 13:8 Comment(0)
T
2
length variable:

In Java, array (not java.util.Array) is a predefined class in the language itself. To find the elements of an array, designers used length variable (length is a field member in the predefined class). They must have given length() itself to have uniformity in Java; but did not. The reason is by performance, executing length variable is speedier than calling the method length(). It is like comparing two strings with == and equals(). equals() is a method call which takes more time than executing == operator.

size() method:

It is used to find the number of elements present in collection classes. It is defined in java.util.Collection interface.

Theosophy answered 12/5, 2017 at 16:12 Comment(0)
A
1

Based on the syntax I'm assuming that it is some language which is descendant of C. As per what I have seen, length is used for simple collection items like arrays and in most cases it is a property.

size() is a function and is used for dynamic collection objects. However for all the purposes of using, you wont find any differences in outcome using either of them. In most implementations, size simply returns length property.

Alyse answered 25/11, 2013 at 12:29 Comment(7)
The language is Java ... and your answer is not applicable to that.Weightlessness
That tag has been added after my answer, so I couldn't have known. All the other answers which target java are given after a year of the question.Alyse
The source code in the question has always been Java ... even if you didn't recognize it as such. So your your answer has always been inapplicable. Either way, it now serves no purpose and should be deleted.Weightlessness
You are also incorrect about the tagging. According to the edit history, it was added in "Feb 14 '15 at 3:00". Two (possibly three) answers other than yours (including a deleted one) that predate the java tag have correctly identified this as Java.Weightlessness
@StephenC This is getting ridiculous. Can you see that I have answered this question on Nov 25th, 2013, 4 minutes after the question is added, When no such tag information is available? You yourself told that tag is added in 2015. Some users do assume programming language and give answer. I just gave what I think is a generic enough explanation so that it helps the candidate to the best of my knowledge. I see you have helped the community a lot. So, I respect you for that. But I would rather help other people by answering than arguing with you to boost my ego about a non-issue.Alyse
I have down-voted it because it is now objectively unhelpful. If your aim is to help people, then the appropriate course would be for you to delete it.Weightlessness
Fine by me. In my view, it is not a bad answer so not going to delete it.Alyse
U
0

I bet (no language specified) size() method returns length property.

However valid for loop should looks like:

for (int i = 0; i < values.length; i++) {}
Unapproachable answered 25/11, 2013 at 12:17 Comment(0)
U
0

In simple words length() method is used in case of string to find number of characters while the size() is to return the number of elements of list,set etc.

Unperforated answered 3/6, 2024 at 8:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.