简体   繁体   中英

How to remove particular value from array list and also its next two values base on one condition usinng iterator over arraylist?

I have an arraylist of string type in first three indexes I have detail of one car like

  • car name
  • model
  • price

and similarly on next three indexes i have details of another car. Now I want to remove details of one car. How should I do this. For example I have listnamed carinfo

carinfo.add("abc");
carinfo.add("xyz");
carinfo.add("someprice");

I used iterator.remove but I can remove only first value car name using if statement. I also want to remove next two values. please help me.

Iterator<String> iterator = a.iterator();
while(iterator.hasNext())
{
    String value = iterator.next();
    if ("abc".equals(value))
    {
        iterator.remove();
        break;
    }
}

you can remove a specific position from all the arrayList like this:-

ArrayList<String> firstList = new ArrayList<>();
        ArrayList<String> secondList = new ArrayList<>();
        ArrayList<String> ThirdList = new ArrayList<>();

        firstList.add("first");
        firstList.add("two");
        firstList.add("three");
        firstList.add("four");

        secondList.add("first");
        secondList.add("two");
        secondList.add("three");
        secondList.add("four");

        ThirdList.add("first");
        ThirdList.add("two");
        ThirdList.add("three");
        ThirdList.add("four");

        String value = "three";

        int pos = firstList.indexOf(value);

        firstList.remove(pos);
        secondList.remove(pos);
        ThirdList.remove(pos);

It only removes the first value, because it's the only one that meets your if statement requirement. If you want to remove them all your just need to improve the if statement or remove it.

In practice what you are doing is "abc".equals(value) and this validation is only true in the first element.

Note: A good practice for here, since you are using Kotlin, would be to convert your list to a MutableList() and use the removeAll() function from Kotlin

EDIT
If you are supposed to remove all elements, you need to remove break; also.

Lets define a simple Car .

class Car {
    String name = "";
    String model = "";
    String price = "";

    public Car(String name, String model, String price) {
        this.name = name;
        this.model = model;
        this.price = price;
    }
}

Declare an interface that will act as our list of cars.

interface CarList {
    void addCar(Car car);
    void removeCar(Car car);
    void removeAll();
}

Implement this interface using a List<String> implementation which follows the same rules as mentioned in question.

class MyCarList implements CarList {

    private final List<String> storage = new ArrayList<>();

    @Override
    public void addCar(Car car) {
        storage.add(car.name);
        storage.add(car.model);
        storage.add(car.price);
    }

    @Override
    public void removeCar(Car car) {
        int index = storage.indexOf(car.name);

        // Remove car name at index.
        storage.remove(index);
        // index+1 item (model) is now index, call remove on `index` again.
        storage.remove(index);
        // index+1 item (price) is now index, call remove on `index` again.
        storage.remove(index);
    }

    @Override
    public void removeAll() {
        storage.clear();
    }

    @NonNull
    @Override
    public String toString() {
        return storage.toString();
    }
}

Sample run

    Car car1 = new Car("car1-name", "car1-model", "car1-price");
    Car car2 = new Car("car2-name", "car2-model", "car2-price");
    Car car3 = new Car("car3-name", "car3-model", "car3-price");

    CarList carList = new MyCarList();
    carList.addCar(car1);
    carList.addCar(car2);
    carList.addCar(car3);
    System.out.println(carList);
    //OUTPUT: [car1-name, car1-model, car1-price, car2-name, car2-model, car2-price, car3-name, car3-model, car3-price]

    carList.removeCar(car2);
    System.out.println(carList);
    //OUTPUT: [car1-name, car1-model, car1-price, car3-name, car3-model, car3-price]

    carList.removeCar(car1);
    System.out.println(carList);
    //OUTPUT: [car3-name, car3-model, car3-price]

    carList.removeCar(car3);
    System.out.println(carList);
    //OUTPUT: []

    carList.addCar(car2);
    System.out.println(carList);
    //OUTPUT: [car2-name, car2-model, car2-price]

Note that the code is missing null checks and edge cases (when remove is called, what if the car passed is null? what if the list is empty and remove is called? what if the car does not exist in the list and remove is called? etc etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM