1

I am new to Java. I am trying to validate the list of objects in Array list.

For example,

Class Cars()
{
  private String name;
  private int year;
}
Cars car = new Cars();
ArrayList<Cars> al = new ArrayList<Cars>();
car.setName("Hyundai");
car.setYear("2010");
car.setName("Maruti");
car.setYear("2010");
al.add(car)

I want to add another car object with "Hyundai" but if my list already contains it, i wanted to change my name to Hyundai1 and then add to the list.

I tried to use,

for(int i=0;i<al.size();i++)
{
  boolean value = al.get(i).getName().contains("Hyundai");
}

if(value)
{
al.setName("Hyundai1");
}

else
{
al.setName("Hyundai");
}

Note : I hardcoded the value "Hyundai" here for making it simpler. Kindly suggest

4
  • You could override equals in Car, then test the name (and only the name). Then give it a constructor that takes a name. Finally, you can say if (al.contains(new Car("Hyundai")) Commented Jul 20, 2016 at 22:21
  • What should i compare in the equals method. Is it the OBject Car with the name ? Commented Jul 20, 2016 at 22:28
  • BTW shouldnt it be car.setName("Hyundai") not cars.set.... Commented Jul 20, 2016 at 22:29
  • @ElliottFrisch could you please suggest Commented Jul 20, 2016 at 22:34

3 Answers 3

1

As Elliot Suggested:

public class Cars {
    private String name;
    private int year;

    public String getName() {
        return this.name;
    }

    @Override
    public boolean equals(Object other){
        if (other == null) return false;
        if (other == this) return true;
        if (!(other instanceof Cars)) return false;
        // Check whether they are equivalent Strings or both null
        Cars otherCar = (Cars) other;
        if (this.name == null || otherCar.getName() == null) {
            return this.name == otherCar.getName();
        } else {
            return this.name.equals(otherCar.getName()
        }
    }

    // If two objects are equal, then they must have the same hash code.
    @Override
    public int hashCode() {
        return this.name.hashCode();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

There are a couple of issues with your code:

  1. class has to be written lowercase in java.
  2. After a class declaration there is no (), this is mostly for methods.
  3. Your class appears to be the abstraction of a car, so I would name it Car instead of Cars.
  4. If you create an object and invoke a setter twice, the first value will be overwritten. You probably wanted to create two objects and set each values accordingly
  5. You test the name of the car inside a loop but invoke your actions outside the loop. Your local variable value wont be visible there and if it would, it would have the value of the last car in the list. So you probably want to act inside the loop.

Comments

0

Looking at the code you wrote, I believe the following might be what you were trying to do:

class Car{

    public Car(){
        this.createdCarNames = new ArrayList();
    }

    private String name;
    private String year;
    private ArrayList<String> createdCarNames;

    /*The following method sets the name of the new Car object.
     *It works by iterating over the list of created car names.
     *If a car name in the list is found to be equal to that which
     *you are attempting to set, concatenate a '1' to its end
     *and set the name of the car object.
     *Else, simply set the name of the Car object.
     *Lastly, the name is added to the list of created car names.
     */

    public void setName(String name){
        for(String carName : createdCarNames){
            if(carName.equals(name))
                this.name = name.concat("1");
            else this.name = name;
        }
        createdCarNames.add(name);
    }
}

If you are unsure about any part of the code, feel free to point it out. Hope this helps.

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.