You should create class that will contain those data related tor car. Take the following code as a example. I have used Java 8 feature that internally iterate over the list.
public class Main {
public static void main(String[] args){
List<Car> list = new ArrayList<>(Arrays.asList(new Car("Nissan", "Black", "KL1", "4"),
new Car("Ford", "Red", "LL0", "4")));
String id = "KL1";
list.removeIf(i -> i.getID().equals(id));
list.forEach(System.out::println); // show the list after operation
}
static class Car {
public final String Brand;
public final String Colour;
public final String ID;
public final String Door;
public Car(String brand, String colour, String ID, String door) {
Brand = brand;
Colour = colour;
this.ID = ID;
Door = door;
}
public String getBrand() {
return Brand;
}
public String getColour() {
return Colour;
}
public String getID() {
return ID;
}
public String getDoor() {
return Door;
}
}
}
Update
You can also do that by doing this
public static void main(String[] args){
List<Car> list = new ArrayList<>(Arrays.asList(new Car("Nissan", "Black", "KL1", "4"),
new Car("Ford", "Red", "LL0", "4")));
String id = "KL1";
// Following code will find and remove the match from the list.
// this for loop do exactly what it is doing "list.removeIf(i -> i.getID().equals(id));"
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getID().equals(id)) {
list.remove(i);
break;
}
}
}
ArrayList<String>instead of anArrayList<Car>? That data should be a class with properties{String brand, String colour, String id, int doorCount}or something, with atoString()method that generates the text you show. It should not be a single string while you work with the data... removing onidbecomes almost trivially simpler that way, too.