4.3.8. Members of an array

Array objects have the following public members:

  • length—The public variable length stores the number of elements of the array.
  • clone()—This method overrides the method clone defined in the class Object but doesn’t throw checked exceptions. The return type of this method is the same as the array’s type. For example, for an array of type Type[], this method returns Type[].
  • Inherited methods— Methods inherited from the class Object, except the method clone.

As mentioned in the earlier section on the String class, a String uses the method length() to retrieve its length. With an array, you can use the array’s variable length to determine the number of the array’s elements. In the exam, you may be tricked by code that tries to access the length of a String using the variable length. Note the correct combination of class and member used to access its length:

  • String—Retrieve length using the method length()
  • Array— Determine element count using the variable length

I have an interesting way to remember this rule. As opposed to an array, you’ll invoke a lot of methods on String objects. So you use the method length() to retrieve the length of String and the variable length to retrieve the length of an array.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.50.156