I know what ArrayList<T> is used for, but when should I use ArrayList<?> ? can you explain with example? thanks.
I know what ArrayList<T> is used for, but when should I use ArrayList<?> ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As far as I’ve been able to tell,
ArrayList<?>basically tells the compiler:Update
I just learned that there is a real difference between using Raw Types (
ArrayList) and generics with wildcards (ArrayList<?>) that goes beyond avoiding compiler warnings. Apparently once you declare something as a Raw type, no generics will work on any methods of that type, even if the generics weren’t based on the type you omitted. See here for an example.So while my original answer was generally correct, I thought it would be important to mention that using
ArrayList<?>instead ofArrayListis more than just a matter of removing compiler warnings.