-7

如何制作一个具有 1-3 int 而不是默认的 0-3 int 的数组?喜欢

int[] anArray;
anArray = new int[3];
has numbers 0-3 I want index starting at 1
4

4 回答 4

3

You make an array of four elements, and then never use the '0' element.

Sorry, but you can't change the language.

于 2013-07-20T00:34:52.117 回答
0

You don't, arrays in Java start their indices at 0.

And new int[3] does not have numbers 0-3, it has indices 0-2.

于 2013-07-20T00:35:18.097 回答
0

The index starts at 0. You'll just need to set The variable different in your loops if it helps readability

Instead of for I=0; i

You'd use

For I= 1; I

Would recommend you get use to indexing from zero though.

Apologies for typos. Using phone

于 2013-07-20T00:36:58.903 回答
0

Java 不允许这样做。在大多数编程语言中,数组索引从零开始。在它们从一开始的情况下,您无法更改下限。

我能想到的唯一可以同时指定数组下限和上限的主流1语言是 Ada。(以及最新版本的 Fortran 也...)


1 ...并且这些天将Ada称为主流是“牵强”。

于 2013-07-20T02:53:21.200 回答