2014-09-08 4 views
0

호출 가능 함수를 통해 함수에 매개 변수가되는 함수를 얻으려고합니다. 당신이 그것을 실행하려고하면, 오류 '무효'타입이 허용되지 않는다고 말할 것입니다. 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로 전달되는 데 다른 방법으로 도움이 될 수 있다면, 그것은 좋은 것입니다. 그리고 매우 감사합니다.

+4

당신은'Sort' 클래스에 대한 코드를 게시 할 수 있습니까? –

+3

나는'sort.Insertion (A)'이'Callable' 객체를 반환하지 않는다고 의심합니다. – alfasin

+0

@JohnKugelman 거기에 – anobilisgorse

답변

1

Sort.Insertion()에는 void 반환 유형이 있습니다라는 오류 메시지가 표시됩니다. 당신은 타이머를 호출하려는 경우

, 당신은 그렇게 (검증되지 않은)처럼 Callable에 포장해야합니다

long a = timer(new Callable<Void>() { 
     Void call() { 
      sort.Insertion(A); 
      return null; 
     } 
    }); 

Afinal을 만들 수 필요합니다. 적절한 경우 Void을 변경하십시오.

+0

Insertion Sort가 정수 배열이라고 가정하면, 'Void'에 무엇을 넣어야합니까? – anobilisgorse

+0

이런, 고마워! :) – anobilisgorse