164k views
3 votes
Which of the following is a CORRECT statement to create an object of ArrayList generic class?

A. ArrayList list = new ArrayList();
B. ArrayList list = new ArrayList():
C. ArrayList list = new ArrayList();
D. ArrayList (Integer) list = new ArrayList(Integer)<>;

1 Answer

3 votes

Final answer:

The correct syntax for creating an ArrayList object is 'ArrayList list = new ArrayList();'. This is a generic declaration without type parameters and will create an ArrayList that can hold any type of Objects.

Step-by-step explanation:

The correct statement to create an object of the ArrayList generic class is:

A. ArrayList list = new ArrayList();

Option A is the correct syntax for creating an ArrayList object in Java without specifying the type parameter. It is important to specify the type parameter for type safety, but if not specified, the list will be treated as an ArrayList of Object types. The correct, type-safe way to declare an ArrayList of Integer would be:

ArrayList list = new ArrayList();

Or, using the diamond operator to take advantage of type inference introduced in Java 7:

ArrayList list = new ArrayList<>();

However, since the student's options did not include type parameters, option A is the best answer according to the provided choices.

User Adam Heath
by
8.6k points