How I can index the array starting from 1 instead of zero?

后端 未结 5 1004
滥情空心
滥情空心 2020-12-10 09:46
for (int i = 0; i < reports.length; i++) {

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

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

  }
}
相关标签:
5条回答
  • 2020-12-10 10:17

    You can use pointers, to jump to a certain point of the array and start the array from there.

    For example:

    char str[20];
    str={'H', 'E' ,'L' ,'L', 'O','W' ,'O ','R','L',' D'};
    char *ptr;
    *ptr=str[0];
    //right now its pointing to the starting.
    ptr=ptr+3;
    //Now pointing at 3rd unit.
    

    This doesn't work in every compiler.This is the closest thing that can be done for your question.

    0 讨论(0)
  • 2020-12-10 10:22

    You can't do that as array index in Java starts from 0. But you can access array with index 1 with little modifications.

    Example: Consider an integer array "a" with length n

    for(int i=0;i<n;i++) {
        System.out.println(a[i]);
    }
    

    This can be modified as:

    int a[] = new int[n+1];
    for(int i=1;i<n+1;i++) {
        System.out.println(a[i]);
    }
    
    0 讨论(0)
  • 2020-12-10 10:25

    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.

    0 讨论(0)
  • 2020-12-10 10:27

    Base Index of Java arrays is always 0. It cannot be changed to 1.

    0 讨论(0)
  • 2020-12-10 10:40

    Just like in most languages arrays are indexed from 0. You better get used to it, there is no workaround.

    0 讨论(0)
提交回复
热议问题