日期:2014-05-20  浏览次数:20774 次

求一个颠倒数组顺序的算法
例如abcd     调用算法后变为   dcba

------解决方案--------------------
public static void main(String[] args) {
  char[] c = "1234567 ".toCharArray();
  char[] d = c;
  reverse(d);    
  System.out.println(new String(d));
}

public static void reverse(char[] c) {
  int len = c.length;    
  for (int i = 0; i < len / 2; i++) {
    char tmp = c[i];
    c[i] = c[len - i - 1];
    c[len - i - 1] = tmp;
  }
}
------解决方案--------------------
当然有区别,方法就根本不一样!

数组:
Integer[] i = new Integer[]{1,2,3};
Collections.reverse(Arrays.asList(i));
System.out.println(Arrays.toString(i));

字符串:
System.out.println(new StringBuilder( "abcd ").reverse().toString());