Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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 对ArrayList进行排序<;字符串>;在树形图中_Java - Fatal编程技术网

Java 对ArrayList进行排序<;字符串>;在树形图中

Java 对ArrayList进行排序<;字符串>;在树形图中,java,Java,我正在排成一排。我正在跟踪文件中的单词对。使用树形图,所有键都被排序。但是,当我向这些键添加单词时,它们不会被排序。 以下是我在流程功能中需要帮助的部分: private static void process(){ if(!result.containsKey(thisWord)){ result.put(thisWord, new ArrayList<String>()); } // Add nextWord t

我正在排成一排。我正在跟踪文件中的单词对。使用树形图,所有键都被排序。但是,当我向这些键添加单词时,它们不会被排序。 以下是我在流程功能中需要帮助的部分:

 private static void process(){


 if(!result.containsKey(thisWord)){
            result.put(thisWord, new ArrayList<String>());

        }

        // Add nextWord to the list of adjacent words to thisWord:
        result.get(thisWord).add(nextWord); // nextword is not sorted within the key
私有静态无效进程(){
如果(!result.containsKey(thisWord)){
result.put(thisWord,newarraylist());
}
//将nextWord添加到此单词的相邻单词列表:
result.get(thisWord).add(nextWord);//nextWord未在键内排序
这个词已分类

nextWord不是

我可以使用Collections.sort(result)吗? 我只是不知道如何才能在结果中进入下一步。 或者,在我的情况下没有办法这样做。除非你推荐,否则我宁愿不改变

这是节目

import java.util.Map.Entry;
import java.util.TreeSet;
import java.io.*;
import java.util.*;





public class program1 {

private static List<String> inputWords = new ArrayList<String>();
private static Map<String, List<String>> result = new TreeMap<String, List<String>>();



public static void main(String[] args) {


    collectInput();
    process();
    generateOutput();
}


private static void collectInput(){
   Scanner      sc = new Scanner(System.in);    
   String       word;


    while (sc.hasNext()) {                      // is there another word?
        word = sc.next();                       // get next word
        if (word.equals("---")) 
         {
            break;
           }

        inputWords.add(word);

        }

}

private static void process(){


    // Iterate through every word in our input list
    for(int i = 0; i < inputWords.size() - 1; i++){

        // Create references to this word and next word:
        String thisWord = inputWords.get(i);
        String nextWord = inputWords.get(i+1);


        // If this word is not in the result Map yet,
        // then add it and create a new empy list for it.
        if(!result.containsKey(thisWord)){
            result.put(thisWord, new ArrayList<String>());

        }

        // Add nextWord to the list of adjacent words to thisWord:
        result.get(thisWord).add(nextWord);  // need to sort nextword
      //  Collections.sort(result);

    }

 }


 private static void generateOutput()
    {

    for(Entry e : result.entrySet()){
        System.out.println(e.getKey() + ":");

        // Count the number of unique instances in the list:
        Map<String, Integer> count = new HashMap<String, Integer>();
        List<String> words = (List)e.getValue();
        for(String s : words){
            if(!count.containsKey(s)){
                count.put(s, 1);
            }
            else{
                count.put(s, count.get(s) + 1);
            }
        }

        // Print the occurances of following symbols:
        for(Entry f : count.entrySet()){
            System.out.println("      " + f.getKey() + ",  " + f.getValue() );

        }
    }
    System.out.println();
}
}
import java.util.Map.Entry;
导入java.util.TreeSet;
导入java.io.*;
导入java.util.*;
公共课程1{
私有静态列表inputWords=new ArrayList();
私有静态映射结果=新树映射();
公共静态void main(字符串[]args){
collectInput();
过程();
生成输出();
}
私有静态输入(){
扫描仪sc=新的扫描仪(System.in);
字符串字;
while(sc.hasNext()){//还有别的词吗?
word=sc.next();//获取下一个单词
if(字等于(“--”)
{
打破
}
输入单词。添加(单词);
}
}
私有静态void进程(){
//迭代输入列表中的每个单词
对于(int i=0;i
如果您想要收集“nextword”如果已排序,为什么不使用树集而不是ArrayList?我唯一能看到的反对理由是你是否有重复项。如果允许重复项,那么是的,当你添加到ArrayList时,在ArrayList上使用Collections.sort。或者查看Apache Commons或Google collection类-我对它们一无所知,但我很抱歉当然有一个排序列表,允许其中一个或两个重复。

Y您不试试这样的东西吗


Collections.sort(inputWords);

我不确定如何使用树集代替arraylist?我尝试了:private static Map result=new TreeMap();和result.put(thisWord,new TreeSet());当我尝试运行程序时,我得到一个错误:树集无法转换为list。是否尝试对结果进行类型转换。get()这对我有用:result.get(thisword.add(nextword);@Corey,那是因为您需要将列表更改为在generateOutput中设置。但是,现在我看generateOutput,我意识到您不能使用树集-您需要遵循我给您的其他建议。我被告知要使用映射的映射,但我不确定如何从文件中添加到它。您能帮忙吗?map m=new HashMap();因为它会将关键字排序到下一个单词,而且配对的单词不正确。我不知道如何将文件添加到地图中。。
result.get(thisWord).add(nextWord);
Collections.sort(result.get(thisWord));