craig
craig

Reputation: 11

Validate the list of objects in arraylist

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

Upvotes: 1

Views: 1464

Answers (3)

Iyanu Adelekan
Iyanu Adelekan

Reputation: 406

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.

Upvotes: 0

garnulf
garnulf

Reputation: 366

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.

Upvotes: 1

Moshe Carmeli
Moshe Carmeli

Reputation: 295

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();
    }
}

Upvotes: 1

Related Questions