当前位置: 首页 > 知识库问答 >
问题:

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

慕学海
2023-03-14

我正在修改这段代码的过程中,从一个例子从我的教科书,以兼容一个双数组。代码中有几个地方我不确定如何修复错误“不兼容类型:从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];
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和编程也很陌生。

谢谢你的帮助!

共有1个答案

高琛
2023-03-14

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

名为IndexofLargestElement的方法也有一个键入,我将其改为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;
    }
}
 类似资料: