0

Can we pass the elements of an array as the name of the objects of some Class in Java. Suppose I created a class Cars with some of its attributes.

class Cars {
  int maxSpeed;
  int mileage;
  public double roadTax(int manufactureYear){
      int x = 150;
      while((manufactureYear<2021) && (manufactureYear > 2010)){
          for (int i = 2010; i < manufactureYear; i++)
          x += (x + (0.10 *x));
      return x;
  }
}

We can create some objects for this class with the usual code like

Cars Volvo = new Cars();

Suppose I have a String-Array of brands and I want to create an object for each element in the Array

String[] carBrands= {"Volvo","BMW","Renault","Ford", ..... }

Is there a code we can pass all the elements in the list as the objects of the class Cars, without having to write one for each of them.

FunnyO
  • 383
  • 2
  • 20
  • Please have a look here https://stackoverflow.com/questions/38458800/create-object-name-with-array – Pantelisy Jan 29 '21 at 21:42
  • Related question: [Assigning variables with dynamic names in Java](https://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java) – andrewJames Jan 29 '21 at 22:08

4 Answers4

1

Personally, i'd use a Map for this;

String[] carBrands = {"Volvo", "BMW", "Renault", "Ford"};
Map<String, cars> cars = new HashMap<>();
for (int i = 0; i < carBrands.length; i++) {
    cars.put(carBrands[i], new cars());
}
Matze._
  • 77
  • 11
0

You are already using your solution, but just in a different way. The mere use of String[] tells me that you know how to handle Object-Arrays.

The same applies to your class Cars. Also you have no constructor specified in your class Cars. This will result in empty Cars-Objects when calling Cars Volvo = new Cars();. That may be false if you generated enough setters for the class.

FunnyO
  • 383
  • 2
  • 20
0

Yes you can, this is called a generic type in Java. You would pass in a Car object to the arraylist as follows:

ArrayList<Cars> arrCar = new ArrayList<>();

This gives you the ability to put many different objects into the arraylist, and at each index you can access their elements when traversing through the array. This is a good starting tutorial:

beastlyCoder
  • 2,349
  • 4
  • 25
  • 52
0

Let's modify your class to have a getter and a constructor for the brand.

class Cars {
  int maxSpeed;
  int mileage;
  String brand;
  public Cars(String brand) {
    this.brand = brand;
  }
  public double roadTax(int manufactureYear){
      int x = 150;
      while((manufactureYear<2021) && (manufactureYear > 2010)){
          for (int i = 2010; i < manufactureYear; i++)
          x += (x + (0.10 *x));
      return x;
  }
  public String getBrand() {
      return brand;
  }
}

Then you can create a map by streaming the array of car brands.

String[] carBrands = { "Volvo", "BMW", "Renault", "Ford" };

Map<String, List<Cars>> carMap =
        Arrays.stream(carBrands).map(Cars::new).collect(
                Collectors.groupingBy(Cars::getBrand));


The above creates a new Car for each brand, and then adds it to a map where the key is the brand and the List<Cars> are all the cards of that brand that you created.

If you only have one type of each brand (i.e. no duplicate keys for the map) you can do it like this.

Map<String, Cars> carMap1 =
        Arrays.stream(carBrands).map(Cars::new).collect(
                Collectors.toMap(Cars::getBrand, car -> car));

You may want to change your Cars class to Car since it represents a single car.

WJS
  • 36,363
  • 4
  • 24
  • 39