java 你能用 long int 索引一个数组吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27333879/
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-11-02 11:38:16  来源:igfitidea点击:

Can you index an array with a long int?

javaarrays

提问by Martin Plávek

Is it somehow possible to use a long int to index an array? Or is this not allowed?

是否可以使用 long int 来索引数组?或者这是不允许的?

What I mean is bellow in the code.

我的意思是在代码中。

long x = 20;
char[] array = new char[x];

or

或者

long x = 5;
char res;
res = array[x];

采纳答案by Joel

If you look at the Java Documentationin 10.4:

如果您查看10.4中的Java 文档

Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (§5.6.1) and become int values.

An attempt to access an array component with a long index value results in a compile-time error.

数组必须由 int 值索引;short、byte 或 char 值也可以用作索引值,因为它们受到一元数字提升(第 5.6.1 节)并成为 int 值。

尝试访问具有长索引值的数组组件会导致编译时错误。

The error you would get would look something like this:

你会得到的错误看起来像这样:

test.java:12: possible loss of precision
found   : long
required: int
        System.out.println(array[index]);
                                 ^
1 error

If for some reason you have an index stored in a long, just cast it to an int and then index your array. You cannot create an array large enough so it cannot be indexed by an integer in Java. So there is no need for long integers here.

如果由于某种原因您将索引存储在 long 中,只需将其转换为 int,然后索引您的数组。你不能创建一个足够大的数组,所以它不能在 Java 中被整数索引。所以这里不需要长整数。

回答by August

No, it's not possible. JLS 15.10states that the expression in an array initializer must be promoted to an int:

不,这不可能。JLS 15.10声明必须将数组初始值设定项中的表达式提升为int

Each dimension expression undergoes unary numeric promotion (§5.6.1). The promoted type must be int, or a compile-time error occurs.

每个维度表达式都经过一元数字提升(第 5.6.1 节)。提升的类型必须是 int,否则会发生编译时错误。

Same thing with applies to array access expressions (JLS 15.13):

同样适用于数组访问表达式(JLS 15.13):

The index expression undergoes unary numeric promotion (§5.6.1). The promoted type must be int, or a compile-time error occurs.

索引表达式经过一元数字提升(第 5.6.1 节)。提升的类型必须是 int,否则会发生编译时错误。

If you want to use a long, you'll have to cast it to intfirst:

如果要使用 a long,则必须int先将其转换为:

char[] array = new char[(int) x];
res = array[(int) x];

回答by m3th0dman

Technically you can have such a structure using Unsafeclass. with it you can allocate as much memory as you want (as you have actually). To be noted that this is native memory and not heap memory. Because of this, there are downsides as compared to typical arrays, though: the memory isn't garbage collected (you'll need to manually deallocate memory) and there aren't bound checks so without being careful you can have seg fault and crash your JVM.

从技术上讲,您可以使用Unsafe类拥有这样的结构。有了它,您可以根据需要分配尽可能多的内存(实际上是这样)。需要注意的是,这是本机内存而不是堆内存。因此,与典型数组相比,存在缺点:内存不是垃圾收集的(您需要手动释放内存)并且没有绑定检查,因此如果不小心,您可能会遇到段错误和崩溃你的JVM。

See example here, at Big Array section: http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/

请参阅此处的示例,在 Big Array 部分:http: //mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/

Also there are rumors that future versions of JVM and the language will have support arrays of size long.

还有传言说 JVM 和该语言的未来版本将支持 size 数组long