import java.util.*;
public class Test{
public static void main(String[] args){
List<Integer> list=new ArrayList<>();
addToList(list);
}
static void addToList(List<?> list){
list.add(new String());
}
}
This program gives error at list.add line. Is there someway we can modify the addToList method's body and make this program work? I want to check the Generic type of List being passed to this method. If the list being passed is of type String, then I want to add a new string to the list. Is there any way to put such condition inside the addToList method?
P.S.- Advance apologies to moderators for putting this silly question. I know the solution would be something very simple, but haven't been able to find out despite a quick google search. Please bear with me and post any quick possible solution. Thanks in advance! :)