一些消息已被简化;用-Xdiags重新编译:详细以获得完整输出?

时间:2018-05-07 00:37:57

标签: java arrays compiler-errors selection-sort

所以我正在编写一个选择排序算法,将排序的所有部分转换为方法。但是,当我编译它时,它返回错误:

注意:有些消息已经简化;使用-Xdiags重新编译:详细以获得完整输出

我做错了什么?

import java.io.*;
    public class Driver01
   {
       public static void main(String[] args)
      {
        //input
         double[] myArray = {2.0, 3.7, 9.9, 8.1, 8.5, 7.4, 1.0, 6.2};

        //sort the array
         double[] sorted = process(myArray);

        //output
         System.out.print("" + sorted);



      }
      private static double findMax( int EndIndex, double[] enterArray) {
         double max = 0;
         int trueIndex = 0; 
         for( int x = 0; x < EndIndex; x++) {
               if(enterArray[x] > max) {
                  max = enterArray[x];
                  trueIndex = x;
               }
               }

      return trueIndex;
      }
      private static void swap(int swap1, int swap2, double[] enterArray) {
         double temp = 0;
         temp = enterArray[swap1];
         enterArray[swap1] = enterArray[swap2];
         enterArray[swap2] = enterArray[swap1];

      }
      private static double[] process(double[] enterArray) {
         int range = enterArray.length -1;
         for( int x = 0; x < enterArray.length-1; x++) {
            double j = findMax(range, enterArray);
            swap(j, range, enterArray);
            range = range -1;
         }
         return enterArray;
      }
      }

修订代码:

import java.io.*;
    public class Driver01
   {
       public static void main(String[] args)
      {
        //input
         double[] myArray = {2.0, 3.7, 9.9, 8.1, 8.5, 7.4, 1.0, 6.2};

        //sort the array
         double[] sorted = process(myArray);

        //output
         System.out.print("" + sorted);



      }
      private static int findMax( int EndIndex, double[] enterArray) {
         double max = 0;
         int trueIndex = 0; 
         for( int x = 0; x < EndIndex; x++) {
               if(enterArray[x] > max) {
                  max = enterArray[x];
                  trueIndex = x;
               }
               }

      return trueIndex;
      }
      private static void swap(int swap1, int swap2, double[] enterArray) {
         double temp = 0;
         temp = enterArray[swap1];
         enterArray[swap1] = enterArray[swap2];
         enterArray[swap2] = enterArray[swap1];

      }
      private static double[] process(double[] enterArray) {
         int range = enterArray.length -1;
         for( int x = 0; x < enterArray.length-1; x++) {
            int j = findMax(range, enterArray);
            swap(j, range, enterArray);
            range = range -1;
         }
         return enterArray;
      }
      }

现在代码正在打印一个如下所示的数组:[D @ 15db9742 现在怎么了?

1 个答案:

答案 0 :(得分:0)

完整的错误消息是:

Driver01.java:41: error: incompatible types: possible lossy conversion
                         from double to int
            swap(j, range, enterArray);
                 ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose
      to get full output
1 error

从这一点来看,很明显,当你期望swap参数时,你正在使用double参数调用int

现在我不打算提出解决方案,但我建议你仔细看看findMax正在做什么以及它正在返回什么。特别是返回类型

(这里的部分问题是findMax变量的名称具有误导性。该方法并不像名称所暗示的那样。)