159k views
18 votes
Which of these method of ArrayList class is used to obtain present size of an object/

a. size()
b. length()
c. index()
d. capacity()

2 Answers

12 votes

Final answer:

The method used to obtain the present size of an object in an ArrayList is the size() method. It returns the number of elements currently in the list, whereas length is a property of arrays, and index and capacity are not applicable methods of the ArrayList class.

Step-by-step explanation:

The method of the ArrayList class used to obtain the present size of an object is size(). This method returns the number of elements in the ArrayList. Unlike arrays that have a fixed length and use the length field to determine their size, the ArrayList class provides the size() method to return the current number of elements.

For example, if you have an ArrayList called myList and you want to find out how many elements it contains, you would use the following code:

int numberOfElements = myList.size();

Options b. length(), c. index(), and d. capacity() are not methods of the ArrayList class; length is a property of arrays, index is not a standard method or property, and capacity() pertains to initial capacity, which is a concept not directly exposed by the ArrayList.

User Csmosx
by
3.1k points
13 votes

Answer:

The method of ArrayList class which is used to obtain the present size of an object is:

a. size()

Step-by-step explanation:

In ArrayList, the size() method provides the number of objects available in the collection, array, or list. In Java programming, ArrayList forms a part of the collection framework and has a set of methods to access elements and modify them. An ArrayList does not have a fixed-size data structure, unlike an Array, but continues to expand in size.

User Christopher Geary
by
3.8k points