1
我有一個包含50個不同長度和內容的字符串行的文本文件。我需要閱讀文件並按升序排序。排序條件:句子中從字母「a」開始的單詞數量。通過Shell Sort排序的字符串行
public static void main(String[] args) throws FileNotFoundException {
String token1 = "";
Scanner inFile1 = new Scanner(new File("E:\\text.txt"));
List<String> temps = new LinkedList<String>();
inFile1.useDelimiter(". ");
while (inFile1.hasNext()) {
token1 = inFile1.nextLine();
temps.add(token1);
}
inFile1.close();
String[] tempsArray = temps.toArray(new String[0]);
for (int i = 0; i < tempsArray.length; i++) {
System.out.println(tempsArray[i]);
}
int cnt = 0; //number of words in the string line
for (int i=0; i<tempsArray.length; i++) {
int k=0; //number of words that start from the letter "а"
System.out.println("Line № = " + i);
StringTokenizer st = new StringTokenizer(tempsArray[i]);
while (st.hasMoreTokens()) {
cnt++;
String s= st.nextToken();
if (s.charAt(0)=='a') {
k++;
}
}
System.out.println("Number of words = " + cnt);
cnt=0;
System.out.println("Number of words 'а' = " + k);
}
}
我使用Map
作爲考告我。但Map
使用唯一鍵。但是我的K可以具有相同的值並且Map
找不到合適的字符串元素。我可以使用其他什麼Сollection
?
是的,這就是我的意思。謝謝! – iFlash
從地圖獲取時,使用'myMap.get(countArr [i])'而不是'myMap.get(i)'。我發佈的原始答案中有一個錯誤。我已糾正它。 – kau
噢,如果有兩個相同的K,Map找不到合適的行,並且公佈其中的一行。 – iFlash