1

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.

1
  • if the data doesn't change often, could also pre-populate hashtables (one for each property/facet of the object) to make the search faster Commented Apr 5, 2018 at 20:19

2 Answers 2

1

search over objects by strings is a little odd in Java. you can create a sample Entity and fill the variables you want to search. something like the below code:

public class Car {
    String color;
    String make;
    String model;

    public Car(){

    }

    public Car(String color, String make, String model) {
        this.color = color;
        this.make = make;
        this.model = model;
    }

    public boolean equals(Car car){
        if(car.color!= null && !car.color.equals(this.color) )
            return false;
        else if(car.make!= null && !car.make.equals(this.make) )
            return false;
        else if(car.model!= null && !car.model.equals(this.model) )
            return false;

        return true;
    }

    public static void main(String[] args) {

        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");

        Car sampleCar = new Car();
        sampleCar.color = "red";

        List<Car> filteredCars = Arrays.stream(myCars).filter(i -> i.equals(sampleCar)).collect(Collectors.toList());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

One approach would be to add boolean methods to your Car Class. For example:

public boolean isColor(String aColor){
    boolean ret = false;
    if(this.color.compareTo(aColor) == 0){
        ret = true;
    }
    return ret;
}

nest a switch statement, inside of a for() -loop, and save whichever index of your for-loop evaluates to true. the switch statement should use the first part of your query to determine which method: isColor() isMake() isModel() to call.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.