Possible loss of precision error with an array in a loop -


So I'm getting similar errors in some programs and I can not understand it. The array in the loop and all the variables are double, so I can not find why it expects an int value. Anyway here, all the mistakes are seen in the previous block for the statement:

  import java.util.Scanner; Public Class Exercise 610 {Public Stable Zero Main (String [] Args) {Smaller; Scanner stdIn = new scanner (System.in); Double array 1 [] = new double [100]; For (int i = 0; array1 [i]! = -7; i ++) {System.out.println ("Input to input input, or -7 to end"); Array 1 [i] = stdIn.nextDouble (); } Smallest = index of average element (array 1); System.out.println ("The smallest value in the array" + is small); } Public stable interaction of asymmetry element (double [] array) {int smallestInt; Double strawberry = array [0]; (Double u: array) {if (array [u + 1] & lt; array [u]) {smallest = array [u + 1]; }} SmallestInt = (int) the smallest; Return short return; }}  

File: / User / Krabien / Iitrix / Excerpts 610. Java [Line: 33]
Error: / User / Krabien / Iitrix / Accusis 610. Java : Possible loss of precision found: double
requirement: int file: / user / carbon / iTrocks / exchease 610.java [line: 33] error: / user / CRBN / introx / accuscie610. Java: 33 : Possible loss of precision found: found: double required: int :: File: /Users/Crbn/introcs/Exercise610.java [Pt Speed: 35]
Error: / User / CRBN /introcs/Exercise610.java35: Potential Loss of Exact
found: Double Required: int int *

You can not say array [u + 1] because it refers to an array indicator. Errors are because u is a double and there are numbers in the integer 0, 1, 2, etc. in any array ...

< P> You should loop through an array using an integer:

 for  (int i = 0; i & lt; some; i ++) {// do something here }  

If you want (code below) you can insert u to an int, but start with int < It is better to declare / code>.

 for  (double u: array) {if (array [(int) (u + 1)]  

The reason for the loss of the exact error is because a int lives in 32 bit memory, there were 64 bits in a double , So if you try and squeeze double in int you can lose some data


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -