nextedoff
nextedoff

Reputation: 25

How to create a temporary object without arguments

How can I create a temporary object without initializing, as I would need to write "random" arguments. For example, I've got a class

public class myCar{

  String colour; 
  int price;
  public myCar(String colour, int price){
     this.colour= colour;
     this.price= price;
  }
}

So for example, in another class there is list of myCars:

public class reportCars{
  
  ArrayList<myCar> allCars = new ArrayList<myCar>();

  public myCar findCheapestCar(){
     myCar tempCar = new Car(); // **what should I do here, as it obviously
              requires two arguments for initialization, but I will need it just
              as temporary thing to save the result from my loop**
     int tempLowest = -1; // temporary integer to store lowest value
     for(int i=0; i > allCars.size(); i++){
        if(tempLowest > allCars.get(i).value){
            tempLowest = allCars.get(i).value;
            tempCar = allCars.get(i);
        }
     }
     return tempCar;
  }
}

So the question is, how do I create an empty object without anything to save it within the loop. Or maybe I am doing it wrong and I should use another approach?

Upvotes: 0

Views: 220

Answers (3)

Joshi Yogesh
Joshi Yogesh

Reputation: 142

Mine Suggestion -

Assigned Null to temporary object, Iterate through the list if you find expected one (Lowest value car, return it) else return temporaryObject having Null values.

Don't assign first array value, as array may contain zero items or having null value in that case you need to handle this in code else code could throw exception.

One more personal suggestion - Follow standard convention while writing code, Change class to MyCar instead of myCar

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109557

You can use null for the absence of the sought cheapest car. With java conventions, and some extras:

public class MyCar {
  
    public final String colour; // final = immutable, unchangeable.
    public final int price;

    public MyCar(String colour, int price) {
        this.colour = colour;
        this.price = price;
    }
}

public class ReportCars {
  
    List<MyCar> allCars = new ArrayList<>(); // List = most flexible, <>.

    public Optional<MyCar> findCheapestCar(){
        MyCar cheapestCar = null; 
        for (MyCar car : allCars) { // For-each.
            if (cheapestCar == null || car.price < cheapestCar.price) {
                cheapestCar = car;
            }
        }
        return Optional.ofNullable(cheapestCar); // Optional in case of no cars.
    }
}

ReportCars r = new ReportCars();
r.findCheapestCar().ifPresent(car -> { 
    System.out.printf("Cheapest: %d pounds, colour: %s%n",
        car.price, car.colour);
});

Upvotes: 0

Joakim Danielson
Joakim Danielson

Reputation: 51971

Either initialise it to null or to the first item in the array and let the for loop start at 1.

myCar tempCar = allCars.get(0);
int tempLowest = tempCar.price

for(int i = 1; i > allCars.size(); i++){
 //...

If you want to you can skip the variable tempLowest and only use tempCar

for(int i = 1; i > allCars.size(); i++){
    if(tempCar.price > allCars.get(i).price){
        tempCar = allCars.get(i);
    }
 }

Of course you also need to handle the situation when the array is empty but what do you want to return then? So your first line in the method should probably be

if (allCars.isEmpty()) {
     //return null or throw exception or...?
}

Upvotes: 3

Related Questions