Aqui está uma maneira de implementar inversão de string em 5 em Java.
1. Matrizes implementam inversão de string
// Array implementa a inversão de string public string reversebyArray () {if (str == null || str.Length () == 1) {return null; } char [] ch = str.toCharArray (); // converte uma string em uma matriz de caracteres para (int i = 0; i <ch.length/2; i ++) {char temp = ch [i]; ch [i] = ch [ch.length-i-1]; ch [ch.Length-i-1] = temp; } retornar nova string (ch); }2. A inversão da corda é implementada
// Use a pilha para implementar a inversão de string public string reverseByStack () {if (str == null || str.Length () == 1) {return null; } Stack <personagem> pilha = new Stack <personagem> (); char [] ch = str.toCharArray (); // converte strings em matriz de caracteres para (char c: ch) {stack.push (c); // cada caractere, empurre a pilha} para (int i = 0; i <ch.length; } 3. Traversal reversa para realizar inversão de cordas
// Use Traversal reverso para implementar a String Inversão public String reverseBySort () {if (str == null || str.Length () == 1) {return null; } StringBuffer sb = new StringBuffer (); para (int i = str.Length () -1; i> = 0; i-) {sb.append (str.charat (i)); // use stringbuffer para unir caracteres da direita para a esquerda} retornar sb.tostring (); } 4. Operação de bits realiza reversão de string
// Use a operação de bit para implementar inversão de string public string reversebybit () {if (str == null || str.Length () == 1) {return null; } char [] ch = str.toCharArray (); // converte strings em matriz de caracteres int len = str.Length (); for (int i = 0; i <len/ 2; i ++) {ch [i]^= ch [len- 1- i]; ch [len- 1- i]^= ch [i]; ch [i]^= ch [len- 1- i]; } retornar nova string (ch); } 5. Implementar recursivamente inversão de string
// Use a recursão para implementar a inversão de string public string reversebyrecursive (string str) {if (str == null || str.Length () == 0) {return null; } if (str.Length () == 1) {return str; } else {// mar a corda do subscrito de 1, retorne reversebyrecursive (str.substring (1)) + str.charat (0); }} Vi. Teste
classe pública teste {public static void main (string [] args) {string s = "123456"; Reverso r = novo (s) reverso (s); System.out.println (R.ReverseByArray ()); System.out.println (r.ReverseByStack ()); System.out.println (R.ReverseBySort ()); System.out.println (r.ReverseByBit ()); System.out.println (R.ReverseByRecursive (s)); }}7. Resultados
8. Todos os códigos usados para inversão de corda
classe pública reverse {private string str = null; public reverse (string str) {this.str = str; } // Implementação de matriz string reversa public string reversebyArray () {if (str == null || str.Length () == 1) {return null; } char [] ch = str.toCharArray (); // converte a sequência em uma matriz de caracteres para (int i = 0; i <ch.length/2; i ++) {char temp = ch [i]; ch [i] = ch [ch.length-i-1]; ch [ch.Length-i-1] = temp; } retornar nova string (ch); } // Use a pilha para implementar String Inversão public String reverseByStack () {if (str == null || str.Length () == 1) {return null; } Stack <personagem> pilha = new Stack <personagem> (); char [] ch = str.toCharArray (); // converte strings em matriz de caracteres para (char c: ch) {stack.push (c); // cada caractere, empurre a pilha} para (int i = 0; i <ch.length; } // Use Traversal inverso para implementar a inversão de string public string reversebysort () {if (str == null || str.Length () == 1) {return null; } StringBuffer sb = new StringBuffer (); para (int i = str.Length () -1; i> = 0; i-) {sb.append (str.charat (i)); // use stringbuffer para unir caracteres da direita para a esquerda} retornar sb.tostring (); } // Use operações de bit para implementar a String Inversion public String reverseByBit () {if (str == null || str.Length () == 1) {return null; } char [] ch = str.toCharArray (); // converte strings em matriz de caracteres int len = str.Length (); for (int i = 0; i <len/ 2; i ++) {ch [i]^= ch [len- 1- i]; ch [len- 1- i]^= ch [i]; ch [i]^= ch [len- 1- i]; } retornar nova string (ch); } // Use a recursão para implementar string reversebyRecursive (string str) {if (str == null || str.Length () == 0) {return null; } if (str.Length () == 1) {return str; } else {// mar a corda do subscrito de 1, retorne reversebyrecursive (str.substring (1)) + str.charat (0); }}}As 5 maneiras acima de implementar a reversão de strings em Java são todo o conteúdo que compartilho com você. Espero que você possa lhe dar uma referência e espero que você possa apoiar mais o wulin.com.