How to create object of a particular type when the parameter passed in is generic? Here is what I have:
public class Sample{
static ArrayList<Bus> buses;
static ArrayList<Taxi> taxis;
static ArrayList<Truck> trucks;
public static void main(String[] args) {
readInFile("busStates.txt",buses);
readInFile("taxiStates.txt",taxis);
readInFile("truckStates.txt",trucks);
}
public static <T> void readInFile(String fileName, ArrayList <T> targetList){
Scanner inFile = new Scanner(new FileInputStream(fileName));
while(inFile.hasNextLine()){
T t = new T(inFile.nextLine().split("\t")); //this failed.
//I hope it can do Bus t = new Bus(params) when busList is passed in
targetList.add(t);
}
}
I thought java will be able to create Bus object when I passed in the busList<> which will contain Bus objects.
Are there any way to initialize object based on the type of parameter passed in? I want my program to invoke Bus constructor when busList is passed in and do the same for taxiList, truckList.
Side note: Bus, Taxi and Truck extends a common superclass.
Thank you.