何时使用 type.length-1; 和 type.length(); 在 Java 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19485971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:39:57  来源:igfitidea点击:

When to use type.length-1; and type.length(); in java

javavariable-length

提问by user2888585

I'm little bit confused when do I really need to use that length-1. I know that is when we don't want to have out of bound error. For instance I wrote this simple array:

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

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

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

It does not give me any error. Any examples when -1 is actually useful? Thank you in advance.

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

采纳答案by dtgee

You want to use oldArray.lengthusually in a for loop call, because as in your example,

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

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

Notice how igoes from 0up until oldArray.length - 1, but stops exacty at oldArray.length(and doesn't execute). Since arrays start at position 0instead of 1, old.Array.length is a perfect fit for the number that ishould stop at. If arrays started at position 1instead, for loops would look something like this:

请注意如何i0up 直到oldArray.length - 1,但准确地停止在oldArray.length(并且不执行)。由于数组从 position0而不是开始1, old.Array.length 非常适合i应该停止的数字。如果数组从位置开始1,for 循环将如下所示:

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

oldArray.length - 1is usually to access the last element:

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

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

Although this is usually when you would use length - 1vs length, there are many other cases where you would also want one over the other, and thus there is no real specific answer. But don't worry, keep coding, you'll get this hang of this soon ;)

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

回答by Terry Li

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++){

So basically, you want to use oldArray.length - 1when you need to access the last element.

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

回答by Thusitha Thilina Dayaratne

There is no specific answer for your question. It depends. one point is when you want to index the last element of an array

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

int lastElement = oldArray[oldArray.length-1];

回答by nasser-sh

Simply put, although your array has 6 elements, the indexing of the array begins at 0. That means that the index of the first element is 0, the second element 1, ... and the 6th element 5. Therefore, if you try to access oldArray[6], you will get an error. In the loop you demonstrate, this is never the case:

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

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

oldArray.lengthreturns 6. The highest value of ireached in the loop is 5, since the loop will break when the value of ibecomes 6. This is why there are no errors. So to answer your question directly, oldArray[oldArray.length - 1]will return the last element in the array.

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

回答by Trying

you can write as below as well:

你也可以这样写:

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

oldArray.length-1is needed when you want that particular element i.e. final elementfrom array.

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

i.e. oldArray[oldArray.length-1]

IE oldArray[oldArray.length-1]

回答by Bohemian

Loops and array utility methods can use array.lengthto mean "up to the last element".

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

The only place I can think of for using array.length - 1is when accessing the last element, eg to find largest number in an array:

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

int[] array;
Arrays.sort(array);
int largest = array[array.length - 1];

回答by afk5min

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

is equivalent to

相当于

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

回答by Jonny Frez

It is use when you want to get the last index number of the array. Seens the computer dosent know how long your array is it gets the indication by using -1. Seens 0 is the start then -1 is the end of the array.

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

I use this when i want to sort numbers just like the example that Bohemian wrote. Take a look at bubble sort algorithem

当我想像 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();

}

//bubble sort

//冒泡排序

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;
        }

     }
   }
}

回答by Jen Billingston

This completely depends on what you want to do. In an array you have a collection of things. If you were shown this array [1,2,3,4] say, in elementary school and someone asked you how many things are in there you would say four. You would count starting from one, which is completely reasonable. If someone asked you what the third thing in that array was, you'd point to the 3. Awesome! Except, now that you're coding, you know that arrays start at zero. So even though there are still four things in that array, their names have changed. Now 1's name is zero, 2's name is one, 3's name is two, and 4's name is three. So when you're using a loop and you just want the loop to go through all the things and that's it (like if you're searching through an array), you can use .length and it'll just run through. If you want to access the number 4 in the array specifically, though, 4's name is no longer 4 - it's 3! So you use length-1 because when it comes to indexes, we're using the new name given to the physical thing.

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

回答by Denise Ignatova

for (int i = 0; i < array.length; i++) {
    // this will loop throw the entire array
 }

if you want to access an element from array, you need to use array.length-1

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

 lastElement = array.length -1;