package test3;
import java.util.Stack;
/**
* 字符串逆序输出
* @author Kui
* date : 2018/04/27
*
*/
public class StringReverse {
public static void main(String[] args) {
//创建一个栈实例 用于存储字符串
Stack<Character> stack = new Stack<>();
//定义一个字符串变量 并进行初始化
String str = "abcdef";
//将字符串转换为字符数组
char[] ch = str.toCharArray();
//遍历字符数组
for (int i = 0 ;i < ch.length ; i++) {
//将遍历出来的字符push()入栈
stack.push(ch[i]);
}
System.out.println("逆序输出字符串:" + str);
//遍历字符数组
for (int i = 0 ;i < ch.length ; i++){
//将遍历出来的字符pop()出栈 并打印到控制台
System.out.print( stack.pop());
}
}
import java.util.Stack;
/**
* 字符串逆序输出
* @author Kui
* date : 2018/04/27
*
*/
public class StringReverse {
public static void main(String[] args) {
//创建一个栈实例 用于存储字符串
Stack<Character> stack = new Stack<>();
//定义一个字符串变量 并进行初始化
String str = "abcdef";
//将字符串转换为字符数组
char[] ch = str.toCharArray();
//遍历字符数组
for (int i = 0 ;i < ch.length ; i++) {
//将遍历出来的字符push()入栈
stack.push(ch[i]);
}
System.out.println("逆序输出字符串:" + str);
//遍历字符数组
for (int i = 0 ;i < ch.length ; i++){
//将遍历出来的字符pop()出栈 并打印到控制台
System.out.print( stack.pop());
}
}
}
运行结果: