gpt4 book ai didi

Java - 如何对这个 ArrayList 进行排序?

转载 作者:行者123 更新时间:2023-11-29 04:56:53 28 4
gpt4 key购买 nike

我是 Java 的新手,我才刚刚开始为我的计算机科学大学类(class)学习它。

我正在编写一个程序,它有一个 ArrayList拥有 Personality称为 individual 的对象, 在 PersonalityList 中声明这样的类:private ArrayList<Personality> individual;

这些 Personality对象拥有一个名为 votes 的私有(private)字段类型 int .

我也声明了相同的ArrayListPersonalityList类,但名称不同 - rankedList - 像这样:private ArrayList<Personality> rankedList;

在我的程序中,我创建了一个名为 top(int value) 的方法.在这种方法中,我试图查看 individual使用 while 循环列出,从该列表中获取对象,前提是它们的索引号低于 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();
}
}

我还编辑了我的 top我的方法 PersonalityList类:

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.");
}

但是现在我收到一个错误,指出“没有找到合适的排序方法(java.util.List)”,在 Collection.sort(rankedList) 调用.这是什么原因?

最佳答案

您发布的代码与排序无关。重要的代码是您的 Personality 类。你要么:

  1. 需要在您的类上实现 Comparable 或
  2. 创建自定义比较器

这是一个示例,展示了两种方法的示例:

/*
** 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);
}
}

关于Java - 如何对这个 ArrayList 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33457728/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com