I want to declare an ArrayList of type int.
Why does the following give me an error:
ArrayList<int> list1 = new ArrayList<int>();
But the following works:
ArrayList<Integer> list1 = new ArrayList<Integer>();
?
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.
ArrayListcan only reference types, not primitives.Integeris a class, not a primitive.When you declare
ArrayList<Integer> list1 = new ArrayList<Integer>(), you’re creating anArrayListwhich will store theIntegertype, not theintprimitive.If you want to read about the difference between primitive and reference types, check out http://pages.cs.wisc.edu/~hasti/cs302/examples/primitiveVsRef.html