, 나는이 같은 당신이 필요로하는 모든 정보를 유지하는 Athlete
클래스를 만들 것입니다 :.
public class Athlete {
private int id;
private String firstName;
private String lastName;
private List<Race> class1Races;
private List<Race> class2Races;
public Athlete() {
class1Races = new ArrayList<>(4);
class2Races = new ArrayList<>(4);
}
public int getId() {
return id;
}
//...probably more getters, if you need them elsewhere.
@Override
public String toString() {
String idString = Integer.toString(id);
StringBuilder result = new StringBuilder(60 + idString.length() + firstName.length() + lastName.length() + 52*class1Races.size() + 52*class2Races.size());
result.append("Athlete Number: ");
result.append(idString);
result.append("\nFirst Name: ");
result.append(firstName);
result.append("\nFamily Name: ");
result.append(lastName);
result.append("\nClass1\n");
for(Race race : class1Races) {
result.append(race.toString());
result.append("\n");
}
result.append("Class2\n");
for(Race race : class2Races) {
result.append(race.toString());
result.append("\n");
}
return result.toString();
}
void setId(int id) {
this.id = id;
}
void setFirstName(String firstName) {
this.firstName = firstName;
}
void setLastName(String lastName) {
this.lastName = lastName;
}
void addRaceClass1(Race race) {
class1Races.add(race);
}
void addRaceClass2(Race race) {
class2Races.add(race);
}
}
public class Race { //And accompanying Race class to hold information about a race.
private String name;
private int position;
private int points;
public Race(String name,int position,int points) {
this.name = name;
this.position = position;
this.points = points;
}
@Override
public String toString() {
String positionString = Integer.toString(position);
String pointsString = Integer.toString(points);
StringBuilder result = new StringBuilder(42 + name.length() + positionString.length() + pointsString.length());
result.append("Position and points awarded for ");
result.append(name);
result.append(" race: ");
result.append(positionString);
result.append(" ");
result.append(pointsString);
return result.toString();
}
//...probably some getters, if you need them elsewhere.
}
다음을 , 당신은 그 파일을 파싱 할 필요가있을 것입니다. 나는 완전한 f를 모른다. ILE 사양하지만 예에 의해 나는 추측 할 수 : 클래스의 수는 단지 1, 2 또한 만들 수있는 더 효율적인 것보다 다른 것을 할 수있는 경우
private List<Athlete> parseAthletes(String filename) throws IOException {
List<Athlete> result = new ArrayList<>(12);
BufferedReader br = new BufferedReader(new FileReader(filename));
Athlete athlete = null;
int currentClass = 0;
String line = null; //Loop as long as there are input lines.
while((line = br.readLine()) != null) { //Sets the line variable and reads it all at once!
if(line.startsWith("Athlete Number: ")) {
athlete = new Athlete();
result.add(athlete);
athlete.setId(Integer.parseInt(line.substring(16))); //Throws NumberFormatException if not a number.
currentClass = 0;
} else if(line.startsWith("First Name: ")) {
if(athlete == null) //No Athlete Number was seen yet.
throw new IOException("Wrong format!");
athlete.setFirstName(line.substring(12));
} else if(line.startsWith("Family Name: ")) {
if(athlete == null)
throw new IOException("Wrong format!");
athlete.setLastName(line.substring(13));
} else if(line.startsWith("Class ")) {
if(athlete == null)
throw new IOException("Wrong format!");
currentClass = Integer.parseInt(line.substring(6)); //Throws NumberFormatException if not a number.
} else if(line.startsWith("Position and points awarded for ") && line.contains(" race: ")) {
if(athlete == null || (currentClass != 1 && currentClass != 2))
throw new IOException("Wrong format!"); //Need to have seen an athlete as well as a class number.
String raceName = line.substring(32,line.indexOf(" race: ",32));
line = line.substring(line.indexOf(" race: ",32) + 7); //Trim off the start.
int position = Integer.parseInt(line.substring(0,line.indexOf(" ")));
int points = Integer.parseInt(line.substring(line.indexOf(" ") + 2));
Race race = new Race(raceName,position,points);
if(currentClass == 1)
athlete.addRaceClass1(race);
else //We already know class must be 2.
athlete.addRaceClass2(race);
}
}
return result;
}
당신은 인종의 목록의 목록을해야 할 수도 있습니다 indexOf 쿼리를 캐싱하면되지만, 일반적으로 이러한 종류의 파일 파서에서는 성능이 실제로 문제가되지 않습니다. 당신은 독서를 병렬 처리하고 이진 형식을 사용하는 것이 좋을 것입니다.
마지막으로 ID로 목록을 정렬하려면 Comparator
을 사용할 수 있습니다.
List<Athlete> athletes = parseAthletes("athlete.txt");
Comparator<Athlete> athletesById = new Comparator<Athlete>() {
@Override
public int compare(Athlete a,Athlete b) {
return Integer.compare(a.getId(),b.getId());
}
}
Collections.sort(athletes,athletesById);
for(Athlete athlete : athletes) {
System.out.println(athlete.toString());
}
이 비교기는 ID 만 비교해야 두 Athlete
의를 비교하는 것으로 정의는, 당신은 단지 익명 Comparator
의 인스턴스를 정의 할 수 있습니다에 대한 당신은 완전히 새로운 클래스를 만들 필요가 없습니다.
내가 여기 저기에 오타 또는 논리 오류가있을 수 있으므로이 코드를 철저히 테스트하거나 더 나은 방법으로 아이디어를 얻은 다음 코드를 직접 작성하십시오.
아이디어를 생각해 보셨나요? 기본적으로 파일을 구문 분석해야합니다. 당신이 그들을 읽을 때 오브젝트에 넣을 수도 있고, (Comparator 인터페이스를 사용하여) 선수 번호별로 정렬 한 다음 toString 메소드를 작성하여 각 운동 선수를위한 문자열을 얻을 수도 있습니다. – Cruncher
실제로 코드를 작성한 다음 특정 문제에 대한 도움을 요청할 수 있습니까? 우리는 당신을 위해 숙제를하고 싶지 않습니다. –
입력 파일의 형식과 운동 선수를 분류하기 위해 지금까지 작성한 코드를 지정하십시오. –