簡體   English   中英

Java - 如何對此ArrayList進行排序?

[英]Java - How do I sort this ArrayList?

我是Java的新手,我剛剛開始學習計算機科學大學課程。

我正在編寫一個程序,它有一個ArrayList ,它包含Personality對象,稱為individual ,在PersonalityList類中聲明如下: private ArrayList<Personality> individual;

這些Personality對象擁有一個名為int類型的votes的私有字段。

我還在PersonalityList類中聲明了相同的ArrayList ,但是使用了不同的名稱 - rankedList - 就像這樣: private ArrayList<Personality> rankedList;

在我的程序中,我創建了一個名為top(int value) 在這個方法中,我試圖使用while循環查看individual列表,從該列表中獲取對象, rankedList是它們的索引號低於value參數的value ,將其添加到rankedList列表,然后對rankedList列表進行排序他們的votes字段的順序,具有最高票數的對象在列表中排在第一位(索引:0)。 目前,這是我top方法:

public void top(int value)
{
    int index = 0;
    int listSize = individual.size();
    rankedList = new ArrayList<Personality>();

    if(value > listSize) {
        value = listSize;
    }
    else {
        System.out.println("Error: Enter a positive integer value.");
    }

    if(listSize > 0) {
            while(index < value) {
                Personality prsn = individual.get(index);
                rankedList.add(prsn);
                System.out.println(prsn.getDetails());
                index++;
            }        
    }
    else {
        System.out.println("Error: The array list has no personalities stored inside it.");
    }
}

我目前正在使用Personality類中定義的getDetails()方法(包含對象字段)打印while循環獲取的每個Personality對象的詳細信息,只是為了檢查它是否獲得了正確數量的對象。

我知道我需要使用Collections.sort功能查看其他Stack Overflow帖子,但是,即使查看了這些帖子的答案,我也不知道如何將其實現到我的代碼中。 我已經嘗試但是我一直在向我拋出錯誤,我並不理解。

任何幫助,最好是具體的代碼,將非常感謝。 謝謝!


更新:

感謝示例代碼@camickr。 在我的Personality類中,我添加了以下代碼:

static class votesComparator implements Comparator<Personality>
{
    public int compare(Personality p1, Personality p2)
    {
    return p1.getVotes() - p2.getVotes();
    }
}

我還在PersonalityList類中編寫了我的top方法:

if(listSize > 0) {
    while(index < value) {
        Personality prsn = individual.get(index);
        rankedList.add(prsn);
        System.out.println(prsn.getDetails());
        index++;
    }        
    Collections.sort(rankedList);
    System.out.println("Sort by Natural order");
    System.out.println("\t" + people);
}
else {
    System.out.println("Error: The array list has no personalities stored inside it.");
}

但是現在我收到一條錯誤,指出“找不到合適的sort(java.util.List)方法”,在Collection.sort(rankedList)調用。 這是什么原因?

您發布的代碼與排序無關。 重要的代碼是你的Personality類。 你要么:

  1. 需要在你的課上實現Comparable
  2. 創建自定義Comparator

這是一個示例,顯示了兩種方法的示例:

/*
**  Use the Collections API to sort a List for you.
**
**  When your class has a "natural" sort order you can implement
**  the Comparable interface.
**
**  You can use an alternate sort order when you implement
**  a Comparator for your class.
*/
import java.util.*;

public class Person implements Comparable<Person>
{
    String name;
    int age;

    public Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }

    public String toString()
    {
        return name + " : " + age;
    }

    /*
    **  Implement the natural order for this class
    */
    public int compareTo(Person p)
    {
        return getName().compareTo(p.getName());
    }

    static class AgeComparator implements Comparator<Person>
    {
        public int compare(Person p1, Person p2)
        {
            return p1.getAge() - p2.getAge();
        }
    }

    public static void main(String[] args)
    {
        List<Person> people = new ArrayList<Person>();
        people.add( new Person("Homer", 38) );
        people.add( new Person("Marge", 35) );
        people.add( new Person("Bart", 15) );
        people.add( new Person("Lisa", 13) );

        // Sort by natural order

        Collections.sort(people);
        System.out.println("Sort by Natural order");
        System.out.println("\t" + people);

        // Sort by reverse natural order

        Collections.sort(people, Collections.reverseOrder());
        System.out.println("Sort by reverse natural order");
        System.out.println("\t" + people);

        //  Use a Comparator to sort by age

        Collections.sort(people, new Person.AgeComparator());
        System.out.println("Sort using Age Comparator");
        System.out.println("\t" + people);

        //  Use a Comparator to sort by descending age

        Collections.sort(people, Collections.reverseOrder(new Person.AgeComparator()));
        System.out.println("Sort using Reverse Age Comparator");
        System.out.println("\t" + people);
    }
}
Collections.sort(rankedList,new Comparator<Personality>(){
                     public int compare(Personality p1,Personality p2){
                           // Write your logic here.
                     }});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM