在java中声明long[]数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20175272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:44:24  来源:igfitidea点击:

declaring long[] array in java

javaarrayslong-integer

提问by Yetti

Can anyone tell me why cant i declare array like this?

谁能告诉我为什么我不能像这样声明数组?

long[] powers = { 0, 0, 1, 7, 35, 155, 651, 2667, 10795, 43435,
                174251, 698027, 2794155, 11180715, 44731051, 178940587,
                715795115, 2863245995, 11453115051, 45812722347, 183251413675,
                733006703275, 2932028910251, 11728119835307, 46912487729835,
                187649967696555, 750599904340651, 3002399684471467};

Compiler says that the literal of type int is out of range. I also tried to to cast it to long like this

编译器说 int 类型的文字超出范围。我也试过像这样把它投射到很长的时间

long[] powers = { 0, 0, 1, 7, 35, 155, 651, 2667, 10795, 43435,
                174251, 698027, 2794155, 11180715, 44731051, 178940587,
                715795115, (long)2863245995, (long)11453115051, (long)45812722347, etc ...

but nothing changed also tried someting like this Long.valueOf(x)where x is number whitch compiler has problem with.

但没有任何改变也尝试过这样的事情Long.valueOf(x),其中 x 是编译器有问题的数字。

Any ideas?

有任何想法吗?

Thanks in advance

提前致谢

采纳答案by Masudul

Plain number is considered as intin java. Append Lwhich are bigger than Integer.MAX_VALUEto convert long.

纯数字被认为是int在java中。附加L大于Integer.MAX_VALUE转换的long

long[] powers = {..., 2863245995L, 11453115051L, ...};

According to docs

根据文档

An integer literal is of type longif it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int.

The suffix L is preferred, because the letter l (ell) is often hard to distinguish from the digit 1 (one).

如果一个整数字面量long以 ASCII 字母 L 或 l (ell) 为后缀,则它属于类型;否则它是类型int

后缀 L 是首选,因为字母 l (ell) 通常很难与数字 1 (one) 区分开来。

回答by quasit

Have you tried something like this?

你有没有尝试过这样的事情?

long[] powers = { 0, 0, 1, 7, 35, 155, 651, 2667, 10795, 43435,
            174251, 698027, 2794155, 11180715, 44731051, 178940587,
            715795115L, 2863245995L, 11453115051L, 45812722347L, 183251413675L,
            733006703275L, 2932028910251L, 11728119835307L, 46912487729835L,
            187649967696555L, 750599904340651L, 3002399684471467L};