I am trying to write a class that contains a method that returns a list of indexes of the objects based on a search of one the objects properties.
I have an array Car which has 5 objects with three different properties Color, Make, Model.
public class Car {
String color;
String make;
String model;
public Car(String color, String make, String model){
this.color = color;
this.make = make;
this.model = model;
}
Car[] myCars =new Car[5];
myCars[0]=new Car("red", "lexus", "rc350");
myCars[1]=new Car("white", "honda", "accord");
myCars[2]=new Car("red", "honda", "accord");
myCars[3]=new Car("black", "ford", "mustang");
myCars[4]=new Car("gray", "lincoln", "navigator");
Now I am trying to loop through this array so that I can search for a given property such as color being red and I would like the indexes 0 and 2 to be added to a new list.
Something like this is what im trying and thinking to do In hopes that it would return an ArrayList or array of ‘0’ and ‘2’:
public class SearchObject{
List <Integer> getObject (String propertyName, String propertyValue)
searchobject. getObject(“color”,“red”);
I am new to Java and programming in general and would really appreciate any help. Thanks in advance.