[英]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 我在前三个索引中有一个字符串类型的数组列表,我有一辆车的详细信息,例如
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.