2014-03-05 60 views
0

我一直在嘗試比較數組中的對象,如果它的屬性使我可以按照降序對數組中的對象進行排序。下面是示例代碼:該數組是Candidate[][]比較數組中的對象進行排序

System.out.println("How many positions for this election? > "); 
    numberOfPositions = sc.nextInt(); 
    Candidate Candidate[][] = new Candidate[numberOfPositions][]; 
    PoliticalParty Parties[][] = new PoliticalParty[numberOfPositions][]; 
    for(int i=0;i<numberOfPositions;i++){ 
     String name; 
     String politicalParty; 
     System.out.println("Enter position name > "); 
     position = sc.next(); 
     System.out.println("How many seats? > "); 
     numberOfSeats = sc.nextInt(); 
     System.out.println("How many candidates? > "); 
     numberOfCandidates = sc.nextInt(); 
     Candidate[i] = new Candidate[numberOfCandidates+1]; 
     Candidate[i].sort(votes); //<--------------------------This is what im trying// 

其中(票)是使用這個代碼從文本文件中導出一個int:

System.out.println("Enter file name > "); 
    filename = sc.next(); 
    try { 
     filescan = new Scanner(new File(filename)); 
    } catch (FileNotFoundException ex) { 
     //Logger.getLogger(Election.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    String L = System.lineSeparator(); 
    filescan.useDelimiter(L); 
    while (filescan.hasNext()) { 
     numberOfVoters++; 
     line = filescan.next(); 
     for(int x=0,j=0;j<line.length();j++){ 
      switch(line.charAt(j)){ 
       case ',': 
        x++; 
        break; 
       case ' ': 
        break; 
       default: 
        int y = line.charAt(j)-48; 
        //Integer.parseInt(line.charAt(j).toString()); 
        Candidate[x][y].addVote(); 
        break; 
      } 
     } 

其中(表決)被封裝在另一個類中:

public class Candidate{ 
int votes = 0; 
String politicalParty; 

public Candidate(String name, String politicalParty) { 
    super(name); 
    this.politicalParty = politicalParty; 
} 

public void addVote() { 
    this.votes++; 
    //return votes; 
} 

public int getVotes() { 
    return votes; 
} 

@Override 
public String getName() { 
    return getName(); 
} 

public void displayFields(){ 
    System.out.println(this.getName() + " (" + getPoliticalParty() + ") - " + votes); 
} 

public String getPoliticalParty() { 
    return politicalParty; 
} 

public void setPoliticalParty(String politicalParty) { 
    this.politicalParty = politicalParty; 
} 
} 
+0

看一下Comparable接口http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html – Averroes

回答

1

陣列有一個預製的排序方法。 Javadoc for Arrays.sort(Object[] a)提到了「自然順序」。 Comparable接口存在以提供自然順序。

步驟1

應用接口類。

  • public class Candidate implements Comparable<Candidate> {

步驟2

實現你們班compareTo(Candidate c) {}方法。

閱讀Javadoc forcompareTo()合同。通常,如果this.property分別大於,等於或小於c.property,則它必須返回正數,零或負數。 property是您正在比較的領域。

  • 提示:如果property是一個字符串,你可以簡單地重複使用字符串的compareTo()
    • return this.property.compareto(c.property);
  • 提示:如果property是一個整數(如票),你可以巧妙地營造積極,零或負數通過採取差異。
    • return this.votes - c.votes;

步驟3

排序的陣列。如果你有一個Collection或Arrays.sort(list)你有一個對象數組。

0

,我建議你使用ArrayList來存儲要排序的元素,然後你就會有2種選擇:使你的項目具有可比性(界面),或者創建一個比較器(INTERF ACE):

public class Candidate implements Comparable<Candidate> { 
    ... 

public int compareTo(Candidate c) { 
    ... //compare here the attributes of this and c 
} 

}

0

快速的問題,答案很簡單:java.util.Arrays.sort()

+0

您錯過了「比較器」或實現「Comparable」。 –

+0

想到OP會很快爲他自己找出一個:) – JimmyB

0
  1. 爲什麼你的變量從大寫字母開始?它應該像來自小的所有變量。
  2. 您應該使用集合來存儲自定義數據類型,然後您可以使用Collections.sort(List<T> list, Comparator<? super T> c)對其進行排序,並根據需要定義自己的Comparator