倒置字符串中各个字符位置

前端之家收集整理的这篇文章主要介绍了倒置字符串中各个字符位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. /**
  2. * 倒置字符串中各个字符位置
  3. *
  4. * @author Administrator
  5. *
  6. */
  7. public class Test_008 {
  8. public static void main(String[] args) {
  9. String str = "abcdefghigk";
  10. char[] s = str.tocharArray();
  11. System.out.println("倒置前:" + Arrays.toString(s));
  12. reverse(s);
  13. System.out.println("倒置后:" + Arrays.toString(s));
  14. }
  15.  
  16. static void reverse(char[] s) {
  17. char temp;
  18. for (int i = 0,j = s.length - 1; i < j; i++,j--) {
  19. temp = s[i];
  20. s[i] = s[j];
  21. s[j] = temp;
  22. }
  23. }
  24. }

输出结果:

  1. 倒置前:[a,b,c,d,e,f,g,h,i,k]
  2. 倒置后:[k,a]

猜你在找的设计模式相关文章