2014-07-02 232 views
0

我正在編寫一個Java應用程序,該應用程序應該讀取包含每行記錄的輸入文件,並用逗號分隔這些字段。應用程序的輸出應該在標準輸出格式如下:按對象排序對象的ArrayList屬性並組織它們

Age1 
    Sex1 
    name1 
    name2 
    Sex2 
    name3 
    name4 
e.g 
33 
    Female 
    Jayanta 
    Kanupriya 
44 
    Female 
    Lajjo 
    Rajjo 
    Male 
    Raghav 
    Sitaram 

等等....

我創建了一個Person類來保存數據,並實現與接口的compareTo可比接口對年齡進行初步分類並對性別進行二級分類。我可以稍後在名稱上添加三級排序。

public class Person implements Comparable<Person>{ 

    private String name; 
    private int age; 
    private String sex; 

    /** 
    * @param age, sex and name 
    */ 
    public Person(String name, int age, String sex) { 
     this.setName(name); 
     this.setAge(age); 
     this.setSex(sex); 

    } 

    //getters and setters 

    @Override 
    public int compareTo(Person person) { 
     return this.age < person.getAge()?-1:this.age > person.getAge()?1:sortOnGender(person); 
    } 

    public int sortOnGender(Person person) { 
     if (this.sex.compareToIgnoreCase(person.getSex()) < 0) { 
      return -1; 
     } 
     else if (this.sex.compareToIgnoreCase(person.getSex()) > 0) { 
      return 1; 
     } 
     else { 
      return 0; 
     } 
    } 

    @Override 
    public String toString() { 
     StringBuilder sb = new StringBuilder(); 
     sb.append(String.valueOf(this.age)).append('-').append(this.sex).append('-').append(this.name); 
     return sb.toString(); 
    } 
} 

接下來在主要方法的App類中,我創建了Person的ArrayList,並填充數據並使用Collections.sort()進行排序。我打印了ArrayList,並按排序順序排列。然而,問題是如何獲得所需格式的數據。

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import java.util.StringTokenizer; 

public class App { 

    //An array of persons 
    private static List<Person> persons = new ArrayList<Person>(); 

    public static void main(String[] args) { 

     File inputfile = new File(args[0]); 

     if(inputfile.exists()) { 
      String line; 
      BufferedReader br; 
      StringTokenizer st = null; 
      try { 
       br = new BufferedReader(new FileReader(inputfile)); 
       // Read comma seperated file line by line 
       while((line = br.readLine()) != null) { 

        //Break the line using "," as field seperator 
        st = new StringTokenizer(line, ","); 

        // Number of columns in the line 
        int numcols = st.countTokens(); 

        if(numcols > 0) { 
         String[] cols = new String[st.countTokens()]; 

         // Till the time columns are there in the line 
         while(st.hasMoreTokens()) { 
          for(int i=0; i < numcols; i++) { 
           cols[i] = st.nextToken(); 
          } 
         } 

         // If there are elements in the cols array 
         if(cols.length !=0) { 
          // Create a list of persons 
          persons.add(new Person(cols[0].trim(), Integer.parseInt(cols[1].toString().trim()), cols[2].trim())); 
         } 
        } 
        else { 
         // Print error and continue to next record (Takes care of blank lines) 
         System.err.println("Error: No columns found in line"); 
        } 
       } 
       // Sort the Collection 
       Collections.sort(persons); 
       br.close(); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
     else { 
      System.out.println("Specify the location of the input file"); 
     } 

     System.out.println(persons); 

    } 
} 
+0

「所需的格式」到底是什麼?什麼樣的格式? –

+0

歡迎來到SO。你試圖解決這個問題的嘗試是什麼? – Daniel

+0

看起來好像你想通過操作來執行一個n維組,在你的例子中,n = 2。番石榴可以幫你做到這一點,或者你可以自己做。 –

回答

0

一旦你有一個排序的列表,你應該能夠做這樣的事

int currentAge = -1; 
String currentSex = null; 

for (Person person : persons) { 
    if (person.getAge() != currentAge) { 
     currentAge = person.getAge(); 
     currentSex = null; 
     System.out.println(person.getAge()); 
    } 
    if (!person.getSex().equals(currentSex)) { 
     currentSex = person.getSex(); 
     System.out.println("\t" + person.getSex()); 
    } 

    System.out.println("\t\t" + person.getName()); 
} 
+0

感謝您的快速解決方案。有效。 –

1

我不知道我跟着你,但你只需要編寫,將顯示你的方法想要!

如果你想在你可以嘗試像控制檯打印:

Public printListPersons(persons[] p){ 
    for(Person p : persons){ 
     System.out.println(p[i].getAge); 
     System.out.println("\n"); 
     System.out.println("\t" + p[i].getSex); 
     .... 
    } 
}