简体   繁体   English

如何在数组列表上使用迭代器的一种条件从数组列表中删除特定值及其下两个值?

[英]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的列表

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. 我使用了iterator.remove,但是我只能使用if语句删除第一个值的汽车名称。 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中删除特定位置:-

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语句要求的值。 If you want to remove them all your just need to improve the if statement or remove it. 如果要删除它们,则只需要改进if语句或将其删除即可。

In practice what you are doing is "abc".equals(value) and this validation is only true in the first element. 实际上,您正在执行的是"abc".equals(value)并且此验证仅在第一个元素中为true。

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 注意:由于您正在使用Kotlin,因此此处的一个好作法是将列表转换为MutableList()并使用Kotlin中removeAll() 函数

EDIT 编辑
If you are supposed to remove all elements, you need to remove break; 如果应该删除所有元素,则需要删除break; also. 也。

Lets define a simple Car . 让我们定义一个简单的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. 使用List<String>实现实现此接口,该实现遵循上述问题。

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). 请注意 ,代码缺少空检查和边缘情况(调用remove时,如果通过的汽车为null怎么办?如果列表为空并删除则怎么办?如果列表中不存在car并删除则怎么办? ?等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果第二个数组中的值与第一个数组中的值相同,如何从另一个数组中删除一个值 - How to remove one arraylist value from another arraylist if the value in second array is same in first arraylist Iterator可以从列表中删除不等于特定值的项目吗? - Can Iterator remove an item not equal to a particular value from the list? Java两个清单 <?> 使用迭代器从一个元素中删除元素? - Java Two List<?> Using Iterator remove element from one? 如何在一个迭代器循环中显示两个arraylist? - how to display two arraylist in one iterator looping? 如何对树图进行排序并显示其值和值的索引,如果前一个与下一个相同,则如何跳至下一个索引 - How to sort a treemap and display its values and indices of the values and also skip to the next index if the previous one is same with the next 如何在迭代时从arraylist中删除一个元素 - How to remove just one element from arraylist while iterating over it 如何通过检查其值从 ArrayList 中删除元素? - How to remove element from ArrayList by checking its value? 仅用一个Iterator Java遍历两个列表 - Iterate over two list with just one Iterator Java 如何选择特定的哈希图 <String,String> 从列表中按其值 - How to select a particular hashmap<String,String> from a list by its value 如何按类型从Arraylist中删除元素 - How to remove an element from Arraylist by its type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM