【问题标题】:Array list not adding values数组列表不添加值
【发布时间】:2020-10-26 22:39:39
【问题描述】:

我正在尝试制作一本包含单词和定义的字典。我制作了一个二维数组列表来存储单词和每个单词的定义。它看起来像这样:

ArrayList<ArrayList<String>> dictionary = new ArrayList<ArrayList<String>>();
    String input = "";
    int pos, i = 0;
    do{
        System.out.print("Enter 'add' to add word and definition, 'remove' to remove a word and its definition, 'change' to change the definition of a word, or 99 to quit: ");
        input = sc.nextLine().toLowerCase();
        switch(input) {
            case "add":
                while(dictionary.size() < 1) {
                System.out.println("Enter word: ");
                input = sc.nextLine();
                ArrayList<String> def = new ArrayList<String>();
                System.out.println("Enter definition to add: ");
                input = sc.nextLine();
                dictionary.get(i).add(input);
                i++;
                }
                break;
            case "remove":
                System.out.print("Enter word to remove: ");
                System.out.println(dictionary);
                input = sc.nextLine();
                ArrayList<String> remove = new ArrayList<String>(Arrays.asList(input));
                pos = dictionary.get(remove);
                if(dictionary.contains(input)) {
                    dictionary.remove(pos);
                    System.out.println("removed " + input);
                }
                else {
                    System.out.println("Word is not in dictionary");
                }
                break;
            case "change":
                System.out.print("Enter word to change: ");
                System.out.println(dictionary);
                input = sc.nextLine();
                pos = dictionary.indexOf(input);
                System.out.println("Enter replacement word: ");
                String replacement = sc.nextLine();
                //replace word (haven't implemented yet)
                break;

我在将新定义添加到数组时遇到问题。我已经尝试了上面的代码,但是每次运行它时它都会返回一个空白数组。

有没有一种方法可以返回包含添加的定义和单词的数组列表?

另外,如何将输入字符串的位置放入字符串数组列表中?

【问题讨论】:

    标签: java arrays arraylist


    【解决方案1】:

    如何将输入字符串的位置放入字符串数组列表中

    给定ArrayList&lt;AnyObject&gt;,您可以执行以下操作

    List<String> list = new ArrayList<>(List.of("A","B","C"));
    int idx = list.indexOf("B");
    System.out.println(idx + " " + list.get(idx));
    

    打印

    1 B
    
    

    但是,您是否考虑过使用 Map

    Map<String,String> dict = new HashMap<>();
    dict.put("House", "A dwelling or place to live");
    dict.put("Cat", "a pet");
    
    System.out.println(dict.get("Cat"));
    

    打印

    a pet
    

    现在改一下

    dict.put("Cat", "A type of animal");
    System.out.println(dict.get("Cat"));
    

    打印

    A type of animal
    

    【讨论】:

    • 请注意,List::of 要求 Java ≥ 9。
    • 是的——但是,泛型仍然需要 Java >= 5,但我不记得有人这么说过。我们快到 Java 16 了!
    • 我知道。不知何故,我觉得许多组织都卡在 Java 7 或 8 上,但我从未听说有人卡在 Java 4 上。
    猜你喜欢
    • 1970-01-01
    • 2020-10-14
    • 2015-01-22
    • 1970-01-01
    • 2020-10-05
    • 2015-03-21
    • 2013-03-01
    • 2014-03-25
    • 1970-01-01
    相关资源
    最近更新 更多