简体   繁体   中英

Java Programming , Type Casting

I wrote a method in a class and I wanted to cast an int array div 26 to pass it to a frequency table that is double . Then that kind of error occurred . Can somebody help me please , I am just a student...?

Thanks

public void frequency(int[] count ,double [] Q){
        for(double arguments : Q){
            Q[arguments]=(double)(count[arguments]/26);
        }
    }



Encryption.java:35: error: possible loss of precision
            Q[arguments]=(double)(count[arguments]/26);
              ^
  required: int
  found:    double
Encryption.java:35: error: possible loss of precision
            Q[arguments]=(double)(count[arguments]/26);
                                        ^
  required: int
  found:    double

You are trying to access the array index with double value, which is incorrect, arrays will only contain indexes like 0,1,2, etc...

Also, you will not be able to use for each loop to iterate through index, so instead use normal for loop as shown below:

for(int i=0;i<count.length;i++){
        Q[i]=(double)(count[i]/26);
}

您使用double作为数组的索引,这是不可能的,因为索引绑定为int基本类型,这就是您收到编译错误的原因。

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