简体   繁体   English

可能的精度损失要求int发现双精度数组

[英]possible loss of precision required int found double array

I have read a file and require the variables to be doubles as they are decimals. 我已读取文件,并要求变量为双精度数,因为它们是小数。 So have done this: 因此,请执行以下操作:

String[] data = list.split("\\t");

        homes = Double.parseDouble(data[0]);
        rate = Double.parseDouble(data[1]);

I am now trying to put the data in an array but am getting the following error: 我现在正尝试将数据放入数组中,但出现以下错误:

possible loss of precision 可能会损失精度
required: int 要求:int
found: double array 找到:双数组

I need the variables homes and rate to be in an array in order to do a calculation and need them to be doubles as rate is a percentage in decimal: 我需要将变量homes和rate放在数组中以便进行计算,并且需要将它们加倍,因为rate是十进制的百分比:

private boolean [][] place = new boolean [homes][rate];

How can I stop this error? 如何停止此错误?

You get the error because you need to initialize your array using Integer sizes, not double s. 之所以会出现错误,是因为您需要使用Integer大小而不是double s初始化数组。 So you need to convert the doubles to integers. 因此,您需要将双精度数转换为整数。

Change 更改

private boolean [][] place = new boolean [homes][rate];

to

private boolean [][] place = new boolean [(int)homes][(int)rate];

arrays by definition are indexed using integer positions . 根据定义, 数组使用整数位置进行索引。 There is no slot number 9 3/4; 没有插槽号9 3/4; that only exists in Harry Potter. 那只存在于哈利·波特中。 You literally have to think of an array to look like this: {true, false, true} - what is the value at "1.75"?!? 您实际上必须考虑一个看起来像这样的数组: {true, false, true} -“ 1.75”的值是多少?

If you want to "index" using arbitrary values, you may be looking for a dictionary , aka a java.collections.Map which maps keys (like 9.75) to values; 如果要使用任意值“索引”,则可能正在寻找一个字典 ,又名java.collections.Map ,它将键(如9.75)映射到值; or use a translation table from keys to indexes. 或使用从键到索引的转换表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM