When a method is taking List of Object[] as input, even at the run-time the actual input is List of Object, it is still okay for the method. Question is, for generics in Java, what does List<something>
really mean and what is the difference between List<Object[]>
and List<Object>
?
/*
* if name is "A", then the input is a List<Object[]>
* if name is "B", then the input actually is a List<Object>
*/
public List<String> convert(List<Object[]> objectList, String name) {
List<String> resultList = new ArrayList<String>();
if (StringUtils.equals(name, "A")) {
for (Object[] object : objectList) {
String code = StringUtils.join((String) object[0], (String) object[1]);
resultList.add(code);
}
return resultList;
}
else if(StringUtils.equals(name, "B")) {
for (Object object : objectList) {
String code = (String) object;
resultList.add(code);
}
return resultList;
}
}
Actually during the run-time, this method works fine and takes both List<Object[]>
and List<Object>
.
Just wondering anyone could give some explanation of how this could work.