从double到int的可能有损转换

为什么我得到Possible lossy conversion from double to int错误,我该如何解决?

 public class BinSearch { public static void main(String [] args) { double set[] = {-3,10,5,24,45.3,10.5}; double l = set.length; double i, j, first, temp; System.out.print("Before it can be searched, this set of numbers must be sorted: "); for (i = l-1; i>0; i--) { first=0; for(j=1; j<=i; j++) { if(set[j] < set[first]) // location of error according to compiler { first = j; } temp = set[first]; set[first] = set[i]; set[i] = temp; } } } } 

正如你所看到的,我已经尝试在声明变量时尝试用top替换顶部的int ,但它似乎没有完成任务。

将用作数组索引的所有变量从double更改为int(即变量jfirsti )。 数组索引是整数。

数组/循环索引应该是整数,而不是双精度数。

例如,当试图访问set[j] ,它抱怨将j视为int。

更改变量类型如下。 数组索引必须是int类型。

 public class BinSearch { public static void main(String [] args) { double set[] = {-3,10,5,24,45.3,10.5}; int l = set.length; double temp; int i, j, first; System.out.print("Before it can be searched, this set of numbers must be sorted: "); for ( i = l-1; i>0; i--) { first=0; for(j=1; j<=i; j++) { if(set[j] < set[first])//location of error according to compiler { first = j; } temp = set[first]; set[first] = set[i]; set[i] = temp; } } } }