Issues with creating a double and an int, Swap + Linear Search method in java using arrays

时间:2022-02-22 01:02:00

I'm new to java and as an introductory assignment on methods, a teacher asked us to create a series of methods involving arrays. He wants a "double" and an "int" for each method. I am struggling getting my linear search method to work because there seems to be an error with my swap method. I am consistantly getting errors on converting my methods from double to int. (I deleted portions of the code that is working ok, so some formatting might be off, I apologize) Any assistance would be greatly appreciated! (knowing java it's probably an obvious mistake)

我是java新手,作为方法的入门作业,老师要求我们创建一系列涉及数组的方法。他希望每种方法都有“双”和“整数”。我正在努力让我的线性搜索方法起作用,因为我的交换方法似乎有错误。我将我的方法从double转换为int时遇到了错误。 (我删除了部分正常工作的代码,因此某些格式可能会关闭,我道歉)任何帮助将不胜感激! (知道java它可能是一个明显的错误)

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 个解决方案

#1


0  

You can't convert a double to an int as soon as a double is a floating point data type. You're tring to do int = double which is nonsense.

只要double是浮点数据类型,就不能将double转换为int。你要做int = double这是无稽之谈。

Here is the prototypes of your function : public static void swap(double[] arr, int loc1, int loc2) and the error says you're doing swap(double[], int, double).

这是你的函数的原型:public static void swap(double [] arr,int loc1,int loc2),错误说你正在做swap(double [],int,double)。

#1


0  

You can't convert a double to an int as soon as a double is a floating point data type. You're tring to do int = double which is nonsense.

只要double是浮点数据类型,就不能将double转换为int。你要做int = double这是无稽之谈。

Here is the prototypes of your function : public static void swap(double[] arr, int loc1, int loc2) and the error says you're doing swap(double[], int, double).

这是你的函数的原型:public static void swap(double [] arr,int loc1,int loc2),错误说你正在做swap(double [],int,double)。