6

我有点困惑什么时候我真的需要使用那个长度-1。我知道那是我们不想出现越界错误的时候。例如我写了这个简单的数组:

    int [] oldArray = {1, 5, 6, 10, 25, 17};

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

它没有给我任何错误。-1实际上有用的任何例子?先感谢您。

4

11 回答 11

7

您通常希望oldArray.length在 for 循环调用中使用,因为在您的示例中,

for(int i = 0; i < oldArray.length; i++) {
    //Executes from i = 0 to i = oldArray.length - 1 (inclusive)
}

请注意如何i0up 到oldArray.length - 1,但准确地停止在oldArray.length(并且不执行)。由于数组从 position0而不是开始1, old.Array.length 非常适合i应该停止的数字。如果数组从 position 开始1,for 循环看起来像这样:

for(int i = 1; i <= oldArray.length; i++) {
    //Executes from i = 1 to i = oldArray.length (inclusive)
}

oldArray.length - 1通常是访问最后一个元素:

int[] oldArray = {1,2,3,4,5};
int lastElement = oldArray.length - 1; // 4
oldArray[lastElement] // returns last element, 5

尽管这通常是您使用length - 1vs的时候length,但在许多其他情况下,您也希望其中一个胜过另一个,因此没有真正的具体答案。但别担心,继续编码,你很快就会掌握这个窍门;)

于 2013-10-21T03:59:59.267 回答
5
for(int i = 0; i < oldArray.length; i++)

相当于

for(int i = 0; i <= oldArray.length - 1; i++)
于 2013-10-21T04:01:25.640 回答
3

循环和数组实用方法可以array.length用来表示“直到最后一个元素”。

我能想到的唯一使用的地方array.length - 1是访问最后一个元素时,例如在数组中查找最大数:

int[] array;
Arrays.sort(array);
int largest = array[array.length - 1];
于 2013-10-21T03:59:28.440 回答
1

这完全取决于你想做什么。在数组中,您有一组东西。如果你看到这个数组 [1,2,3,4],比如说,在小学,有人问你里面有多少东西,你会说四个。你会从一开始数,这是完全合理的。如果有人问你那个数组中的第三个东西是什么,你会指向 3。太棒了!除了,现在您正在编码,您知道数组从零开始。因此,即使该数组中仍然有四个东西,它们的名称也发生了变化。现在1的名字是0,2的名字是1,3的名字是2,4的名字是3。因此,当您使用循环并且只希望循环遍历所有内容时(例如,如果您正在搜索数组),您可以使用 .length 并且它会一直运行。但是,如果您想专门访问数组中的数字 4,则 4 的名称不再是 4,而是 3!所以你使用 length-1 因为当涉及到索引时,我们使用的是物理事物的新名称。

于 2016-09-04T07:44:57.353 回答
0

当您想要获取数组的最后一个索引号时使用它。看到计算机知道您的阵列有多长,它通过使用 -1 得到指示。看到 0 是开始,然后 -1 是数组的结束。

当我想像 Bohemian 写的例子一样对数字进行排序时,我会使用它。看一下冒泡排序算法

{
   int[] arr= new int[5]{23,2,3,34,6}; // unsorted data set
   bubblesort(arr,5); // sorting process using bubble sort
   int i;
  for(i=0;i<5;i++)
      Console.Write(arr[i]); //after sorting
  Console.ReadLine();

}

//冒泡排序

static void bubblesort(int[] dataset, int n){
   int i,j;
   for(i=0;i<n;i++)
     for(j=n-1;j>i;j--)
        if(dataset[j]<dataset[j-1])
       {
          int temp=dataset[j];
          dataset[j]=dataset[j-1];
          dataset[j-1]=temp;
        }

     }
   }
}
于 2015-04-09T16:14:42.297 回答
0
int [] oldArray = {1, 5, 6, 10, 25, 17};

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

=>

int [] oldArray = {1, 5, 6, 10, 25, 17};

for(int i = 0; i <= oldArray.length - 1; i++){

所以基本上,你想oldArray.length - 1在需要访问最后一个元素时使用。

于 2013-10-21T03:54:08.067 回答
0

你的问题没有具体的答案。这取决于。有一点是当您想要索引数组的最后一个元素时

int lastElement = oldArray[oldArray.length-1];
于 2013-10-21T03:54:32.227 回答
0

简单地说,虽然你的数组有 6 个元素,但数组的索引是从 0 开始的。这意味着第一个元素的索引是 0,第二个元素是 1,...,第 6 个元素是 5。因此,如果你尝试访问oldArray[6],你会得到一个错误。在您演示的循环中,情况并非如此:

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

oldArray.length返回 6。循环中达到的最大值i为 5,因为当 的值i变为 6 时循环将中断。这就是没有错误的原因。因此,要直接回答您的问题,oldArray[oldArray.length - 1]将返回数组中的最后一个元素。

于 2013-10-21T03:57:30.087 回答
0

你也可以这样写:

for(int i = 0; i <= oldArray.length-1; i++){

oldArray.length-1当您想要特定元素时需要,即数组中的最终元素

IEoldArray[oldArray.length-1]

于 2013-10-21T03:57:58.990 回答
0

The main rule is to remember is the Array is starts with 0. While .length is returns the actual number of array, so length do not count or start with 0

So, as per your example:

  int [] oldArray = {1, 5, 6, 10, 25, 17};

Here in your example below

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

The i starts with 0 and loop continue till i value as 5 as you are using < only.

If you try to use <= it will fail again, as you try to assess 7th(0-6) element which is not present.

Now take a another example as below, where we try to reverse a string:

    String name="automation";
    
    System.out.println(name.length());
    
    for(int i=name.length()-1;i>=0;i--) {
        System.out.print(name.charAt(i));
    }

Output:

10

noitamotua

Now in above example if you don't use -1 the value 10 will be pass inside the loop, as the length return 10 as per length of string but for array 10th element not present as array starts with 0 so it require till 9th place only as per above example , if you pass 10 it will throw error as 10th position is not present for it, thats why we need to use -1 at least for decrements(i--) order for sure

于 2020-02-02T21:17:06.747 回答
0
for (int i = 0; i < array.length; i++) {
    // this will loop throw the entire array
 }

如果要访问数组中的元素,则需要使用 array.length-1

 lastElement = array.length -1;
于 2019-07-15T13:33:00.370 回答