Reputation: 775
I have a unknown number of lists (two in my example but it can vary):
SimpleDateFormat sdf = new SimpleDateFormat("yyyy, MM, dd");
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(100, "Abe Adams", sdf
.parse("2009, 12, 1"), 10000.00));
employees.add(new Employee(101, "Betty Barnes", sdf
.parse("2010, 11, 1"), 11000.00));
employees.add(new Employee(102, "Caleb Crown", sdf
.parse("2011, 10, 1"), 12000.00));
employees.add(new Employee(103, "Dirk Daniels", sdf
.parse("2012, 09, 1"), 13000.00));
Date date = new Date();
List<ObjectTest> objects = new ArrayList<ObjectTest>();
objects.add(new ObjectTest("name",date,10.0));
objects.add(new ObjectTest("name2",date,20.0));
and I would like to use them as arg in a memthod
oReport.writeReportToExcel(employees,objects);
with
public <T> void writeReportToExcel(List <T> ... varArrayData ) {
...
}
I have to use var args parameters but the compiler rejects the code What needs to be changed ?
The message of the compiler is
method writeReportToExcel in class Bean2Excel cannot be applied to given types;
required: List<T>[]
found: List<Employee>, List<ObjectTest>
reason: inferred type does not conform to equality constraint(s)
inferred: ObjectTest
equality constraint(s): ObjectTest, Employee
where T is a type-variable:
T extends Object declared in method <T> wirteReportToExcel(List<T> ...)
EDIT FOR THE SOLUTION (it may be a dirty way)
Bean2Excel oReport = new Bean2Excel();
List recapList = new ArrayList();
recapList.add(employees);
recapList.add(objects);
oReport.writeReportToExcel(recapList);
And as suggested I removed the generics
public void writeReportToExcel(List datas) {
try {
Bean2Excel oReport = new Bean2Excel();
for (int i = 0;i<datas.size();i++){
List data = (List)datas.get(i);
...
}
}
Upvotes: 0
Views: 84
Reputation: 56
<T> should be a specified type in a method. Employee or ObjectTest here, for example. But once you defined 2 different types in a method, and try to apply to List<T>[] named varArrayData, compile will refuse to do, for it cannot make sure T means Employee.class or ObjectTest.class.
If you really want to use var args, remove <T> mark in method signature might works:
public void writeReportToExcel(List ... varArrayData ) {
...
}
Or you can put the type define to the class leave:
public class Bean2Excel<T>{
public void writeReportToExcel(List<T> ... varArrayData ) {
...
}
}
Hope it helps.
Upvotes: 1
Reputation: 12939
As said, i would create a common interface, let's say WritableToExcel
or something, then use wildcards:
public void writeReportToExcel(List<? extends WritableToExcel>... varArrayData) { }
Now you can read from the lists inside your method, and call methods from WritableToExcel
to write your table.
Upvotes: 1
Reputation: 47280
the arguments are of different types, T. You could remove the generic definition o rmake them implement a common interface.
Do you have to use var args ?
Upvotes: 2
Reputation: 957
Create a common base class for the types that can go in the lists.
If Employee
and ObjectTest
inherit from the same base class, it should work.
Upvotes: 0