0
Reverse a string question java
https://code.sololearn.com/c5s77FZsJIZX/?ref=app This is how i solved the coding project to reverse a string in java. Im just wondering why it outputs characters on different lines. Also why when i write "arr.length -1", why does that reverse it instead of just taking one char away from the length. Shouldnt it be arr.length * -1? I overthink my own code too much.
18 odpowiedzi
+ 3
array.length-1 index is same as index [-1] in python, not [::-1]
Ex: "ABCD" is word
| | | |
0,1,2,3 indexes, length is number of characters which is 4
word[ array.length-1] is 'D'
So maximum valid index is array.length-1 only.. (Note: index starts from 0 )
In python, it's same as word[-1] , negetive index allowed in python..
word[::-1] reverse total string
edit:
Kristopher updated read again
Hope it helps...
+ 1
word[::-1]
Is actually
word[(len(word)-1)::-1]
It is just an index, no matter if it's Python or Java.
0
Use System.out.print() to print in the same line. For the second question, I dont understand what you mean
0
Infinity i mean inside the for-loop, when i wrote "arr.length-1" why does that reverse the length of the string instead of taking 1 character off the end
0
Because println does exactly what the name says: it prints a *line*. Use print if you do not want line breaks.
length does exactly what the names says: It returns the length of the string. If you want to iterate the characters of the string, you need to consider that indexing starts with 0.
0
Lisa but if String str ="hello" has a length of 5, then why would str.length -1 not just make it "hell"?
0
arr.length returns a number - an integer which tells you how many numbers are there in the array. It doesn't reverse anything. The loop does. The looping variable 'i' is intialised to arr.length - 1 and goes down to 0 which is the starting index of the array, basically accessing the array elements in the reverse direction and printing at the same time. It doesn't change anything in the orignal distribution of the elements in the array.
0
"hello" has length 5, right? What is the index of "o"? – it's 4
0
No, they are not the same. arr.length - 1 returns a number. arr[::-1] in Python returns a string
0
Lisa when u index usually u would use [ ] to indicate that but here you just put -1
0
length - 1 just says that you start from the last letter.
I wouldn't directly compare it to Python slicing but the outcome is the same, so if it help, you can think of it like that :)
0
Infinity 😭 i must be stupid or something cause i dont understand lmfao. So the .length returns an integer for the amount of characters in a string. So for ex: int x = [1,3,2] has length of 3. So why does x.length - 1 return [2, 3, 1] instead of [1, 3,].?
0
Lisa ok thank you i guess thats just something that works in java. Im new to coding and i dont think sololearn really ever told me i could do something like that. Very confusing
0
Lisa so if you did word[array.length -2] its gonna start from 1 (B) and then initiate the for loop?
0
Jayakrishna🇮🇳 yeah i understand how indexing the array works, i just didnt understand why you can just say arr.length - 1 instead of something like arr.length[-1]
0
arr.length is exactly one integer.
arr.length is not the same as arr[3] or arr[arr.length-1]
arr[arr.length - 2] would ve the 2nd last char. You can start the loop with any number you want, you can count upwards/ left-to-right or downwards/ right-to-left as long as it is an index that exists
0
Kristopher There
arr is an array
length is property of array, which returns length means number of elements in array.
So arr.length returns a integer, and now this is not an array to index,
Java does not allow negetive indexing..
arr[1] is valid,
arr[-1] is invalid,
arr.length[-1] => is indexing a number, invalid..
In python, it's like
arr[ len(arr)-1 ] , it returns last character, and also you can use as arr[-1] in python.
is it valid to use len(arr)[-1] in python? No.
arr.length-1 returns last , maximum valid index. It's an integer..
Hope it clears..!!!