本帖最后由 张政 于 2013-2-2 21:48 编辑
两个问题- /*
- 字符串反转
- */
- class FanZhuan{
- public static char[] fanZhuan(char[] str){
- for(int i = 0,end = str.length - 1; i<end; i++,end--)
- swap(str,i,end);
- return str;
- }
- //重载
- public static char[] fanZhuan(char[] str,int start,int end){
- for(int i = start; i<end; i++,end--)
- swap(str,i,end);
- return str;
- }
- private static void swap(char[] str,int i,int end){
- //异或
- str[i] = (char)(str[i]^str[end]);
- str[end] = (char)(str[i]^str[end]);
- str[i] = (char)(str[end]^str[i]);
- }
- //输出
- public static void show(String str){
- System.out.println("shwo:" + str);
- }
- }
- class String_toCharArray{
- public static void main(String[] args){
- String str = "123456";
- char[] cr = str.toCharArray();
- FanZhuan.show(new String(FanZhuan.fanZhuan(cr)));
- }
- }
复制代码 上面的方式跟把除了要调用的私有不静态 然后让主函数new对象调 哪个好?- /*
- 字符串反转2
- */
- class FanZhuan{
- private static char[] fanZhuan(char[] str){
- for(int i = 0,end = str.length - 1; i<end; i++,end--)
- swap(str,i,end);
- return str;
- }
- //重载
- private static char[] fanZhuan(char[] str,int start,int end){
- for(int i = start; i<end; i++,end--)
- swap(str,i,end);
- return str;
- }
- private static void swap(char[] str,int i,int end){
- //异或
- str[i] = (char)(str[i]^str[end]);
- str[end] = (char)(str[i]^str[end]);
- str[i] = (char)(str[end]^str[i]);
- }
- public static void call(String str){
- char[] cr = str.toCharArray();
- show(new String(fanZhuan(cr)));
- }
- public static void call(String str,int start,int end){
- char[] cr = str.toCharArray();
- show(new String(fanZhuan(cr,start,end)));
- }
- //输出
- private static void show(String str){
- System.out.println("shwo:" + str);
- }
- }
- class String_toCharArray{
- public static void main(String[] args){
- String str = "123456";
- FanZhuan.call(str,2,5);
- }
- }
复制代码 call方法存在有没有意义
各位帮我从代码优化、性能效率上分析下 thank. |
|