파일로 파이핑 중입니다. 파일에서 단어 쌍을 추적 중입니다. 트리 맵을 사용하면 키가 모두 정렬됩니다. 그러나, 내가 그 열쇠에 단어를 추가 할 때 그들은 정렬되지 않습니다.ArrayList 배열 <String>을 트리 맵에 배열
난은, Collections.sort (결과)를 사용할 수 있습니다 ..
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
thisword이
nextWord가 아닌 정렬되며, 여기 는 내가 처리 기능에에 도움이 필요한 부분입니다 어쩐지? 메신저 그냥 결과 내에서 nextWord가 어떻게되는지 확신 할 수 없습니다. 또는 내 상황에서이를 수행 할 방법이 없습니다. 나는 당신이 그것을 추천하지 않으면 오히려 사물을 바꾸지 않을 것입니다. 당신이 분류 "nextword"의 수집을 원하는 경우에
이 프로그램
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();
}
}
지도를지도에 표시하면지도가 잘 돌아갈 수 있습니다.지도의지도에 파일을 추가하는 방법을 잘 모릅니다. – Steller