可能有损转换从long到int,JAVA [英] Possible lossy conversion from long to int, JAVA
问题描述
我正在尝试将long值传递给该方法并在那里使用它来创建一个Long Array。但是,
我在创建数组时遇到从long到int可能有损转换错误
I am trying to pass long value to the method and use it there to create a Long Array. However, I am getting "possible lossy conversion from long to int" error while creating an array
long[] array = new long[n];
虽然我没有使用任何整数值。
although I have not used any integers values.
import java.util.Scanner;
import java.util.ArrayList;
public class test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
long n = input.nextLong();
System.out. although("result is " + n + " is " + testing(n));
}
private static long testing(long n){
long[] array = new long[n];
return 0;
}
}
推荐答案
数组维度只能是 int
类型。编译器期望该类型,但您传入的是 long
类型。您可以将传入的参数类型更改为 int
并进行相应的更改。
Array dimensions can only be int
types. The compiler is expecting that type but you are passing in a long
type. You could change the argument type being passed in to an int
and the make the corresponding changes.
为了完整性,以下是 JLS 中关于变量的说明数组
For completeness, here is what the JLS says about variables in an array
它们由使用非负整数索引值的数组访问表达式引用。
they are referenced by array access expressions that use non-negative integer index values.
这篇关于可能有损转换从long到int,JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!