Passing both generic list and different types of elements(char, bool, int ) to a single method in java

11 Views Asked by At
public class IteratorExample {
    public static <T> void main(String[] args) {

        List<T> tList = new LinkedList<>();
        addDataToList("First Element", tList);
        addDataToList("String", tList);
        addDataToList(100986, tList);
        addDataToList(1.98F, tList);
        addDataToList(1009L, tList);
        addDataToList('x', tList);
    }

    static <T> void addDataToList(T t, List<T> tList) {
        tList.add(t);
    }
}

I want to create the method that accepts both the Generic element type (T t), & its list(List tList), and adds the element to the list;

I get the error : The method addDataToList(T, List) in the type IteratorExample is not applicable for the arguments (String, List)"

0

There are 0 best solutions below