【发布时间】:2017-03-21 17:15:34
【问题描述】:
我要做的是初始化布尔数组,数组的大小是长值。
public static List<Integer> primesUpTo(long target) {
boolean[] nonPrime = new boolean[target];
}
我收到以下错误:
possible loss of precision
boolean[] nonPrime = new boolean[target];
^
required: int
found: long
有人可以解释一下为什么我无法使用长值初始化布尔数组,而且我无法增加长值说:boolean[] nonPrime = new boolean[target+1] 也不起作用。谢谢提前.
【问题讨论】:
-
读取错误 - 数组只能使用 int 值声明。如果您可以保证
target将适合int然后将其转换。否则你可能会耗尽内存。 -
数组总是用
int初始化。您不能使数组大于Integer.MAX_VALUE。
标签: java arrays oop boolean long-integer