简体   繁体   中英

Trying to sort arraylist with more than one type of variable in object

I'm trying to Sort an arraylist with objects with more than one type of variable, eg ints and strings.

I have no clue at all how to start this off. I will post my code below, if you need any more information, I'll be camping this.

Sorry, I'm very tired and I don't know where to start.

Also, as I'm giving you all of my code, I also need help with trying to delete specific objects from an array list. how do I know I'm deleting what I want to delete? etc.

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

}

Thank you.

did you mean something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM