Last Updated : 10 Apr, 2024

Given below are some declarations of ArrayList. Which of the following declarations are syntactically correct:

1. ArrayList<Integer> list = new ArrayList<Integer>();
2. ArrayList list<Integer> = new ArrayList<>();
3. ArrayList<Integer> list = new ArrayList<>();

(A) 2 and 3
(B) 1 and 3
(C) 1 and 2
(D) 1, 2 and 3


Answer: (B)

Explanation:

ArrayList arraylist_name = new ArrayList();
ArrayList arraylist_name = new ArrayList<>();

above are the two ways to declare arraylist

ArrayList list = new ArrayList<>(); gives error.



Share your thoughts in the comments