Question about addAll

0 Brenda Martis · April 4, 2015
Hi, I have executed the above code. It doesn't work for me. Can someone help me in understanding whats the mistake?


class AddAll{
public static void main(String[] args){
String[] words = {"apples","beef","corn","ham"};
List<String> list = Arrays.asList(words);

ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("Youtube");
arrayList.add("Google");
arrayList.add("Facebook");

for(String x : arrayList){
System.out.printf("%s ",x);
}
System.out.println();

Collections.addAll(arrayList,list);

for(String x : arrayList){
System.out.printf("%s ",x);
}
}
}



Error:

AddAll.java:21: error: method addAll in class Collections cannot be applied to given types;
Collections.addAll(arrayList,list);
          ^
  required: Collection<? super T>,T[]
  found: ArrayList<String>,List<String>
  reason: inferred type does not conform to upper bound(s)
    inferred: List<String>
    upper bound(s): String,Object
  where T is a type-variable:
    T extends Object declared in method <T>addAll(Collection<? super T>,T...)
1 error

Post a Reply

Replies

Oldest  Newest  Rating
0 Jasmin Bakalović · April 4, 2015
Instead of passing list into Collections.addAll, you need to pass an array (words in your case).

Collections.addAll(arrayList, words);


http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#addAll%28java.util.Collection,%20T...%29
0 Avanish Kumar · September 12, 2015
check on http://j2sexample.blogspot.com/
  • 1

Java / Android Development

107,051 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator