The String
class inherits from the Object
class, and you can write the following:
List<Object> objList = new ArrayList<>(); objList.add(new String("string"));
But this rule does not work when using generic programming. This means that List<String>
not inherited from List<Object>
, and if you use the overloaded add()
method to add the list to another list, the compiler will generate an error (actually, what happened to you). If this were not the case, then we could easily break the uniformity of the list:
List<List<Object>> objList = new ArrayList<List<String>>(); List<Object> intList = new ArrayList<>(); intList.add(new Integer(5)); objList.add(intList);
As you can see, in this example we added a list of integer variables to the list of lists, which expects to store only string data.
If you need a list with the type List<List<String>>
, you can get the result of the method DBManager.getAllRecords(tableName, s)
, explicitly DBManager.getAllRecords(tableName, s)
each of its elements to the type String
and add to the list listOfRowsData
.
Additional links:
- Generics, Inheritance, and Subtypes .
- Is the List a subclass of List? Why aren't Java's generics implicitly
DBManager.getAllRecords
probably returns aList<Object>
- zRrr