Here is my array
ArrayList cars = new ArrayList();
cars.add(new vehicles ("CAR","Peugot","3008",12500.00));
cars.add(new vehicles ("CAR","BMW","316",4995.00));
cars.add(new vehicles ("CAR","Ford","Fiesta",2995.00));
and i want a way to put these in order from cheapest first but i dont want to use a compare method. Maybe create a temporary variable and check this variable with the values and overwrite the values if theyre cheaper.
Ive tried this
public static void main(String[] args){
ArrayList carsArray = new ArrayList();
carsArray.add(new vehicles ("CAR","Peugot","3008",12500.00));
carsArray.add(new vehicles ("CAR","BMW","316",4995.00));
carsArray.add(new vehicles ("CAR","Ford","Fiesta",2995.00));
vehicles lowestPrice = (vehicles) carsArray.get(0);
for(vehicles car : carsArray){
if(car.Cost<lowestPrice.Cost){
lowestPrice = car;
}
}
}
Yet i still get a mistake :/The 'carsArray' in the forloop is underlined in red
Collections.sort(or some other sorting algorithm) andComparator.Carwhich remember current lowest and iterate thru your arrayList. This is strange way of doing it, but will work.