[英]String lines sorting by Shell Sort
I have a text file of 50 string lines of varying length and content. 我有一个文本文件,其中包含50条不同长度和内容的字符串行。 I need to read the file and sort in ascending order.
我需要阅读文件并按升序排序。 Sorting condition: the number of words in a sentence that start from the letter "a".
排序条件:句子中以字母“ a”开头的单词数。
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);
}
}
I use Map
as Kau advise me. 我使用
Map
作为Kau的建议。 But Map
use unique keys. 但是
Map
使用唯一键。 But my K can have same values and Map
can't find an appropriate string element. 但是我的K可以具有相同的值,而
Map
找不到合适的字符串元素。 What other Сollection
сan I use? 我还使用其他
Сollection
推荐?
I am assuming you already have the algorithm for shell short to sort an array of integers. 我假设您已经有shell short算法来对整数数组进行排序。 Let the method be
shellSort(int[] a)
. 令方法为
shellSort(int[] a)
。 What you can do is create a map with key as k and value as the string representing the line. 您可以做的是创建一个键为k ,值为代表线的字符串的地图。 At the same time we'll create an array of integers that holds all k .
同时,我们将创建一个包含所有k的整数数组。 Then call the method
shellSort
on the array of k values. 然后在k个值的数组上调用方法
shellSort
。 Then read back from the sorted array, look in the map using the array elements as keys. 然后从排序后的数组中读取,使用数组元素作为键在地图中查找。 Fetch the corresponding map values (which are the lines) and put them back one by one in
tempsArray
which should finally have all the lines sorted in the desired way. 提取对应的映射值(即线条),并将它们一一
tempsArray
,该tempsArray
最终应以所需的方式对所有线条进行排序。 Below is the code (untested) just to give an idea. 下面是代码(未经测试),仅用于提供想法。
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
Map<Integer, List<String>> myMap = new HashMap<Integer, List<String>>();
int[] countArr = new int[tempsArray.length];
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++;
}
}
countArr[i] = k;
List<String> listOfLines = myMap.get(k);
if(listOfLines == null){
listOfLines = new ArrayList<String>();
listOfLines.add(tempsArray[i]);
myMap.put(k, listOfLines);
} else{
listOfLines.add(tempsArray[i]);
}
System.out.println("Number of words = " + cnt);
cnt=0;
System.out.println("Number of words 'а' = " + k);
}
//Call shellsort here on the array of k values
shellSort(countArr);
List<String> sortedListOfLines = new ArrayList<String>();
for(int i=0; i<countArr.length; i++){
List<String> lineList = myMap.get(countArr[i]);
if(lineList != null){
sortedListOfLines.addAll(lineList);
lineList = null;
myMap.put(countArr[i], lineList);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.