사이트에 제출용이라 좀 허접하지만 올리겠습니다. <div><br></div> <div><div><font size="3">import java.util.*;</font></div> <div><font size="3"><br></font></div> <div><font size="3">public class Olympiad{</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>public int Nation;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>public int StudentNum;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>public int Score;</font></div> <div><font size="3"><br></font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>public Olympiad(int Nation,int StudentNum,int Score) {</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>this.Nation=Nation;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>this.StudentNum=StudentNum;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>this.Score=Score;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>public String toString(){</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>return Nation+" "+StudentNum;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>public static void main(String[] args){</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>Scanner scan=new Scanner(System.in);</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>int count;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>ArrayList<Integer> inputNation=new ArrayList<>(); </font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>ArrayList<Integer> inputStudentNumber=new ArrayList<>(); </font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>ArrayList<Integer> inputScore=new ArrayList<>(); </font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>ArrayList<Object> sample=new ArrayList<>();</font></div> <div><span class="Apple-tab-span" style="white-space:pre;"><font size="3"> </font></span></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>count=scan.nextInt();</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>for(int i=0; i<count; i++){</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>inputNation.add(scan.nextInt());</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>inputStudentNumber.add(scan.nextInt());</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>inputScore.add(scan.nextInt());</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>sample.add(new Olympiad(inputNation.get(i),</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>inputStudentNumber.get(i),inputScore.get(i)));</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</font></div> <div><span class="Apple-tab-span" style="white-space:pre;"><font size="3"> </font></span></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>Collections.sort(sample, new CompareScore());</font></div> <div><span class="Apple-tab-span" style="white-space:pre;"><font size="3"> </font></span></div> <div><font size="3"><span style="line-height:24px;white-space:pre;">//요기가 제가 질문한 부분이 되겠네요.</span></font></div> <div><font size="3"><span style="line-height:24px;white-space:pre;">//이 반복문에서는 정렬된 배열의 3번째 원소까지만 출력하게 되어있습니다.</span></font></div> <div><font size="3"><span style="line-height:24px;white-space:pre;">//그런데 클래스 안의 Nation이라는 원소가 2번이상 중복되게 되면 안되기 때문에</span></font></div> <div><font size="3"><span style="line-height:24px;white-space:pre;">//배열 안에 있는 클래스들의 Nation값을 비교해야만 합니다.</span></font></div> <div><font size="3"><span style="line-height:24px;white-space:pre;">//BUT!!! get()함수를 쓸 경우에 toString()값인 StudentNumber와 Score가 출력되기 때문에</span></font></div> <div><font size="3"><span style="line-height:24px;white-space:pre;">//Nation값에 접근조차 못하고 있습니다.</span></font></div> <div><font size="3"><span style="line-height:24px;white-space:pre;"><br></span></font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>for(int i=0;i<3;i++){</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>System.out.println(sample.get(i));</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</font></div> <div><font size="3">}</font></div> <div><br></div> <div><font size="3">class CompareScore implements Comparator<Object>{</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>public int compare(Object o1, Object o2){</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>int score1=((Olympiad)o1).Score;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>int score2=((Olympiad)o2).Score;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>if(score1>score2)</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>return -1;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>if(score1==score2)</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>return 0;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>return 1;</font></div> <div><font size="3"><span class="Apple-tab-span" style="white-space:pre;"> </span>}</font></div> <div><font size="3">}</font></div> <div><font size="3"><br></font></div> <div><font size="3"><br></font></div> <div><font size="3"><br></font></div> <div><font size="3">제가 주석달아놓은 부분을 봐주세요~</font></div> <div><font size="3">요점은 배열 안에 있는 클래스들의 Nation값을 비교하고 싶은것 입니다.</font></div> <div><br></div></div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.