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
equalsinCar, then test thename(and only thename). Then give it a constructor that takes aname. Finally, you can sayif (al.contains(new Car("Hyundai"))car.setName("Hyundai")notcars.set....