Java 操纵字符串

Java 操纵字符串,java,string,Java,String,我试图用以下代码查找字符串中重复次数最多的单词: public class Word { private String toWord; private int Count; public Word(int count, String word){ toWord = word; Count = count; } public static void main(String args[]){ String

我试图用以下代码查找字符串中重复次数最多的单词:

public class Word 
{
    private String toWord;
    private int Count;

    public Word(int count, String word){
        toWord = word;
        Count = count;
    }

    public static void main(String args[]){
        String str="my name is neo and my other name is also neo because I am neo";
        String []str1=str.split(" ");
        Word w1=new Word(0,str1[0]);
        LinkedList<Word> list = new LinkedList<Word>();
        list.add(w1);
        ListIterator itr = list.listIterator();
        for(int i=1;i<str1.length;i++){
            while(itr.hasNext()){
                if(str1[i].equalsTO(????));
                else
                    list.add(new Word(0,str1[i]));
            }
        }
    }
}
如何将字符串数组str1中的字符串与链表中存储的字符串进行比较,然后如何增加相应的计数

然后我将打印计数最高的字符串,
我也不知道怎么做

您需要将每个单词存储到一个列表中,可能是一个带有计数变量的长列表,该变量指示该单词被使用了多少次


对于每个单词,如果它已经在列表中,则增加计数,如果它不在列表中,则将其添加到列表中。

您需要将每个单词存储到一个列表中,可能是一个带有计数变量的long,该变量指示该单词被使用了多少次


对于每个单词,如果已经在列表中,则增加计数;如果不在列表中,则将其添加到列表中。

C?您可以尝试使用LINQ GroupBy,然后使用Count或Max-非常简单。

C?您可以尝试使用LINQ GroupBy,然后使用Count或Max-非常简单。

使用:

您可以使用words.counttopWord来获取顶部单词的频率。

使用:


你可以通过words.counttopWord来获取最热门单词的频率。

我建议使用HashMap而不是链表

遍历字符串。 每一个字, 检查单词是否在地图上, 如果存在,则递增计数和
否则,使用计数1插入,我建议使用HashMap而不是链表

遍历字符串。 每一个字, 检查单词是否在地图上, 如果存在,则递增计数和
否则用计数1插入,我想你可以在这里使用一些正则表达式,比如

    final String str = "my name is neo and my other name is also neo because I am neo";

    final String[] arr = str.split (" ");
    final Set <String> set = new HashSet <String> ();
    for (final String word : arr) {
        System.out.println ("arr " + word);
        set.add (word);
    }

    String preWord = "";
    int preCount = 0;
    for (final String word : set) {
        System.out.println ("----------------");

        final Pattern p2 = Pattern.compile ("\\b" + word + "\\b");
        final Matcher m2 = p2.matcher (str);
        int count = 0;

        while (m2.find ()) {
            count++;
        }

        System.out.println ("preCount " + preWord + ":" + word + ":" + preCount + ":" + count);

        if ((preCount < count)) {
            preWord = word;
            preCount = count;
            System.out.println ("assigning word " + word + ":" + count);
        }
    }

    System.out.println ("result " + preWord + ":" + preCount);

我想你可以在这里用一些正则表达式,比如

    final String str = "my name is neo and my other name is also neo because I am neo";

    final String[] arr = str.split (" ");
    final Set <String> set = new HashSet <String> ();
    for (final String word : arr) {
        System.out.println ("arr " + word);
        set.add (word);
    }

    String preWord = "";
    int preCount = 0;
    for (final String word : set) {
        System.out.println ("----------------");

        final Pattern p2 = Pattern.compile ("\\b" + word + "\\b");
        final Matcher m2 = p2.matcher (str);
        int count = 0;

        while (m2.find ()) {
            count++;
        }

        System.out.println ("preCount " + preWord + ":" + word + ":" + preCount + ":" + count);

        if ((preCount < count)) {
            preWord = word;
            preCount = count;
            System.out.println ("assigning word " + word + ":" + count);
        }
    }

    System.out.println ("result " + preWord + ":" + preCount);

使用Apache Commons StringUtils获取计数

String str="my name is neo and my other name is also neo because I am neo";
// Make a unique list (java.util.Set) of words.
Set<String> stSet = new HashSet<String>(Arrays.asList(str.split(" ")));
int sz = stSet.size();
int[] counts = new int[sz];
Map<Integer,String> matches = new HashMap<Integer,String>(sz);
int i = 0;
for (String s : stSet) {
   // saves the individual word count in a sortable array.
   counts[i] = StringUtils.countMatches(str,s));
   // saves the word count and the word in a HashMap for easy retrieval.
   matches.put(counts[i],s);
   i++;
}
Arrays.sort(counts);
int max = counts.length - 1;
System.out.println("The the word with the most occurrances is: "+matches.get(counts[max])+", the number of occurrances is: "+counts[max]);

使用Apache Commons StringUtils获取计数

String str="my name is neo and my other name is also neo because I am neo";
// Make a unique list (java.util.Set) of words.
Set<String> stSet = new HashSet<String>(Arrays.asList(str.split(" ")));
int sz = stSet.size();
int[] counts = new int[sz];
Map<Integer,String> matches = new HashMap<Integer,String>(sz);
int i = 0;
for (String s : stSet) {
   // saves the individual word count in a sortable array.
   counts[i] = StringUtils.countMatches(str,s));
   // saves the word count and the word in a HashMap for easy retrieval.
   matches.put(counts[i],s);
   i++;
}
Arrays.sort(counts);
int max = counts.length - 1;
System.out.println("The the word with the most occurrances is: "+matches.get(counts[max])+", the number of occurrances is: "+counts[max]);


你用什么语言?格式化你的代码。这是作业吗?这是作业吗?你试过什么?为什么不起作用?更具体地说,我正在尝试将输入字符串与链接列表中的字符串进行比较,如果匹配,我将增加字符串的计数,我的问题是如何与链接列表中的字符串进行比较您使用的是什么语言?格式化你的代码。这是作业吗?这是作业吗?你试过什么?为什么不起作用?更具体地说,我正在尝试将输入字符串与链表中的字符串进行比较,如果匹配,我会增加字符串的计数,我的问题是如何与链表中的字符串进行比较我使用的是java,这不是家庭作业。我只需要习惯链表中的字符串操作,你没有指定任何标记,Java和C的基本结构都是一样的。我在使用Java,这不是家庭作业。我只需要习惯使用链接列表的字符串操作,您没有指定任何标记,Java和C的基本结构是相同的。我知道算法,但如何比较字符串是否在链表中,请查看我的代码您将字符串中的每个单词添加到数组的方式对您不利,因为它可能包含重复项。正如我提到的,将每个单词逐个添加到列表中。如果单词已经在列表中,则增加计数,而不是再次添加。我知道算法,但如何比较字符串是否在链接列表中,请查看我的代码将字符串中的每个单词添加到数组的方式对您不利,因为它可能包含重复项。正如我提到的,将每个单词逐个添加到列表中。如果单词已经在列表中,则增加计数,而不是再次添加。如何包括Multiset和Hashmultiset类?您需要下载并安装Google Guava库。提取它并将番石榴罐放在类路径中。然后您可以导入它们。如何包含Multiset和Hashmultiset类?您需要下载并安装Google Guava库。提取它并将番石榴罐放在类路径中。然后你可以导入它们。我知道我的答案中没有使用链表。若你们真的需要使用链表,我建议你们改变你们的要求,不要这样做。我知道我的答案中并没有使用链表。若你们真的需要使用链表,我建议你们改变你们的要求,不要这样做。