如何在Java中打印ArrayList并删除其中重复元素

huangapple 未分类评论43阅读模式
英文:

How to print an arraylist and remove its duplicates in java

问题

我目前正在解决来自 https://java-programming.mooc.fi/part-5/4-objects-and-references(练习:archive) 的一个练习,要求我只打印列表中不重复的标识符。如果标识符已经在列表中找到,只会打印第一个。尽管在我的输入中有重复的标识符,但它仍然会打印ArrayList的每个项目。我该如何解决这个问题?

期望输出:

import java.util.ArrayList;
import java.util.Scanner;
    
public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

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

        String identifier = "";

        String name = "";

        while (true) {
            System.out.println("Identifier? (empty will stop)");

            identifier = scanner.nextLine();

            if (identifier.isEmpty()) {
                break;
            }

            System.out.println("Name? (empty will stop)");

            name = scanner.nextLine();

            if (name.isEmpty()) {
                break;
            }

            if (!items.contains(identifier)) {
                items.add(identifier + ": " + name);
            }
        }

        System.out.println("");
        System.out.println("==Items==");
        for (String i : items) {
            System.out.println(i);
        }
    }

}
英文:

I'm currently solving an exercise from https://java-programming.mooc.fi/part-5/4-objects-and-references (Exercise: archive) and it tells me to print only the non-duplicate identifiers of the list. If the identifiers are already found in the list, only the first one will be printed. Even though there are duplicate identifiers in my input it will still print each item of the ArrayList. How can I fix this?

Desired Output:

如何在Java中打印ArrayList并删除其中重复元素

import java.util.ArrayList;
import java.util.Scanner;
    
public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        ArrayList&lt;String&gt; items = new ArrayList&lt;&gt;();

        String identifier = &quot;&quot;;

        String name = &quot;&quot;;

        while (true) {
            System.out.println(&quot;Identifier? (empty will stop)&quot;);

            identifier = scanner.nextLine();

            if (identifier.isEmpty()) {
                break;
            }

            System.out.println(&quot;Name? (empty will stop)&quot;);

            name = scanner.nextLine();

            if (name.isEmpty()) {
                break;
            }

            if (!items.contains(identifier)) {
                items.add(identifier + &quot;: &quot; + name);
            }
        }

        System.out.println(&quot;&quot;);
        System.out.println(&quot;==Items==&quot;);
        for (String i : items) {
            System.out.println(i);
        }
    }

}

huangapple
  • 本文由 发表于 2020年4月4日 08:47:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61022462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定