java - Sort arraylist that contains subclass classes -
i have arraylist contains fruits.
i have subclass of fruits called "apples".
apples have "color" attribute other fruits subclasses don't have.
how can sort list? (i have put "apple" objects in list.)
fruits parent class, apple , others subclasses.
using instanceof
1 way of doing that.
fruits[] myfruits = ...... // array of fruits (int = 0; < myfruits.length; i++) { if (myfruits[i] instanceof apple) { // ... } else { // ... } }
Comments
Post a Comment