2015-01-15 11 views
0

我想寫一個方法,將採取一個ArrayList的學生對象,並返回給我一個字符串數組與學生的姓名的順序他們的分數(學生的名字得分最高將在索引0)。如何將ArrayList轉換爲帶有序元素的字符串數組?

public static void orderStudent(List<Student> ls) { 

    for (Student stu : ls) { 
     System.out.println("Name: " + stu.getName() + ", Score: " 
       + stu.getScore()); 
    } 
} 

上述片段在被執行時將打印像

Name: Alex, Score: 10.35 
Name: Bob, Score: 11.2 
Name: Charles, Score: 8.22 

我希望orderStudent方法返回一個字符串數組,其將具有內容[鮑勃,亞歷克斯,查爾斯]鮑勃是最佳射手其次是亞歷克斯和查爾斯。

+0

你使用哪個Java(8)? – eduardohl

+0

我正在使用Java 7. – Keerthi

+0

* ...返回一個字符串數組... * 第一步,將使該方法'學生[]',而不是'無效'。 –

回答

1

通過列表中的陣列替換:

public static List<String> orderStudent(List<Student> ls) { 
    Collections.sort(ls, new Comparator<Student>() { 
     @Override 
     public int compare(Student o1, Student o2) { 
      return o2.getScore().compareTo(o1.getScore()); 
     } 
    }); 
    List<String> result = new ArrayList<String>(); 
    for(Student s : ls) { 
     result.add(s.getName()); 
    } 
    return result; 
} 

如果使用Java 8將少於兩行...

2

如果Student實現了Comparable接口並按Score排序,那麼您只需對列表進行排序,然後構建您的名稱數組。

如果您不能編輯Student類,則需要編寫一個實現Comparator<Student>的類,然後使用該類對列表進行排序,然後構建您的名稱數組。

簡化@ PrasadKhode的回答是:

public static String[] orderStudent(List<Student> list) { 

    String[] students = new String[list.size()]; 

    Collections.sort(list, new Comparator<Student>() { 
     @Override 
     public int compare(Student object1, Student object2) { 
      return Integer.compare(object2.getScore(), object1.getScore()); 
     } 
    }); 

    for (int index = 0; index < list.size(); index++) { 
     Student student = list.get(index); 
     students[index] = student.getName(); 
     System.out.println("Name: " + student.getName()); 
    } 

    return students; 
} 

有沒有必要每次比較的分數創建CompareToBuilder的一個實例。這是低效的。

+0

是的,我可以做到這一點。但是如果我沒有訪問權限來修改Student類(如果它來自外部jar或框架類) – Keerthi

+0

那麼你寫了一個實現'Comparator '的類並使用它來對數組進行排序。 – Jason

1
  1. 你需要在你的學生來定義對象的一些串序列化(可選)
  2. 你需要排序自定義comporator通過分數的訂單(必填)

    Collections.sort(名單你的對象列表中,比較器C)

    • 名單列表
    • c是比較
  3. 遍歷有序集合和打印

1

你需要實現你的Student類兩件事情:Comparable,並且覆蓋到.toString()方法。首先,確定你的學生類,如:

//used for sorting later 
public int compareTo(Student other) { 
    return this.getScore().compareTo(other.getScore());//Assuming Student.score is not a primitive type 
} 

而且覆蓋.toString()方法如下:

//used for printing later 
public string toString() { 
    return "Name: " + this.getName() + ", Score: " + this.getScore().toString(); 
} 

現在

public class Student implements Comparable<Student>

然後,你必須按照如下方式定義.compareTo()方法你可以簡單地將你的orderStudents函數改爲:

public static void orderStudent(List<Student> ls) { 

// this will sort your collection based on the logic in the `Student.compareTo()` method. 
ls = Collections.sort(ls); 

for (Student stu : ls) { 
//this will print the student object based on the `Student.toString()` method 
     System.out.println(stu); 
    } 
} 
2

首先,你需要理清你的列表,然後你需要構造String []

您可以使用CompareToBuilder而不對現有的POJO類進行任何更改。

在CompareToBuilder上,您需要添加您需要對其集合進行排序的屬性。你可以看到下面的代碼:

import org.apache.commons.lang3.builder.CompareToBuilder; 
... 

public static String[] orderStudent(List<Student> list) { 

    String[] students = new String[list.size()]; 

    Collections.sort(list, new Comparator<Student>() { 
     @Override 
     public int compare(Student object1, Student object2) { 
      return new CompareToBuilder().append(object2.getScore(), object1.getScore()).toComparison(); 
     } 
    }); 

    for (int index = 0; index < list.size(); index++) { 
     Student student = list.get(index); 
     students[index] = student.getName(); 
     System.out.println("Name: " + student.getName()); 
    } 

    return students; 
} 
+0

每次進行比較時,實例化一個CompareToBuilder效率不高,尤其是考慮到比較非常簡單。 – Jason

+0

@Jason有沒有其他的方式來做到這一點,而不必編寫比較邏輯如果我必須根據多個字段進行排序 –

+0

如果比較方法之外可以以某種方式構造'CompareToBuilder',那麼理想情況是什麼?每一次比較都會被構建出來。但我不認爲這是可能的。在這種情況下效率低下,因爲我們只比較一個領域,因此編寫實際比較會更好。 – Jason

相關問題