1
public abstract class Main implements Comparable {
public static void main(String[] args) {
Integer[] intArray = {1,2,3,4,5,6,7,8,9,10};
String[] stringArray = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
java.util.Date[] dateArray = {};
for (int j = 0; j < 10 ; j++)
dateArray[j] = new java.util.Date();
/* Code to call max method and display the largest value of these */
}
public static Object max (Comparable[] a){
Object tempObj = new Object();
for (int i = 0; i < a.length - 1; i++){
if ((a[i]).compareTo(a[i+1]) > 0)
tempObj = a[i];
else
tempObj = a[i+1];
}
return tempObj;
}
public int compareTo(Object o) {
if (/*this*/ > o)
return 1;
else if (/*this*/ < o)
return -1;
else
return 0;
}
}
일반 max (a, b) 형식으로 작성하는 것이 더 쉬운 반면, 요구 사항 중 하나는 다음과 같이 작성해야합니다. 방법. 이 실제로 compareTo 메소드를 호출하는 객체의 값을 참조하는 방법을 찾을 수 없습니다.Comparable 인터페이스를 사용하여 문자열, 날짜, 정수의 최대 값을 반환하는 프로그램
와우. 이것은 내가 생각했던 것보다 더 간단했습니다. 다니엘 리앙 (Daniel Liang)의 자바 프로그래밍 입문 (Introduction to Java Programming) 제 8 호를 진심으로 싫어합니다. 설명이 없습니다. – MISMajorDeveloperAnyways
최고의 프로그래머는 게으른 프로그래머입니다. 문제에 대한 간단한 해결책을 찾으려고 노력합니다. –