java 为什么字符串长度-1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38281267/
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
why string length -1
提问by Siddhartha Rayaprolu
I have found the following program to check if a string is palindrome.
我找到了以下程序来检查字符串是否为回文。
import java.util.Scanner;
public class PalindromeString{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the string which you want to check whether that is palindrome or not: ");
String s = in.next();
String r = "";
for(int i=s.length()-1; i>=0; i--){
r = r+s.charAt(i);
}
System.out.println("Reverse of entered string "+s+" is "+r);
if(r.equals(s)){
System.out.println("String "+s+" is palindrome.");
}else{
System.out.println("String "+s+" is not palindrome.");
}
}
}
I did not understand why the coder initialized the loop variable i
to s.length()-1
. Can anyone explain me why? I have searched related content for string length but couldn't find an answer for this.
我不明白为什么编码器将循环变量初始化i
为s.length()-1
. 谁能解释我为什么?我搜索了字符串长度的相关内容,但找不到答案。
回答by Burkely91
The indexes of elements in Java (And most other languages) always start with 0 and continue to the number 1 below the size of the element/number of elements.
Java(和大多数其他语言)中的元素索引总是从 0 开始,并继续到元素大小/元素数量以下的数字 1。
String string = "Hello World"
"H" is the first element in the string and is therefore indexed '0', ie string[0] = "H", "e" is indexed '1' -- string[1] = "e", etc. The last element will be indexed '10'.
"H" 是字符串中的第一个元素,因此索引为 '0',即 string[0] = "H","e" 的索引为 '1' -- string[1] = "e" 等。最后一个元素将被索引为“10”。
Thus, in the example above with an array with 11 elements the indexes go from 0 to 10 (The string length minus one).
因此,在上面带有 11 个元素的数组的示例中,索引从 0 到 10(字符串长度减一)。
I think this explains it better than I do (and has pictures!) -- http://codingbat.com/doc/java-string-introduction.html
我认为这比我更好地解释了它(并且有图片!)--http://codingbat.com/doc/java-string-introduction.html
Don't forget a string is an array of chars in Java, so read this for more info on manipulating and accessing arrays -- http://tutorials.jenkov.com/java/arrays.html
不要忘记字符串是 Java 中的字符数组,因此请阅读本文以获取有关操作和访问数组的更多信息——http: //tutorials.jenkov.com/java/arrays.html
回答by Ruben Pirotte
Strings are arrays of chars, and array indexes start to count from 0.
字符串是字符数组,数组索引从 0 开始计数。
length is the amount of elements in that char array, but because the array starts at 0 the highest index is the amount of elements - 1 ==> .length() -1
length 是该字符数组中的元素数量,但由于数组从 0 开始,因此最高索引是元素数量 - 1 ==> .length() -1
回答by Steve101
The index for charAt starts at 0. ie. 0 is the first character.
charAt 的索引从 0 开始。即。0 是第一个字符。
This means a string of length 10, has chars between 0-9, not 1-10.
这意味着长度为 10 的字符串,字符在 0-9 之间,而不是 1-10。
回答by u6567863
The index for charAt starts at 0. ie. 0 is the first character.
charAt 的索引从 0 开始。即。0 是第一个字符。
This means a string of length 10, has chars between 0-9, not 1-10. 应该是这样子的
这意味着长度为 10 的字符串,字符在 0-9 之间,而不是 1-10。应该是这样子的
回答by Rashin
Because java indexing arrays from 0.
因为java从0索引数组。
For example "Hello" string's length is 5. the 'H' is the 0. charachter of the "Hello" string. According to this, the 'e' charachter is int the 1. position, and the 'o' is in the 4.
例如“Hello”字符串的长度是5。“H”是“Hello”字符串的0.字符。据此,“e”字符位于 1. 位置,“o”位于 4. 位置。
Easy to see that there are no 5 (the length of the string) position, so if we want the last charachter, we need the length-1. position.
很容易看出没有5(字符串的长度)位置,所以如果我们想要最后一个字符,我们需要length-1。位置。
Also I suggest take a look at this
我也建议看看这个
回答by Vamshidhar R
Because Java Strings use zero-based indexing.
因为 Java 字符串使用从零开始的索引。
i.e. Character indices in the string are from 0...n-1
即字符串中的字符索引来自 0...n-1