2014-03-06 48 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

相關問題