0

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

7
  • 1
    Sure. What happened when you tried the approach you mentioned? Commented Apr 17, 2014 at 16:29
  • i dont want to use a comparator to be honest, is there another way ? Commented Apr 17, 2014 at 16:31
  • You're going to end up using the same logic as Collections.sort (or some other sorting algorithm) and Comparator. Commented Apr 17, 2014 at 16:31
  • Just use exactly what you say : use temp Car which remember current lowest and iterate thru your arrayList. This is strange way of doing it, but will work. Commented Apr 17, 2014 at 16:31
  • @AJ_91 or better yet keep lowest price index. Commented Apr 17, 2014 at 16:32

4 Answers 4

1

You could use a TreeMap with the price as the key and the vehicles object as the value. It would then be sorted by price, and you would have all of the information for the vehicle as the value.

Sign up to request clarification or add additional context in comments.

Comments

0
Car lowestPrice = cars.get(0);
for(Car car : cars) {
   if(car.price<lowestPrice.price) {
       lowestPrice = car;
   }
}

Concerning the red underlined ArrayList ... replace it with

ArrayList<Vehicles> yourlist = new ArrayList<Vehicles>();

5 Comments

how would i implement this into my code? Im a noob so i dont understand this fully. Im getting a mistake. The 'Car'is underlined in red
@AJ_91 Car would be vehicles in your case. Btw, class names should start with a capital letter (e.g. Vehicles) and since the class seems to represent a single vehicle, it might better be named Vehicle.
ive added some code to my question if you have a chance to view it :)
I updated my answer... btw it won't take us ages to find solutions if you were telling us WHAT error there is instead of just telling us there is an error. A stacktrace is worth a thousand words.
it works :)) much appreciated and sorry for the late reply. bank holiday weekend and all that ;)
0

If you want to use approach you mentioned in OP you can declare static variable for vehicles class and in contructor you can check whether price is lower than actual minimum But my advice is to use Comparator or Comparable because this is their natural use. Also values of objects can be changed throughout various set methods and you will have to be aware of that

If you still want to do it .You do it like this

public static int min_price=Integer.MAX_VALUE;

then in contructor method if(price<min_price)min_price=price;

Comments

0

As you want to sort the items of the list you can use the Collections.sort(List<T> list, Comparator<? super T> c) method and provide own Comparator

class VechiclePriceComparator implements Comparator<vechiles> {

  public int compare(vechiles v1, vechiles v2) {

    if(v1 == v2) {
       return 0;
    )

    return Double.compare(v1.getPrice(),v2.getPrice());

  }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.