尝试在对象中使用多种类型的变量对arraylist进行排序

时间:2014-11-17 05:54:20

标签: java oop sorting arraylist

我试图用具有多种类型变量的对象对一个arraylist进行排序,例如整数和字符串。

我根本不知道怎么开始这个。我将在下面发布我的代码,如果您需要更多信息,我将会露营。

对不起,我很累,而且我不知道从哪里开始。

此外,由于我提供了所有代码,因此我还需要帮助尝试从数组列表中删除特定对象。我怎么知道我删除了我要删除的内容?等

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;
}

}

谢谢。

1 个答案:

答案 0 :(得分:2)

你的意思是这样吗

Collections.sort(PL, new Comparator<FootballClub>() {
    @Override
    public int compare(FootballClub footballClub1, FootballClub  footballClub2)
    {
        return  footballClub1.inName.compareTo(footballClub2.inName);
    }
});