我如何从1开始而不是从零开始索引数组? [英] How I can index the array starting from 1 instead of zero?

查看:472
本文介绍了我如何从1开始而不是从零开始索引数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

($ i $ 0; i< reports.length; i ++){

Products [] products = reports [i] .getDecisions;

for(int j = 0; j< products.length; j ++){

}
}

这里我想从1开始索引内部for循环,但它没有按预期工作,我也改变了j

解决方案

Java数组总是从0开始。你无法改变这种行为。您可以从另一个索引填充或使用它,但不能更改基本索引。



它在JLS§10.4,如果您对此感兴趣。


数组的一个组件由一个数组访问表达式(第15.13节)访问,该表达式由一个表达式组成,该表达式的值是一个数组引用,后跟一个索引表达式by [and],如A [i]。



所有数组都是0原点。长度为n的数组可以用整数0到n-1索引。



for (int i = 0; i < reports.length; i++) {

  Products[] products = reports[i].getDecisions;

  for (int j = 0; j < products.length; j++) {

  }
}

Here I want to index the inner for loop starting from 1 , but it is not working as expected, I also changed the j

解决方案

Java arrays are always 0-based. You can't change that behavior. You can fill or use it from another index, but you can't change the base index.

It's defined in JLS §10.4, if you are interested in it.

A component of an array is accessed by an array access expression (§15.13) that consists of an expression whose value is an array reference followed by an indexing expression enclosed by [ and ], as in A[i].

All arrays are 0-origin. An array with length n can be indexed by the integers 0 to n-1.

这篇关于我如何从1开始而不是从零开始索引数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆