2

i am beginning Collection Frameworks in java, I have a simple code but its make me curious about what maybe possible arguments for generic classes.

import java.util.ArrayList;
import java.util.LinkedList;

class ListExa{
public static void main(String[] args){
    ArrayList<int> names = new ArrayList<int>();
    names.add(1);
    names.add(2);
    names.add(3);
    names.add(4);
    names.add(5);

    System.out.println(names);

 }
}

ListExa.java:6: error: unexpected type
                ArrayList<int> names = new ArrayList<int>();    
required: reference  
found:    int
Avinash Kumar
  • 288
  • 6
  • 21

5 Answers5

2

You need to use the class Integer instead of primitive type int :


Since jdk1.7 you can use diamond operator <> to avoid code duplicate :

ArrayList<String> names = new ArrayList<>(); 
                                       ^^  
azro
  • 53,056
  • 7
  • 34
  • 70
2

This is not a completely new answer, more of a sum-up of what's already said.

With the Java Collections Framework, as the type parameter (in the angle brackets) you name what type of objects you want to store in the Collection (List, Set, ...).

By the language design, you cannot use primitive types like int, long, double etc. there, only reference types. So, if you want to store integers in an ArrayList, you have to do ArrayList<Integer> instead of ArrayList<int>.

But later in your code, you want to add Strings to your list. To allow that, your list should be declared ArrayList<String>. An ArrayList<Integer> will give a compile error when trying to add anything that is not an Integer.

You can of course declare an ArrayList<Object>. That one will accept all types of objects (String, Integer, Person, File, ...), but that's against the idea behind generics: to tell the compiler as specifically as possible what type of objects you want to allow in your list (and disallow the others), avoiding strange runtime errors.

And it's good style to declare something like

List<String> names = new ArrayList<>();

or

Collection<String> names = new ArrayList<>();

If later you decide that a LinkedList or a HashSet is the data structure that better suits your program's need, then you just change the right-hand side of the declaration.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
1

You should use a wrapper class for Integers, not primitive types:

ArrayList<Integer> names = new ArrayList<Integer>();

For more details check here: Why don't Java Generics support primitive types?

Kostas Stamos
  • 175
  • 1
  • 3
  • 16
0
ArrayList<int> names = new ArrayList<int>();

this is not working because this takes parameter as an OBJECT but when we pass int, it is primitive data type.

Java has Wrapper class that builds in into OBJECT:

int -> Integer and so on.

so correct way will be :

ArrayList<Integer> names = new ArrayList<>();
atline
  • 28,355
  • 16
  • 77
  • 113
-1

collection supports object only , use Integer instead of int

as best practice we should not write like this ArrayList<Integer> names = new ArrayList<Integer>();

means always give reference of super so it should be like this

List<Integer> names = new ArrayList<Integer>();
user2862544
  • 415
  • 3
  • 13
  • his first line was itself wrong so i corrected that and obviously if he is adding strings so it should be List names = new ArrayList(); – user2862544 Aug 08 '17 at 17:45