本帖最后由 葛奎 于 2012-6-26 18:36 编辑
- class StringTest3
- {
- public static void main(String[] args)
- {
- String str="abcdef";
- char[] ch=str.toCharArray();
- char[] ch1=startToEnd(ch,4,4);<font size="5">//请问:(ch,2,4) (ch,0,5)这里面不管输入的是什么,怎么最后的运行的结果都是abcdef????</font>
- sop(charToString(ch1));
- }
- public static String charToString(char[] ch)
- {
- String str="";
- for(int i=0;i<ch.length;i++)
- {
- str+=ch[i];
- }
- return str;
- }
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- public static char[] startToEnd(char[] arr,int start,int end)
- {
- if(start<0||end>(arr.length-1)||(start-end)<=0)
- return arr;
- else
- {
- while((end-start)>=0)
- {
- swap(arr,start,end);
- start++;
- end--;
- }
- return arr;
- }
- }
- public static void swap(char[] arr,int a,int b)
- {
- char ch;
- ch=arr[a];
- arr[a]=arr[b];
- arr[b]=ch;
- }
- }
复制代码 |
|