字符串逆向输出

一、思路:将字符串转化为字符串数组,而后根据下标反向输出
java

public void reverse() {
		String s = "hello";
		char[] c;
		c = s.toCharArray();  //转化为字符串数组
		for (int i = c.length - 1; i >= 0; i--) {
			System.out.print(c[i]);   //下标逆向输出
		}
	}

二、用String的那个charAt()方法数组

public void reverse2() {
		String s = "hello";
		
		for (int i = s.length()-1; i >= 0; --i) {
			System.out.print(s.charAt(i));   //下标逆向输出
		}
	
	}


三、调用StringBuffer的reverse()方法spa

public void reverse1() {
		StringBuffer s = new StringBuffer("hello");
		System.out.println(s.reverse());
	}

四、字符串两边互掉位置,从新组合造成新的字符串而后输出code

相关文章
相关标签/搜索