字符串(可更改)
public class Str1 {
public static void main(String[] args) {
String str ="cyycsy非常好";
System.out.println(str);
String sd="";
for (int i = 0; i <str.length(); i++){
System.out.println(str.charAt(str.length()-1-i)); //倒序,y轴倒序
sd=str.substring(i,i+1)+sd; //str.substring(indexStart,indexEnd)截取方法,含头不含尾
}
System.out.println(sd);
System.out.println(new StringBuffer(sd).reverse()); //另一种倒序输出
}
}
字符 (可更改)
public class Char1 {
public static void main(String[] args) {
//倒序
String str = "ABC123ABC";
char[] charArray = str.toCharArray();
Stack<Character> stack = new Stack<>();
StringBuilder newStr = new StringBuilder();
for (char item : charArray) {
stack.push(item);
}
for (int i = 0; i < charArray.length; i++) {
newStr.append(stack.pop());
System.out.println("反转前:" + str);
System.out.println("反转后:" + newStr.toString());
}
}
}