Results 1 to 3 of 3
  1. #1
    gunitinug is offline Member
    Join Date
    Dec 2016
    Rep Power

    Default Using Arrays.asList

    Java Code:
    import java.util.*;
    public class AddStuff {
      public static double addUp(List<? extends Number> addfrom) {
        double counter=0.0;
        for (Number n : addfrom) {
        return counter;
      public static void main(String[] args) {
        List<Integer> addfrom = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        double sum=addUp(addfrom);
    Here if I change

    Java Code:
    List<Integer> addfrom = Arrays.asList(1,2,3,4,5,6,7,8,9,10);

    Java Code:
    ArrayList<Integer> addfrom = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
    It fails.

    Error message is
    /tmp/java_wXBCrd/ error: incompatible types: no instance(s) of type variable(s) T exist so that List<T> conforms to ArrayList<Integer>
    ArrayList<Integer> addfrom = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
    where T is a type-variable:
    T extends Object declared in method <T>asList(T...)
    1 error


    Signature of Arrays.asList is
    Java Code:
    public static <T> List<T> asList(T... a)
    Something to do with polymorphism... should read up.

    Java Code:
    public class Parent {}
    public class Child extends Parent {}
    public class Main {
      public static void main(String[] s) {
      	// polymorphism
        Parent c1 = new Child();
        Child c2 = new Child();
        Child c3 = new Parent();
    Child c3 is erroneous. Maybe this is why?
    Last edited by gunitinug; 05-09-2017 at 09:04 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Using Arrays.asList

    As you show, the signature says it returns a List<T>.
    It doesn't say it returns an ArrayList<T>.
    asList could return any sort of List, and you shouldn't care.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    @timtoday is offline Member
    Join Date
    Apr 2017
    Rep Power

    Default Re: Using Arrays.asList

    To add to what has been said. List is actually an interface so you can instantiate like you showed in the your example trying to illustrate "polymorphism".

Similar Threads

  1. what does asList(new Object[][]{..}) return?
    By esolve in forum Advanced Java
    Replies: 2
    Last Post: 12-08-2016, 09:21 AM
  2. asList
    By manou2324 in forum New To Java
    Replies: 8
    Last Post: 04-03-2013, 01:09 PM
  3. A question on Arrays.asList()
    By fatabass in forum New To Java
    Replies: 11
    Last Post: 02-06-2012, 11:23 PM
  4. Arrays.asList(...) Problem
    By plm-pusik in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 01:12 AM
  5. about Arrays.asList source
    By geniusxiayi in forum Advanced Java
    Replies: 2
    Last Post: 04-13-2009, 07:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts