Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用LinkedList计算词频_Java - Fatal编程技术网

Java 使用LinkedList计算词频

Java 使用LinkedList计算词频,java,Java,我是新来的,我正在学习编码。我正在做一个练习,在这个练习中,我需要找到给定字符串中单词的频率,并将它们放入LinkedList中。我觉得我离得很近,但没有雪茄烟。任何帮助都将不胜感激 public static void main(String[] args) { String test = "Informatics is the future It is a bridge to all things useful Informatics harnesses the

我是新来的,我正在学习编码。我正在做一个练习,在这个练习中,我需要找到给定字符串中单词的频率,并将它们放入LinkedList中。我觉得我离得很近,但没有雪茄烟。任何帮助都将不胜感激

public static void main(String[] args)
{
    String test = "Informatics is the future It is a bridge to all things useful 
    Informatics harnesses the power and possibility of digital technology to 
    transform data and information into knowledge that people use every day This 
    strong focus on the human use of computing helps people to interact with 
    technology in the best and most efficient way possible Computer scientist 
    believe informatics is the human part of the IT equation making computer 
    software and hardware relatable accessible and enjoyable to use";

    String lower = test.toLowerCase();
    String[] split = lower.split(" ");
    LinkedList<String> words = new LinkedList<>(Arrays.asList(split));
    String strings = new String();
    LinkedList<Integer> count = new LinkedList<>();

        for(int j = 0; j<split.length; j++)
        {

        for (int i = 0; i < test.length(); i++)
        {
            if (words.contains(test))
            {
                int index = words.indexOf(test);
                count.set(index, count.get(index) + 1);
            }
            else
                {
                 words.add(strings);
                 count.add(1);
           }
      }
  }

    for (int i = 0; i < words.size(); i++)
    {
        System.out.println(strings + " : " + count.get(i));


    }
}
publicstaticvoidmain(字符串[]args)
{
String test=“信息学是未来,它是通向一切有用事物的桥梁
信息学利用数字技术的力量和可能性
将数据和信息转化为人们今天每天使用的知识
对人类使用计算机的强烈关注有助于人们与他人互动
以计算机科学家可能的最佳和最有效的方式使用技术
相信信息学是计算机IT方程式的人类部分
软件和硬件相关,易于使用和享受”;
String lower=test.toLowerCase();
String[]split=lower.split(“”);
LinkedList单词=新的LinkedList(Arrays.asList(split));
字符串=新字符串();
LinkedList计数=新建LinkedList();

对于(int j=0;j而言,此代码中的主要缺陷是输入字符串
test
转换为小写,然后将其拆分为单词列表,然后多次验证小写单词列表是否包含整个输入字符串
test
。当然,此条件永远不会成立

正确的逻辑应该是遍历单词列表,如果发现重复项,则增加计数器并删除重复项(从而减少
单词
大小)

可以使用以下方法将其设置为列表中的特定位置:

String test=“信息学是未来它是通向所有有用信息学工具的桥梁”
+“数字技术将数据和信息转化为知识的力量和可能性”
+“人们每天都在使用计算机这种对人类使用计算机的强烈关注有助于人们进行互动”
+“以计算机科学家可能相信的最好和最有效的方式使用技术信息学”
+“是IT等式中的人的一部分,使计算机软件和硬件可访问”
+“和愉快的使用”;
String lower=test.toLowerCase();
String[]split=lower.split(\\s+);//使用regex\s+放弃多个空格
LinkedList单词=新的LinkedList(Arrays.asList(split));
LinkedList计数=新建LinkedList();
System.out.println(“初始字数:+words.size());
对于(int currendex=0;currendex
输出(部分):

使用自定义列表

import java.util.ArrayList;
public class WordCount {
public static void main(String[] args) {
    String test = "Informatics is the future It is a bridge to all things useful Informatics harnesses "
    + "the power and possibility of digital technology to transform data and information into knowledge "
    + "that people use every day This  strong focus on the human use of computing helps people to interact "
    + "with technology in the best and most efficient way possible Computer scientist believe informatics "
    + "is the human part of the IT equation making computer  software and hardware relatable accessible "
    + "and enjoyable to use";
    String lower = test.toLowerCase();
    String[] split = lower.split("\\s+");
    CustomList<Object> cList = new CustomList<Object>();
    for (int i = 0; i < split.length; i++) {
        cList.addWord(split[i]);
    }
    cList.forEach(System.out::println);
    }
    }

class CustomList<Words> extends ArrayList<Object> {

private static final long serialVersionUID = 1L;

public void addWord(String name) {
    for (int i = 0; i < this.size(); i++) {
        @SuppressWarnings("unchecked")
        Words word = (Words) this.get(i);
        if (word.name.equals(name)) {
            word.count = ++word.count;
            return;
        }
    }
    this.add(new Words(name, 1));
}

class Words {
    int count;
    String name;

    Words(String name, int count) {
        this.name = name;
        this.count = count;
    }

    public String toString() {
        return name + ":" + count;
    }
}
}

P{lease format(indent)您的代码正确。您试图完成的任务需要一个
映射
数据结构,而不是
列表
@JimGarrison,
列表
也可以使用,但需要一个
自定义对象(字符串,Int)
import java.util.ArrayList;
public class WordCount {
public static void main(String[] args) {
    String test = "Informatics is the future It is a bridge to all things useful Informatics harnesses "
    + "the power and possibility of digital technology to transform data and information into knowledge "
    + "that people use every day This  strong focus on the human use of computing helps people to interact "
    + "with technology in the best and most efficient way possible Computer scientist believe informatics "
    + "is the human part of the IT equation making computer  software and hardware relatable accessible "
    + "and enjoyable to use";
    String lower = test.toLowerCase();
    String[] split = lower.split("\\s+");
    CustomList<Object> cList = new CustomList<Object>();
    for (int i = 0; i < split.length; i++) {
        cList.addWord(split[i]);
    }
    cList.forEach(System.out::println);
    }
    }

class CustomList<Words> extends ArrayList<Object> {

private static final long serialVersionUID = 1L;

public void addWord(String name) {
    for (int i = 0; i < this.size(); i++) {
        @SuppressWarnings("unchecked")
        Words word = (Words) this.get(i);
        if (word.name.equals(name)) {
            word.count = ++word.count;
            return;
        }
    }
    this.add(new Words(name, 1));
}

class Words {
    int count;
    String name;

    Words(String name, int count) {
        this.name = name;
        this.count = count;
    }

    public String toString() {
        return name + ":" + count;
    }
}
}
informatics:3
is:3
the:6
future:1
it:2
a:1
bridge:1
...