Java 我试图在使用数组后从数组中删除一个字符串。但我不知道我错在哪里

Java 我试图在使用数组后从数组中删除一个字符串。但我不知道我错在哪里,java,arrays,Java,Arrays,您没有删除任何内容:您只是在数组中设置null引用。你应该使用ArrayListine。这将完成工作,并移除其方法 如果你真的被数组所困扰,你必须构建一个全新的数组,它的大小将是你以前数组的-1,然后将以前的数组复制到新的数组中。你真的应该考虑使用第一个选项。你会发现使用第二个数据结构更容易计数。试试这个: Word: Car Printed:3 times Word: Car Printed:2 times Word: Car Printed:1 times 如果这两个字符串相同,就交

您没有删除任何内容:您只是在数组中设置null引用。你应该使用ArrayListine。这将完成工作,并移除其方法


如果你真的被数组所困扰,你必须构建一个全新的数组,它的大小将是你以前数组的-1,然后将以前的数组复制到新的数组中。你真的应该考虑使用第一个选项。

你会发现使用第二个数据结构更容易计数。试试这个:

Word: Car  Printed:3 times
Word: Car  Printed:2 times
Word: Car  Printed:1 times

如果这两个字符串相同,就交换它们?然后删除其中一个?请问你在做什么?我在读一篇课文,我得数一数每个单词的使用次数。此计数不应区分大小写,然后按字母顺序以“适当”大小写打印所有单词,即:第一个字母大写,其余单词小写,以及该单词在文本中出现的次数。每个单词只能打印一次。@F.Lachlan如果您仍然对答案和评论不满意,您可以添加自己的评论并要求更多。否则,请验证答案以关闭此主题。
Word: Car  Printed:3 times
Word: Car  Printed:2 times
Word: Car  Printed:1 times
// Count each word
Map<String, Integer> counts = new HashMap<String, Integer>();
for (String s: arrays) { // TODO rename "arrays" to "words" or something
    s = s.toLowerCase();
    int count = counts.get(s);
    if (count == null) {
        counts.put(s, 1);
    }
    else {
        counts.put(s, count + 1)
    }
}

// Sort and print
List<String> keys = counts.keySet();
Collections.sort(keys);
for (String key: keys) {
    System.out.println(key + ": " + counts[key]);
}