错误代码不兼容类型:可能有损转换从double到int

时间:2017-04-04 16:57:33

标签: java

我正在从我的教科书中的示例更改此代码以与双数组兼容。代码中有几个地方我不知道如何修复错误"不兼容的类型:可能有损转换从double到int"

temp = numberList[max];    
numberList[max] = numberList[numberList.length - i - 1];
numberList[numberList.length - i - 1] = temp;

if (numberList[i] > numberList[index] )

j = i;
temp = numberList[i];

那些显示错误"不兼容的类型:从double到int的可能有损转换。"

我也在第一次使用indexOfLargestElement时收到错误,"找不到符号。"

package week5;
import java.util.Arrays;
/**
 *
 * @author meowens
 */
public class Week5 {

/**
 * @param args the command line arguments
 */

public static void selectionSort( double [] numberList) {

    double temp; 
    double max;

    for (double i = 0; i < numberList.length - 1; i++)  
    {
        max = indexOfLargestElement( numberList, numberList.length - i);

        temp = numberList[max];
        numberList[max] = numberList[numberList.length - i - 1];
        numberList[numberList.length - i - 1] = temp;
    }
}
private static double indexOfLarggestElement( double [] numberList, double size )
{
    double index = 0;
    for ( double i = 1; i < size; i++)
    {
        if (numberList[i] > numberList[index] )
            index = i;
    }
    return index;
}
public static void insertionSort ( double [] numberList)
{
    int j, temp;

    for ( double i = 1; i < numberList.length; i++ )
    {
        j = i;
        temp = numberList[i];

        while ( j != 0 && numberList[j - 1] > temp )
        {
            numberList[j] = numberList[j - 1];
            j--;
        }

        numberList[j] = temp;
    }
    // TODO code application logic here



    // assigning values
    numberList[0] = 53.5;
    numberList[1] = 60.3;
    numberList[2] = 96.2;
    numberList[3] = 53.3;
    numberList[4] = 56.4;
    numberList[5] = 52.7;
    numberList[6] = 76.4;
    numberList[7] = 77.5;
    numberList[8] = 71.0;
    numberList[9] = 78.2;

    numberList[10] = 65.2;
    numberList[11] = 59.3;
    numberList[12] = 80.5;
    numberList[13] = 92.1;
    numberList[14] = 85.7;
    numberList[15] = 78.7;
    numberList[16] = 66.2;
    numberList[17] = 88.8;
    numberList[18] = 50.2;
    numberList[19] = 73.4;

    }

}

我确信还有其他一些简单的错误我还没有找到,因为我还没有机会调试和校对代码,而且我对Java和编程也很陌生。

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

onKeyDown

您不能使用除for ( double i = 1; i < numberList.length; i++ ) byteshort之外的任何其他类型来索引数组,因此存在问题。 int值也可以用作索引值,因为它们受到一元数字提升并成为char值,但是看到int值用作索引器非常罕见。

确保将char类型变量更改为double类型变量,只要您使用int类型变量来索引数组。

Java文档

  

数组必须用int值索引; short,byte或char值可以   也可以用作索引值,因为它们是一元的   数字促销(§5.6.1)并成为int值。

     

尝试访问具有长索引值的数组组件   导致编译时错误。

答案 1 :(得分:0)

这里的一般问题是您在几个地方混合了doubleint值。您的数组类型为double[]。因此,无论何时访问它的元素,索引都应为int类型,并且它将访问类型double的值。我改变了循环和临时变量。

名为indexOfLarggestElement的方法也有一个拼写错误,我改为indexOfLargestElement

package week5;

import java.util.Arrays;

/**
 *
 * @author meowens
 */
public class Week5 {

    /**
    * @param args the command line arguments
    */

    public static void selectionSort( double [] numberList)
    {
        double temp; 
        int max;

        for (int i = 0; i < numberList.length - 1; i++)  
        {
            max = indexOfLargestElement( numberList, numberList.length - i);

            temp = numberList[max];
            numberList[max] = numberList[numberList.length - i - 1];
            numberList[numberList.length - i - 1] = temp;
        }
    }

    private static int indexOfLargestElement( double [] numberList, double size )
    {
        int index = 0;
        for ( int i = 1; i < size; i++)
        {
            if (numberList[i] > numberList[index] )
                index = i;
        }
        return index;
    }

    public static void insertionSort ( double [] numberList)
    {
        int j;
        double temp;

        for ( int i = 1; i < numberList.length; i++ )
        {
            j = i;
            temp = numberList[i];

            while ( j != 0 && numberList[j - 1] > temp )
            {
                numberList[j] = numberList[j - 1];
                j--;
            }

            numberList[j] = temp;
        }
        // TODO code application logic here

        // assigning values
        numberList[0] = 53.5;
        numberList[1] = 60.3;
        numberList[2] = 96.2;
        numberList[3] = 53.3;
        numberList[4] = 56.4;
        numberList[5] = 52.7;
        numberList[6] = 76.4;
        numberList[7] = 77.5;
        numberList[8] = 71.0;
        numberList[9] = 78.2;

        numberList[10] = 65.2;
        numberList[11] = 59.3;
        numberList[12] = 80.5;
        numberList[13] = 92.1;
        numberList[14] = 85.7;
        numberList[15] = 78.7;
        numberList[16] = 66.2;
        numberList[17] = 88.8;
        numberList[18] = 50.2;
        numberList[19] = 73.4;
    }
}