


Exemple Java - méthodes push et pop pour obtenir une inversion de chaîne
Feb 04, 2017 am 09:59 AML'exemple suivant montre l'utilisation de la méthode StringReverserThroughStack() définie par l'utilisateur pour réaliser l'inversion de chaîne :
/* author by w3cschool.cc StringReverserThroughStack.java */import java.io.IOException;public class StringReverserThroughStack { private String input; private String output; public StringReverserThroughStack(String in) { input = in; } public String doRev() { int stackSize = input.length(); Stack theStack = new Stack(stackSize); for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); theStack.push(ch); } output = ""; while (!theStack.isEmpty()) { char ch = theStack.pop(); output = output + ch; } return output; } public static void main(String[] args) throws IOException { String input = "www.w3cschool.cc"; String output; StringReverserThroughStack theReverser = new StringReverserThroughStack(input); output = theReverser.doRev(); System.out.println("反转前: " + input); System.out.println("反转后: " + output); } class Stack { private int maxSize; private char[] stackArray; private int top; public Stack(int max) { maxSize = max; stackArray = new char[maxSize]; top = -1; } public void push(char j) { stackArray[++top] = j; } public char pop() { return stackArray[top--]; } public char peek() { return stackArray[top]; } public boolean isEmpty() { return (top == -1); } }}
Le résultat de l'exécution du code ci-dessus est :
反转前: 反转后: cc.loohcsc3w.www
Ce qui précède est l'exemple Java - la méthode de poussée et d'éclatement de la pile pour obtenir une inversion de chaîne. Pour plus de contenu connexe, veuillez faire attention au site Web PHP chinois (www.php.cn) !

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Générateur de nombres aléatoires en Java

Questions d'entretien chez Java Spring

Break or Return of Java 8 Stream Forach?
