I am trying to add an array of strings to an arrayList . Below is the code:
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;
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);
}
}
}
However, during compilation it throws below error. I am not able to figure out how to resolve it? Can someone help me with this code?
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
Collections.addAll(arrayList,list);?