This is VERY basic but I had to re-learn it anyways:
.length()
is a method invoked from an object or a class while .length
is a an array property (or at least that's how I would like to call it)For example,
.length()
is a method of the String class to retrieve the number of characters in a string. We usually call it when playing around with strings for example:public int last2(String str) { if (str.length() <= 2) return 0; String last = str.substring(str.length()-2); int counter =0; for(int i=0;i<str.length()-2;i++) { if (str.substring(i,i+2).equals(last)) counter++; } return counter; }On the other hand,
.length
is used to retrieve the number of elements in an array. We usually use it to play around with an array of objects, let's say for example an array of integers:
public boolean arrayFront9(int[] nums) { for(int i=0;i<nums.length;i++) if (i<4 && nums[i] == 9) return true; return false; }
And that's it... I have to remember not to forget this from this point forward.
No comments:
Post a Comment