java comparable list_使用Comparable对ArrayList进行排序

尝试使用Comparable,我对编程很新,并且从未对ArrayList进行排序 . 我已经看了一些关于堆栈溢出的其他示例并查看了Java Doc但是它相当令人困惑,我不确定如何将它应用到我的程序中

基本上我有两个类,一个Personality类和一个PersonalityList类 . PersonalityList类包含一个名为personalities的Personality数组,它在Array List中存储了许多个性对象 .

我需要按每个人格的投票数对其进行排序 . 方法top(int topValue)应该返回一个长度为topValue的新数组,其中Personality对象的投票率最高 .

我知道我需要在Personality类中使用一些Comparable,但不确定如何执行此操作 .

到目前为止,这是我的PersonalityList类:

import java.util.ArrayList;

import java.util.Iterator;

import java.util.Collections;

public class PersonalityList

{

private ArrayList personalities; //Create ArrayList of Personality, called personalities.

private ArrayList sortedPersonalities;

/**

* Constructor for objects of class PersonalityList

*/

public PersonalityList()

{

personalities = new ArrayList(); //Initialise personalities ArrayList.

sortedPersonalities = new ArrayList();

}

/**

* Adds a personality to the ArrayList of Personality, called personalities.

*/

public void addPersonality(Personality personality)

{

personalities.add(personality);

}

/**

* Returns the number of Personality objects in the ArrayList

*/

public int getSize()

{

return personalities.size();

}

/**

* Lists the details of all the Personality objects stored in the ArrayList

*/

public void list()

{

System.out.println("Personality List");

for(Personality personality : personalities) { //Iterates through each personality in ArrayList

System.out.println(personality.getDetails());

}

System.out.println();

}

/**

* Adds one vote to the personality which matches the name entered into the method

*/

public void voteFor(String name)

{

boolean nameFound = false; //Boolean variable to identify if the personality has been found

int index = 0;

while (index < personalities.size() && !nameFound) {

Personality personality = personalities.get(index);

String compName = personality.getName();

if (compName.equals(name)) { //Adds a vote if the name is found

personality.increaseVotes(1);

nameFound = true;

}

index++;

}

if (nameFound == false) { //Error message if name not found

System.out.println(name + " could not be found.");

}

}

/**

* Removes personalities if they have less votes than the parameter value

*/

public void shortlist(int minimumVotes)

{

Iterator it = personalities.iterator();

while(it.hasNext()) {

Personality personality = it.next();

int currentP = personality.getVotes();

if (currentP < minimumVotes) {

it.remove();

}

}

}

/**

*

*/

public Personality top(int topValue)

{

int index = 0;

int listSize = personalities.size();

if (topValue > listSize) {

topValue = listSize;

}

if(listSize > 0) {

//Coppies the ArrayList personalities to the sortedPersonalities ArrayList

while(index < topValue) {

Personality sortedPersonality = personalities.get(index);

sortedPersonalities.add(sortedPersonality);

System.out.println(sortedPersonality.getDetails());

index++;

}

Collections.sort(sortedPersonalities, Collections.reverseOrder(new Personality.votesComparator()));

System.out.println("Sorted by Votes");

System.out.println("\t" + people);

}

else {

System.out.println("No personalities are currently in the Array List");

}

return sortedPersonalities ;

}

}

提前致谢 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值