如何制作一个具有 1-3 int 而不是默认的 0-3 int 的数组?喜欢
int[] anArray;
anArray = new int[3];
has numbers 0-3 I want index starting at 1
如何制作一个具有 1-3 int 而不是默认的 0-3 int 的数组?喜欢
int[] anArray;
anArray = new int[3];
has numbers 0-3 I want index starting at 1
You make an array of four elements, and then never use the '0' element.
Sorry, but you can't change the language.
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.
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
Java 不允许这样做。在大多数编程语言中,数组索引从零开始。在它们从一开始的情况下,您无法更改下限。
我能想到的唯一可以同时指定数组下限和上限的主流1语言是 Ada。(以及最新版本的 Fortran 也...)
1 ...并且这些天将Ada称为主流是“牵强”。