Java 尝试在对象中使用多种类型的变量对arraylist进行排序
我正在尝试对arraylist进行排序,其中的对象包含多种类型的变量,例如int和string 我一点也不知道该怎么开始。我将在下面发布我的代码,如果你需要更多信息,我将在这里发布 对不起,我很累,不知道从哪里开始 另外,在我向您介绍我所有的代码时,我还需要帮助从数组列表中删除特定对象。我如何知道我正在删除我要删除的内容?等等Java 尝试在对象中使用多种类型的变量对arraylist进行排序,java,oop,sorting,arraylist,Java,Oop,Sorting,Arraylist,我正在尝试对arraylist进行排序,其中的对象包含多种类型的变量,例如int和string 我一点也不知道该怎么开始。我将在下面发布我的代码,如果你需要更多信息,我将在这里发布 对不起,我很累,不知道从哪里开始 另外,在我向您介绍我所有的代码时,我还需要帮助从数组列表中删除特定对象。我如何知道我正在删除我要删除的内容?等等 package coursework; import java.util.*; /** * * @author w1469384 */ public class
package coursework;
import java.util.*;
/**
*
* @author w1469384
*/
public class PremierLeagueManager implements LeagueManager{
public static void main(String[] args) {
Scanner c1 = new Scanner(System.in);
ArrayList<FootballClub> PL = new ArrayList<FootballClub>();
int choice;
System.out.println("Enter 1; To create a club, 2; To Delete a Club, 3; To display all clubs and 99 to close the program");
choice = c1.nextInt();
//Creates and adds a new FootballClub Object
while (choice != 99){
if (choice == 1){
System.out.println("Please Enter The games played for the club");
int played = c1.nextInt();
System.out.println("Please enter the number of wins");
int wins = c1.nextInt();
System.out.println("please enter the number of losses");
int losses = c1.nextInt();
System.out.println("please enter the number of draws");
int draws = c1.nextInt();
System.out.println("please enter the number of goals for");
int goalsFor = c1.nextInt();
System.out.println("please enter the number of goals against");
int goalsAgainst = c1.nextInt();
System.out.println("Please Enter the name of the club");
//Due to Java's kinks, There will be a filler next line here.
String yo = c1.nextLine();
String name = c1.nextLine();
System.out.println("please enter the Location of the club");
String location = c1.nextLine();
System.out.println("Please Enter the Capacity of the club's stadum");
int capacity = c1.nextInt();
int points = (wins*3)+draws;
FootballClub club = new FootballClub(played, wins, losses, draws, goalsFor, goalsAgainst, points, name, location, capacity);
PL.add(club);
System.out.println("Stuff bb o yeah");
}
//Deletes a FootballClub Object
if (choice == 2){
String find;
System.out.println("Please enter the name of the club you wish to delete, exactly as it was entered");
find = c1.nextLine();
int index=Collections.binarySearch(PL, find);
}
//Displays all Football Clubs in the PremierLeague array
if (choice == 3){
String bigname = c1.nextLine();
for(int i = 0; i < PL.size(); i++){
FootballClub obj = PL.get(i);
if(obj.getName().equals(bigname)){
System.out.println(i);
}
}
}
choice = c1.nextInt();
}
}
}
public abstract class SportsClub {
public String name;
public String location;
public int capacity;
public void setName(String inName){
name = inName;
}
public void setLocation(String inLocation){
location = inLocation;
}
public void setCapacity(int inCapacity){
capacity = inCapacity;
}
public String getName(){
return name;
}
public String getLocation(){
return location;
}
public int getCapacity(){
return capacity;
}
}
public class FootballClub extends SportsClub {
//Statistics for the club.
int played;
int wins;
int losses;
int draws;
int goalsFor;
int goalsAgainst;
int points;
String inName;
String inLocation;
int inCapacity;
public FootballClub(int gPlayed, int gWins, int gLosses, int gDraws, int gFor, int gAgainst, int gPoints, String inName, String inLocation, int inCapacity){
played = gPlayed;
wins = gWins;
losses = gLosses;
draws = gDraws;
goalsFor = gFor;
goalsAgainst = gAgainst;
points = gPoints;
name = inName;
location = inLocation;
capacity = inCapacity;
}
@Override
public String toString() {
return String.format("The Club is " + name + " And is based in " + location + " and has " + points + "points");
}
public void setPlayed(int newPlayed){
played = newPlayed;
}
public void setWins(int newWins){
wins = newWins;
}
public void setLosses(int newLosses){
losses = newLosses;
}
public void setDraws(int newDraws){
draws = newDraws;
}
public void setGoalsFor(int newGoalsFor){
goalsFor = newGoalsFor;
}
public void setGoalsAgainst(int newGoalsAgainst){
goalsAgainst = newGoalsAgainst;
}
public void setPoints(int newPoints){
points = newPoints;
}
public int getPlayed(){
return played;
}
public int getWins(){
return wins;
}
public int getLosses(){
return losses;
}
public int getDraws(){
return draws;
}
public int getGoalsFor(){
return goalsFor;
}
public int getGoalsAgainst(){
return goalsAgainst;
}
public int getPoints(){
return points;
}
}
套装课程;
导入java.util.*;
/**
*
*@作者w1469384
*/
公共类PremierLeagueManager实现LeagueManager{
公共静态void main(字符串[]args){
扫描仪c1=新扫描仪(系统英寸);
ArrayList PL=新的ArrayList();
智力选择;
System.out.println(“输入1;创建俱乐部,2;删除俱乐部,3;显示所有俱乐部,99关闭程序”);
choice=c1.nextInt();
//创建并添加新的足球俱乐部对象
while(选项!=99){
如果(选项==1){
System.out.println(“请输入为俱乐部玩的游戏”);
int played=c1.nextInt();
System.out.println(“请输入获胜次数”);
int wins=c1.nextInt();
System.out.println(“请输入损失数量”);
int loss=c1.nextInt();
System.out.println(“请输入提款数量”);
int draws=c1.nextInt();
System.out.println(“请输入目标数”);
int goalsFor=c1.nextInt();
System.out.println(“请输入目标数量”);
int goalsantim=c1.nextInt();
System.out.println(“请输入俱乐部名称”);
//由于Java的缺陷,下一行将有一个填充符。
字符串yo=c1.nextLine();
字符串名称=c1.nextLine();
System.out.println(“请输入俱乐部的位置”);
字符串位置=c1.nextLine();
System.out.println(“请输入俱乐部球场的容量”);
int capacity=c1.nextInt();
积分=(赢*3)+平局;
足球俱乐部=新的足球俱乐部(比赛、胜利、失败、平局、进球数、进球数、得分、名称、位置、容量);
PL.add(俱乐部);
System.out.println(“Stuff bb o yeah”);
}
//删除足球俱乐部对象
如果(选项==2){
字符串查找;
System.out.println(“请输入您希望删除的俱乐部名称,与输入的名称完全一致”);
find=c1.nextLine();
int index=Collections.binarySearch(PL,find);
}
//显示英超联赛阵列中的所有足球俱乐部
如果(选项==3){
字符串bigname=c1.nextLine();
对于(int i=0;i
谢谢。你是说这样的事吗
Collections.sort(PL, new Comparator<FootballClub>() {
@Override
public int compare(FootballClub footballClub1, FootballClub footballClub2)
{
return footballClub1.inName.compareTo(footballClub2.inName);
}
});
Collections.sort(PL,新的Comparator(){
@凌驾
公共int比较(足球俱乐部足球俱乐部1、足球俱乐部足球俱乐部2)
{
返回footballClub1.inName.compareTo(footballClub2.inName);
}
});
您需要编写自己的equals方法来比较对象。但是,像object.equals()这样的方法如何呢?而且,正是整数和字符串让我感到厌烦。我在互联网上找不到任何类似的没有意义的方法D:然后编写一个通用的比较器方法,它接受一个int或一个字符串,如果大于0,则返回1,如果小于,则返回-1。有一种叫做自然秩序的东西被使用