I have some ArrayLists declared. Some of them are ArrayList<Integer> and other are ArrayList<Double>.
I want to get them from another class, so I wrote a method:
private ArrayList<Double> list1, list2;
private ArrayList<Integer> list3;
public ArrayList getList(String nameOfList){
ArrayList myList = new ArrayList();
if(nameOfList.equals("list1")) myList = list1;
else if(nameOfList.equals("list2")) myList = list2;
else if(nameOfList.equals("list3")) myList = list3;
return myList;
}
This compiles, but throws me some warnings:
*ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized* in the method above, and *Type safety: The expression of type ArrayList needs unchecked conversion to conform to List<? extends Number>* when calling the method from here:
XYSeries series = new SimpleXYSeries(data.getList("list1"), data.getList("list2"), "My chart");
where SimpleXYSeries is a chart whose constructor expects to be getting **(List<? extends Number>, List<? extends Number>, String)**.
Which is the right way to do what I’m intending to do?
Use multiple methods instead of one god method.
This is poor design to have one
getListmethod that uses aStringto select a member. Instead, there should be multiple methods – one for each list. Then each method can return the type appropriate to its list.