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