无法在java中使用长值初始化布尔数组?
我要做的是初始化布尔数组,数组的大小是长值无法在java中使用长值初始化布尔数组?,java,arrays,oop,boolean,long-integer,Java,Arrays,Oop,Boolean,Long Integer,我要做的是初始化布尔数组,数组的大小是长值 public static List<Integer> primesUpTo(long target) { boolean[] nonPrime = new boolean[target]; } 有人能解释一下为什么我不能使用长值初始化布尔数组,也不能增加长值,比如:Boolean[]nonPrime=new Boolean[target+1]也不工作。谢谢你的帮助。这是语言限制 Java因不支持超过2^31的数组
public static List<Integer> primesUpTo(long target) {
boolean[] nonPrime = new boolean[target];
}
有人能解释一下为什么我不能使用长值初始化布尔数组,也不能增加长值,比如:Boolean[]nonPrime=new Boolean[target+1]也不工作。谢谢你的帮助。这是语言限制 Java因不支持超过2^31的数组而受到批评−1约21亿元素。[17][18][19]这是语言的局限性;Java语言规范第10.4节规定: 数组必须由int值索引。。。试图访问具有长索引值的数组组件会导致编译时错误。[20]
在此处找到答案:。读取错误-只能使用int值声明数组。如果你能保证这个目标会适合整数,那么就施放它。否则,您可能会耗尽内存。数组总是用int初始化。您不能使数组大于Integer.MAX_值。感谢共享信息。我感谢你提供信息的方式。我想知道人们给否定的分数,而不是试图回答这个问题。
possible loss of precision
boolean[] nonPrime = new boolean[target];
^
required: int
found: long