简体   繁体   English

如何在Java中删除和替换数组列表中的项目?

[英]How to remove and replace an item in an array list in Java?

I have the following code in java, 我在Java中有以下代码,

import java.util.ArrayList;
import java.util.Objects;

public class Cars {

    private static ArrayList<String> replaceDuplicates(ArrayList<String> aList) {

        for (int i = 0; i < aList.size(); i++) {
            for (int j = i + 1; j < aList.size(); j++) {
                if (Objects.equals(aList.get(i), aList.get(j))) {
                    aList.remove(i);
                    aList.add(i, "");
                    aList.remove(j);
                    aList.add(j, "");
                }
            }
        }

        return aList;
    }

    public static void main(String[] args) {

        ArrayList<String> cars = new ArrayList<>();

        cars.add("Ford");
        cars.add("Ford");
        cars.add("Hyundai");
        cars.add("Toyota");
        cars.add("Toyota");
        cars.add("Toyota");
        cars.add("Ford");
        cars.add("Honda");
        cars.add("GMC");

        System.out.println(cars);

        cars = replaceDuplicates(cars);

        System.out.println(cars);

    }

}

The output of this code is - [, , Hyundai, , , Toyota, Ford, Honda, GMC] 此代码的输出为- [, , Hyundai, , , Toyota, Ford, Honda, GMC]

I want to replace the name of cars that appear more than once in the array list with a " " . 我希望替换多次出现在数组列表与汽车的名字" " For some reason, in my code if a car's name has appeared thrice in the array list, then the third occurrence isn't getting replaced by " " . 出于某种原因,在我的代码中,如果汽车的名称在数组列表中出现了三次,那么第三次出现就不会被" "代替。

My desired output should be like this - [, , Hyundai, , , , , Honda, GMC] 我想要的输出应该是这样- [, , Hyundai, , , , , Honda, GMC]

What am I doing wrong here? 我在这里做错了什么?

Thank you in advance! 先感谢您!

First off: you can simplify this code by using List.set instead of inserting and removing elements. 首先,您可以通过使用List.set而不是插入和删除元素来简化此代码。

aList.remove(i);
aList.add(i, "");

would simply become 只会变成

aList.set(i, "");

You're deleting both entries, if they're duplicate. 如果两个条目都是重复的,则您要删除它们。 This leads to the behavior of always deleting an even number of entries. 这导致始终删除偶数个条目的行为。 For example: 例如:

a  b  a  a  c  a  d  a
b  a  c  a  d  a         #first pair removed
b  c  d  a               #second pair removed

If the number of elements is odd, there will always remain one element in the list. 如果元素数为奇数,则列表中将始终保留一个元素。

Obviously you need some way to remember what elements to delete. 显然,您需要某种方式来记住要删除的元素。 A simple approach to this would be to use a flag to remember whether a duplicate of an element has been encountered: 一种简单的方法是使用标志来记住是否遇到了元素的重复:

for (int i = 0; i < aList.size(); i++) {
    //the flag
    boolean duplicate = false;        

    for (int j = i + 1; j < aList.size(); j++) {
        if (Objects.equals(aList.get(i), aList.get(j))) {
            aList.set(j, "");   //remove all duplicates with an index higher than i
            duplicate = true;   //remember that element at index i is a duplicate
        }
    }

    //remove last duplicate element
    if(duplicate)
        aList.set(i, "");
}

If you still want to use your approach, you can create references to your items before modifying list: 如果仍然要使用方法,则可以在修改列表之前创建对项目的引用:

for (int i = 0; i < aList.size(); i++) {
    String a = aList.get(i);
    for (int j = i + 1; j < aList.size(); j++) {
        String b = aList.get(j);
        if (Objects.equals(a, b)) {
            aList.remove(i);
            aList.add(i, "");
            aList.remove(j);
            aList.add(j, "");
        }
    }
}

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

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