2016-12-05 57 views
0

我想按字母順序排列候選人名稱,同時對候選人獲得的票進行排序,我把兩個數組作爲名稱,另一個作爲投票,因爲我排序的名稱數組需要在這裏排序我可以「T使其排序,請幫助 這裏是我的代碼:如何對不同數據類型的數組進行排序

package com.sarga.Swinglearn; 
import java.util.Scanner; 

public class Project3 { 

public static void main(String[] args) 
{ 

    int i=0,j=0; 
    Scanner s=new Scanner(System.in); 
    System.out.println("Enter number of candidates"); 
    int candcount = Integer.parseInt(s.nextLine()); 
    System.out.println("Enter name of the candiadates"); 
    String names[]=new String[candcount];//create an array 
    for(i=0;i<names.length;i++) 
    { 
     names[i]=s.nextLine(); 
    } 
    System.out.println("candidates are: "); 
    for(i=0;i<candcount;i++) 
     System.out.println(names[i]); 
    for(i=0;i<candcount;i++) 
    { 
     for(j=i;j<candcount;j++) 
     { 
      if(names[i].compareTo(names[j])>0) 
      { 
       String temp=names[i]; 
       names[i]=names[j]; 
       names[j]=temp; 
      } 
     } 
    } 
    /*To sort names alphabetically*/ 
    System.out.println("alphabetical order of candidates"); 
    for(i=0;i<candcount;i++) 
    { 
     System.out.println(names[i]); 
    } 
    System.out.println("Enter number of votes of each candidate"); 
    int votes[]=new int[candcount]; 
    for(i=0;i<candcount;i++) 
    { 
     votes[i]=s.nextInt(); 
     System.out.println(names[i]+":"+votes[i]); 
    } 
    //sort names based on their votes 
    System.out.println("List of candidates according to their votes"); 
    //int max= votes[1]; 
    int temp=0; 
    for(i=0;i<candcount-1;i++) 
    { 
     for(j=i;j<candcount;j++) 
     { 
      if(votes[i]<votes[j]) 
      { 
      temp=votes[i]; 
      votes[i]=votes[j]; 
      votes[j]=temp; 
      } 
     } 
    } 
    for(i=0;i<candcount;i++) 
    System.out.println(names[i]+":"+votes[i]); 
    s.close(); 
} 

} 
+3

我建議你創建一個保存姓名和投票數的一類。然後你可以創建一個這種類型的數組,當按票數進行排序時,你會得到正確的名字順序。順便說一句:數組中有[Sort](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-T:A-java.util.Comparator-) - 類 – Fildor

+3

創建一個新的對象來封裝名稱和投票並存儲在'ArrayList'中,並使用'Collections.sort'和自定義比較器 – GurV

回答

0

你使用面向對象的範式;創建一個Candidate類,它實現了Comparable接口:

public class Candidate 
    implements Comparable<Candidate> 
{ 
    public String name; /* should use getter and setter */ 
    public int votes; /* idem */ 

    public int compareTo(Candidate other) 
    { 
     /* implements the comparison, see Comparable doc */ 
    } 
} 

然後在你的主排序候選數組:

Candidate[] candidates = new Candidate[candcount]; 
/* populates the array */ 
Arrays.sort(candidates); 
1

創建Candidate類:

public class Candidate implements Comparable<Candidate> { 
    private String name; 
    private int votes; 

    public Candidate(String name, int votes) { 
     this.name = Objects.requireNotNull(name); 
     this.votes = votes; 
    } 

    // Getters and setters 

    @Override 
    public int compareTo(Candidate that) { 
     int c = this.name.compareTo(that.name); 
     if(c != 0) return c; 
     return this.votes - that.votes; 
    } 
} 

下一頁創建這些候選人名單,並對其進行排序:

List<Candidate> clist = new ArrayList<>(); 
// Add some Candidates to clist 
Collections.sort(clist); 
相關問題