2017-10-26 1 views
0

저는 Java에 익숙하지 않고 메서드에 대한 소개를 위해 선생님이 배열과 관련된 일련의 메서드를 만들 것을 요청했습니다. 그는 각 방법에 대해 "double"과 "int"를 원합니다. 스왑 방식에 오류가있는 것 같아서 선형 검색 방법을 사용하는 데 어려움을 겪고 있습니다. 내 메소드를 double에서 int로 변환 할 때 일관되게 오류가 발생합니다. (일부 코드는 삭제되었으므로 일부 서식이 삭제 될 수 있으므로 삭제하는 부분을 삭제했습니다.) 모든 도움을 주시면 대단히 감사하겠습니다! 더블이 부동 소수점 데이터 유형 같이배열을 사용하여 Java에서 Double 및 Int, Swap + Linear Search 메서드를 생성 할 때의 문제

ArrayUtils2.java:47: error: incompatible types: possible lossy conversion from double to int 
     int temp = arr[loc1]; 
        ^
ArrayUtils2.java:93: error: no suitable method found for swap(double[],int,double) 
     swap(arr,ix,small); 

public class ArrayUtils2 { 
    public static void main(String[] args) { 
     int[] nums = {1,7,5,3,9}; 
     displayArray(nums); 
     System.out.println(); 

     displayArray(nums); 



    public static void swap(int[] arr, int loc1, int loc2){ 
     int temp = arr[loc1]; 
     arr[loc1] = arr[loc2]; 
     arr[loc2] = temp; 

     System.out.println(loc2); 

    } 

    public static void swap(double[] arr, int loc1, int loc2){ 
     double temp = arr[loc1]; 
     arr[loc1] = arr[loc2]; 
     arr[loc2] = temp; 

    } 


    private static int selectSmallestIndex(int[] arr, int Start){  
     int smallestValue = arr[Start]; 

     for (int ix = Start; ix < arr.length; ix++){ 
     if (arr[ix] < smallestValue) { 
      smallestValue = arr[ix]; 

     } 

     } return smallestValue; 

    } 

    private static double selectSmallestIndex(double[] arr, int Start){  
     double smallestValue = arr[Start]; 

     for (int ix = Start; ix < arr.length; ix++){ 
     if (arr[ix] < smallestValue) { 
      smallestValue = arr[ix]; 

     } 

     } return smallestValue; 

    } 


    public static void selectionSort(int[] arr){      
     for(int ix = 0; ix < arr.length; ix++){     
     int small = selectSmallestIndex(arr,ix);     
     swap(arr,ix,small); 

     } 

    } 

    public static void selectionSort(double[] arr){      
     for(int ix = 0; ix < arr.length; ix++){     
     double small = selectSmallestIndex(arr,ix);     
     swap(arr,ix,small); 

     } 

    } 


    public static int linearSearch(int[] arr, int num){ 
     for (int ix = 0; ix < arr.length; ix++){ 
     if(arr[ix] == num){ 
      return ix; 

     } 

     }return -1; 

    } 

     public static double linearSearch(double[] arr, int num){ 
     for (int ix = 0; ix < arr.length; ix++){ 
      if(arr[ix] == num){ 
      return ix; 

     } 

     }return -1; 

    } 

} 
+1

메인 메서드에서 이러한 메서드를 호출하는 방법을 보여줍니다. –

+0

할당시 주 메서드를 제출할 필요가 없습니다. 어쨌든, 내가 사용하고있는 main 메소드가 이제 코드에 추가되었습니다. – andy2780

답변

0

당신은 즉시 doubleint에 변환 할 수 없습니다 (아마 명백한 실수 자바를 알고). 너는 말도 안되는 int = double을해야한다.

다음은 함수의 프로토 타입입니다. public static void swap(double[] arr, int loc1, int loc2)이고 오류 메시지는 swap(double[], int, double)입니다.

관련 문제