Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.util.List;
import java.util.ArrayList;
import static java.util.Arrays.asList;
public class StaticImports {
    public static void main(String[] args) {
        List<Integer> l=new ArrayList<>(asList(10,20,30));
        System.out.println(l);
    }
}


Output :
[10,20,30]

What I have tried:

Java
import java.util.List;
import java.util.ArrayList;
import static java.util.Arrays.asList;
public class StaticImports {
    public static void main(String[] args) {
        ArrayList<Integer> al1=new ArrayList<>();
        al1=asList(10,20,30);//Error: incompatible type
        System.out.println(al1);
     }
}


Could anyone explain me why asList() works fine for List but not for ArrayList ? Thanks in advance :)
Posted
Updated 14-Sep-18 21:42pm
v2
Comments
Patrice T 15-Sep-18 3:14am    
Have you read documentation ?
@k5hu 15-Sep-18 3:43am    
yes. But I couldn't get it because its mentioned asList has a generic of type T( the class of object in the array) but why can't it accept ArrayList object alone ?

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900