方法内部的java数组排序

ecfdbz9o  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(241)

我必须完成一个基于战斗模拟游戏的学校作业。你能看一下我的帮会职业代码吗,我对排序功能有一些问题。没有它也可以工作,但我仍然想实现它。get members方法应该根据玩家的等级对数组进行排序,然后使用此数组来组成成员组
本公会职业

import java.util.Collections;
import java.util.Comparator;

public  class Guild {
    ArrayList<Character> memebers;

    public Guild() {
        memebers = new ArrayList<>();
    }

    public void addMember(Character member){
        memebers.add(member);
    }

    public Collection<Character> getMembers(){
        return (Collection<Character>)memebers;
    }

    public StudentTeam getTeam(Team enemyTeam){

        StudentTeam team = new StudentTeam("StudentTeam");

      **memebers.sort(new Comparator<Character>(){
            public int compare(Character c1, Character c2) {
                if (c1.getLevel() > c2.getLevel()){
                    return 1;
                }else if (c1.getLevel() < c2.getLevel()){
                    return  -1;
                }else {
                    return 0;
                }
            }
        });**

        for (Character member : memebers){
            team.addMember(member);
            member.setTeam(team);
        }

        return team;
    }

} ``

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题