gpt4 book ai didi

java - java链表中单词的出现

转载 作者:行者123 更新时间:2023-11-30 03:26:26 25 4
gpt4 key购买 nike

我必须在链接列表中执行单词出现,但不将单词存储到 map 中。我只被允许使用链接列表。输出:单词、出现次数、百分比。有人可以帮忙吗?

public class Linkedlist {

private LinkedList<String> list = new LinkedList<String>();

public void readFile() {

File file = new File("words.txt");

try {

Scanner sc = new Scanner(file);

String words;

while (sc.hasNext()) {
words = sc.next();
words = words.toLowerCase();
Collections.sort(list);

if (words.length() >= 2) {
if (list.contains(words)) {


}
}

sc.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public void showList() {
System.out.println(list);
}

public static void main(String args[]) {

Linkedlist abc = new Linkedlist();

abc.readFile();
abc.showList();
}
}

最佳答案

这样的东西应该有效

    Collections.sort(words);
String last = null;
int n = 0;
for (String w : words) {
if (w.equals(last)) {
n++;
} else {
if (last != null) {
System.out.printf("%s %d %.1f%%%n", last, n, 100.0 * n / words.size());
}
last = w;
n = 1;
}
}
System.out.printf("%s %d %.1f%%%n", last, n, 100.0 * n / words.size());

关于java - java链表中单词的出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30100290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com