简体   繁体   中英

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
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:

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. 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; 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"?!?

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; or use a translation table from keys to indexes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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