i am facing problem with the retyping the object to integer.
I have the object Car with 2 string attributes. First attribute is ID.
I retyped ID from string to integer
ArrayList<Car> intList = new ArrayList<Car>(); // already filled with objects
ArrayList<Integer> intList = new ArrayList<Integer>();
for(Car tempInt : items){
intList.add(Integer.parseInt(tempInt.getID()));
}
Collections.sort(intList); // i sorted id's
Can i retype the integer back to Car object?
for (ItemObject temp : intList) { // HERE I GET THE ERROR
if(temp instanceof Car){
System.out.println("ID : "+((Car)temp).getID())
}}
Is this possible? I need sort objects.
Thanks.
S
I think you are fundamentally misunderstanding which objects are which in this scenario. When you create your
ArrayList<Integer>you do not magically preserve any information about which cars those integers were connected to.I think what you want to do is sort your list of
Carsby id, correct? In that case, what you want is aComparator: http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.htmlHere is an example of sorting using a
Comparator: