Considere a classe Java abaixo.O que o console exibirá qu...
c-
The indexOf() method returns the position of the first occurrence of specified character(s) in a string.
An int value, representing the index of the first occurrence of the character in the string, or -1 if it never occurs
https://www.w3schools.com/java/ref_string_indexof.asp
____________________________________________________________________________________________________________
The charAt() method returns the character at the specified index in a string.
The index of the first character is 0, the second character is 1, and so on.
https://www.w3schools.com/java/ref_string_charat.asp
____________________________________________________________________________________________________________
1° iteração:
String vowel = "aeiou";
char c=s1.charAt(s1.length()-cont-1);
charAt usa o tamanha do String (6) menos valor atual cont -1. cont = 0. logo, 6-1 = 5.
pos 5 do String: l.
c = 'l'.
funcao indexOf retorna -1 porque letra L nao consta em String aeiou.
-1 < 0. logo:
s2+=Character.toString(c);
"l" é 1° valor.
cont recebe +1.
funcao se invoca> recursion
2° iteração:
char c=s1.charAt(s1.length()-cont-1); .-> 6-2 = 4.
c = i.
'i' const em vowel.indexOf. retornando 2.
2<0. entra no else:
s2+=".";
"." é 2° valor.
cont=2;
3° iteração:
char c=s1.charAt(s1.length()-cont-1); .-> 6-3 = 3
c = 's' -> s1.charAt -> 's' esta na pos 3 do String "brasil".
's' retorna -1 em vowel.indexOf. logo:
s2+=Character.toString(c);
3° valor = "s";
cont=3;
4° iteração:
char c=s1.charAt(s1.length()-cont-1); .-> 6-4 = 2
c = 'a' -> s1.charAt -> 'a' esta na pos 2 do String "brasil".
'a' retorna 0 em vowel.indexOf. logo:
s2+=".";
4° valor = ".";
cont=4;
5° iteração:
char c=s1.charAt(s1.length()-cont-1); .-> 6-5 = 1
c = 'r' -> s1.charAt -> 'r' esta na pos 1 do String "brasil".
'r' retorna -1 em vowel.indexOf. logo:
s2+=Character.toString(c);
5° valor = "r";
cont=5;
6° iteração:
char c=s1.charAt(s1.length()-cont-1); .-> 6-6 = 0
c = 'b' -> s1.charAt -> 'b' esta na pos 0 do String "brasil".
'b' retorna -1 em vowel.indexOf. logo:
s2+=Character.toString(c);
6° valor = "b";
cont=6;
if (cont == s1.length())
return s2;
cont é igual ao tamanha do String. loop termina.
s2 é:
l.s.rb
Navegue em mais questões