The code you provided works. I guess you want your al
list to be List<String>
. Then, you would have to make getMeListOfObjs
like this:
public static void getMeListOfObjs(List<? extends Object> al) {
System.out.println(al.get(0));
}
public static void main(String[] args) {
List<String> al = new ArrayList<String>();
String mys1 = "jon";
al.add(mys1);
getMeListOfObjs(al);
}
Notice the differnce?
public static void getMeListOfObjs(List<? extends Object> al) {
The wildcard ?
changes any type that extends Object
, i.e. any object.