标签:build rgs ever 方法 stat bsp print length ack
方法1:
利用stark的先进后出的特点
思路:
1.将字符串转换为字符型数组
2.遍历数组,入栈 Stark.pull()
3.出栈 Stark.pop(),利用Stringbuider.append拼接输出。
import java.util.Stack;
public class Main {
public static void main(String[] args) {
//反转字符串
String a = "asdvggh";
System.out.println("反转之前:" + a);
method01(a);//方法1
}
private static void method01(String a) {
char[] b = a.toCharArray();
StringBuilder reverseStr = new StringBuilder();
Stack<Character> stack = new Stack<>();
for (char e : b) {
stack.push(e);
}
for (int i = 0; i < b.length; i++) {
reverseStr.append(stack.pop());
}
System.out.print("反转后:" + reverseStr);
}
}
标签:build rgs ever 方法 stat bsp print length ack
原文地址:https://www.cnblogs.com/yindong2019/p/12540553.html