호출 가능 함수를 통해 함수에 매개 변수가되는 함수를 얻으려고합니다. 당신이 그것을 실행하려고하면, 오류 '무효'타입이 허용되지 않는다고 말할 것입니다. at a = timer (sort.Insertion (A));여기서 'void'유형은 허용되지 않습니다. 호출 가능 함수
이유는 무엇입니까?
import java.util.*;
import java.util.concurrent.*;
import java.io.*;
public class ThisClass {
public static Sort sort = new Sort();
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("location/here.txt"));
int z = 0;
while(scanner.hasNextInt()) {
z++;
}
int[] A = new int[z];
for(int i = 0; i < A.length; i++) {
A[i] = scanner.nextInt();
System.out.print(" " + A[i]);
}
long a = timer(sort.Insertion(A));
}
public long timer (Callable func) throws Exception {
long start = System.currentTimeMillis();
func.call();
long end = System.currentTimeMillis();
return end - start;
}
public static void print (int[] A) {
for(int i = 0; i < A.length; i++) {
System.out.print(" " + A[i]);
}
}
}
import java.util.ArrayList;
public class Sort {
public void Insertion (int[] A) {
for(int j = 1; j < A.length; j++) {
int key = A[j];
int i = j - 1;
while((i >= 0) && (A[i] > key)) {
A[i + 1] = A[i];
i = i - 1;
}
A[i + 1] = key;
}
}
}
PS. 사람이 단지 자바에서 높은 정렬 된 기능을 가진, 또는 기능을 기능에 PARAMATERS로 전달되는 데 다른 방법으로 도움이 될 수 있다면, 그것은 좋은 것입니다. 그리고 매우 감사합니다.
당신은'Sort' 클래스에 대한 코드를 게시 할 수 있습니까? –
나는'sort.Insertion (A)'이'Callable' 객체를 반환하지 않는다고 의심합니다. – alfasin
@JohnKugelman 거기에 – anobilisgorse