字符串的反转
作者:互联网
方法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);
}
}
标签:String,反转,stack,字符串,reverseStr,Stack 来源: https://www.cnblogs.com/yindong2019/p/12540553.html