Reputation: 37
Arrays:
static String[] names;
static int[] points;
static int[] goals_scored;
static int[] goles_received;
I am asked in an exercise to get the second highest number in points array, then display the second highest team by it's name, then the points, the goals, etc like this e.g.:
Name: FC Spain
Points: 50
Goals scored: 43
Goals received: 27
I though about sorting the array but the problem is that if I sort the points array, then, the second highest position of points would not be the same as the names/goals array, also i can not sort also the goals arrays because the best team (e.g.) won't have the max goals. Right?
But also in another execise I have to display the data of the teams according to a logical criterion (like in ascending order) so I do not know what to do.
EDIT: Also, the user enters the max values that the arrays can have.
Upvotes: 1
Views: 64
Reputation: 38
You should create a class that has those fields(Name, Points, Goals scored and Goals recieved). Then you should create an array of that class. And just create those teams. And sort them out by it's goals... So the class should look something like this:
public class Team{
private String name;
private int points;
private int goalsScored;
private int goalsRecieved;
public Team(String teamName, int p, int gs, int gr){
this.name = teamName;
this.points = p;
this.goalsScored = gs;
this.goalsRecieved = gr;
}
public int getPoints(){
return this.points;
}
public void setPoints(int points){
this.points = points;
}
public void setName(String name){
this.name = name;
}
}
And then you should create the array of that class somewhere and sort it by points:
public class Main{
public static void main(String[] args){
Team[] teams = new Team[numberOfTeams];
//initialize every team in the array like this:
teams[0] = new Team("Spain", 50, 43, 27);
//and so on for every team
//and then sort them out by teams[index].getPoints();
}
}
Upvotes: 1