2014-03-06 49 views
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

回答

1

我假设你已经有壳短的算法对整数数组进行排序。让方法是shellSort(int[] a)。 你可以做的是创建一个带有密钥的地图k和值作为代表该行的字符串。同时,我们将创建一个整数数组,其中包含所有的k。然后在数组上调用shellSort的数组值。然后从已排序的数组中读回,使用数组元素作为关键字查看地图。获取相应的地图值(即线条)并将它们逐个放回tempsArray,最终应该以所需的方式对所有行进行排序。 下面是代码(未经测试)只是为了给出一个想法。

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); 
     } 
    }  
} 
+0

是的,这就是我的意思。谢谢! – iFlash

+0

从地图获取时,使用'myMap.get(countArr [i])'而不是'myMap.get(i)'。我发布的原始答案中有一个错误。我已纠正它。 – kau

+0

噢,如果有两个相同的K,Map找不到合适的行,并且公布其中的一行。 – iFlash

相关问题