Need help to understand String length logic
I am trying to understand how String length() function work while reversing a string.
String length is 4 then why i need to give length()-1 in below working code.
No issue in below code, need help to understand length()
public class MStringReverse {
String getReverse(String input) {
System.out.println(input.length());
String reverse = "";
for(int i = input.length() - 1; i >= 0; i--) {
reverse = reverse + input.charAt(i);
}
return reverse;
}
public static void main(String[] args) {
MStringReverse mr = new MStringReverse();
String result = mr.getReverse("Achilis");
System.out.println(result);
}
}
As the index starts from 0, not from 1. So if you have a String of length 4 then 0,1,2,3 are the only possible indexes. If your index provided as the argument in charAt() is less than 0 or greater than or equals the length of the String then you will get StringIndexOutOfBoundsException exception. Here you can see how charAt method works :
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
That's because indexing starts at 0.
charAt(0) is character 1.
The answer is that you are iterating on indexes, which start at 0.
Imagine an array of length 4. It will store 4 items, the first one at index 0, second at index 1, third at 2 and the last one at index 3. The index of the last element is always length() - 1, that's why you put it as the upper border in loops in order to not raise an IndexOutOfBoundsException while iterating.
You could add some console output to view the accessed indexes per String for each iteration like this:
public class MStringReverse {
static String getReverse(String input) {
System.out.println("Input length is " + input.length());
String reverse = "";
for(int i = input.length() - 1; i >= 0; i--) {
System.out.println("accessing index " + i + " of \"input\"");
reverse = reverse + input.charAt(i);
System.out.println("last index of \"reverse\" is now " + (reverse.length() - 1));
}
return reverse;
}
public static void main(String[] args) {
String result = getReverse("Achilis");
System.out.println(result);
}
}
The last index of the String is 1 less than the length because of a 0-based index.
i.e.
abcd has a length of 4 but to iterate from the last character, you need to start at index 3 (which is d), therefore length()-1 is where you start.
Because your string index starts at 0. Your length is 7. If you access input.charAt(7) you will get an index out of range exception.
A c h i l i s
0 1 2 3 4 5 6
String length is always less than one because it starts index position from 0 .
if the String length is 4 then index position starts from 0 to 3
For this, you just understand the concept of the array.
Suppose we have an array of int of size 5.
So if size is 5 that means array index is from 0 to 4 last index always -1 from actual size.
Same apply on String length method in case of reversing the string.
Suppose you
String name = "Stack";
Its length is 5 but the last index is 4 because of the last index always -1 from actual length.
String length is 4 then why i need to give length()-1 in below working code.
No issue in below code, need help to understand length()
public class MStringReverse {
String getReverse(String input) {
System.out.println(input.length());
String reverse = "";
for(int i = input.length() - 1; i >= 0; i--) {
reverse = reverse + input.charAt(i);
}
return reverse;
}
public static void main(String[] args) {
MStringReverse mr = new MStringReverse();
String result = mr.getReverse("Achilis");
System.out.println(result);
}
}
As the index starts from 0, not from 1. So if you have a String of length 4 then 0,1,2,3 are the only possible indexes. If your index provided as the argument in charAt() is less than 0 or greater than or equals the length of the String then you will get StringIndexOutOfBoundsException exception. Here you can see how charAt method works :
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
That's because indexing starts at 0.
charAt(0) is character 1.
The answer is that you are iterating on indexes, which start at 0.
Imagine an array of length 4. It will store 4 items, the first one at index 0, second at index 1, third at 2 and the last one at index 3. The index of the last element is always length() - 1, that's why you put it as the upper border in loops in order to not raise an IndexOutOfBoundsException while iterating.
You could add some console output to view the accessed indexes per String for each iteration like this:
public class MStringReverse {
static String getReverse(String input) {
System.out.println("Input length is " + input.length());
String reverse = "";
for(int i = input.length() - 1; i >= 0; i--) {
System.out.println("accessing index " + i + " of \"input\"");
reverse = reverse + input.charAt(i);
System.out.println("last index of \"reverse\" is now " + (reverse.length() - 1));
}
return reverse;
}
public static void main(String[] args) {
String result = getReverse("Achilis");
System.out.println(result);
}
}
The last index of the String is 1 less than the length because of a 0-based index.
i.e.
abcd has a length of 4 but to iterate from the last character, you need to start at index 3 (which is d), therefore length()-1 is where you start.
Because your string index starts at 0. Your length is 7. If you access input.charAt(7) you will get an index out of range exception.
A c h i l i s
0 1 2 3 4 5 6
String length is always less than one because it starts index position from 0 .
if the String length is 4 then index position starts from 0 to 3
For this, you just understand the concept of the array.
Suppose we have an array of int of size 5.
So if size is 5 that means array index is from 0 to 4 last index always -1 from actual size.
Same apply on String length method in case of reversing the string.
Suppose you
String name = "Stack";
Its length is 5 but the last index is 4 because of the last index always -1 from actual length.
Comments
Post a Comment