게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
[JAVA] ArrayList 질문글 전체소스 입니다.
게시물ID : programmer_8428짧은주소 복사하기
작성자 : 광패
추천 : 0
조회수 : 752회
댓글수 : 11개
등록시간 : 2015/03/03 21:30:42
옵션
  • 본인삭제금지
사이트에 제출용이라 좀 허접하지만 올리겠습니다.

import java.util.*;

public class Olympiad{
public int Nation;
public int StudentNum;
public int Score;

public Olympiad(int Nation,int StudentNum,int Score) {
this.Nation=Nation;
this.StudentNum=StudentNum;
this.Score=Score;
}
public String toString(){
return Nation+" "+StudentNum;
}
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int count;
ArrayList<Integer> inputNation=new ArrayList<>(); 
ArrayList<Integer> inputStudentNumber=new ArrayList<>(); 
ArrayList<Integer> inputScore=new ArrayList<>(); 
ArrayList<Object> sample=new ArrayList<>();
count=scan.nextInt();
for(int i=0; i<count; i++){
inputNation.add(scan.nextInt());
inputStudentNumber.add(scan.nextInt());
inputScore.add(scan.nextInt());
sample.add(new Olympiad(inputNation.get(i),
inputStudentNumber.get(i),inputScore.get(i)));
}
Collections.sort(sample, new CompareScore());
//요기가 제가 질문한 부분이 되겠네요.
//이 반복문에서는 정렬된 배열의 3번째 원소까지만 출력하게 되어있습니다.
//그런데 클래스 안의 Nation이라는 원소가 2번이상 중복되게 되면 안되기 때문에
//배열 안에 있는 클래스들의 Nation값을 비교해야만 합니다.
//BUT!!! get()함수를 쓸 경우에 toString()값인 StudentNumber와 Score가 출력되기 때문에
//Nation값에 접근조차 못하고 있습니다.

for(int i=0;i<3;i++){
System.out.println(sample.get(i));
}
}
}

class CompareScore implements Comparator<Object>{
public int compare(Object o1, Object o2){
int score1=((Olympiad)o1).Score;
int score2=((Olympiad)o2).Score;
if(score1>score2)
return -1;
if(score1==score2)
return 0;
return 1;
}
}



제가 주석달아놓은 부분을 봐주세요~
요점은 배열 안에 있는 클래스들의 Nation값을 비교하고 싶은것 입니다.

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호