黑马程序员技术交流社区

标题: 代码优化 [打印本页]

作者: zhangx    时间: 2013-4-12 21:45
标题: 代码优化
本帖最后由 zhangx 于 2013-4-13 10:54 编辑

假如不要java类库,这段代码怎么优化啊?
public class Demo{
public static void main(String args[]){
int score[]=null;
score=new int[10];
for(int i=0;i<10;i++){
  score=i ;
  System.out.print(score+(i==9?"":","));
  }
  System.out.println();
for(int i=0;i<5;i++){
  int temp=0;
  temp=score;
  score=score[9-i];
  score[9-i]=temp;
}
for(int j=0;j<10;j++){
  System.out.print(score[j]+(j==9?"":","));
}
}
}

作者: 李芳池    时间: 2013-4-12 22:19
  1. class Demo{
  2.         public static void main(String[] args){
  3.                 int[] score=new int[10];
  4.                 for (int i=0;i<score.length;i++){
  5.                         score[i]=i;
  6.                 }
  7.                 print(score);
  8.                 reverseArray(score);
  9.                 System.out.println("反转后的数组:");
  10.                 print(score);
  11.                

  12.         }
  13.         private static void reverseArray(int[] arr){
  14.        
  15.                 for (int start=0,end=arr.length-1;start<end;start++,end-- ){
  16.                         int temp = arr[start];
  17.                         arr[start] = arr[end];
  18.                         arr[end] = temp;
  19.                 }
  20.         }
  21.         public static void print(int[] arr){
  22.                 for (int i=0;i<arr.length ;i++ ){
  23.                         System.out.print(arr[i]+(i==9?"":","));
  24.                 }
  25.                 System.out.println();
  26.         }
  27. }
复制代码

作者: 何俊森    时间: 2013-4-12 22:26
这里说到优化,只能把临时变量temp的空间节省掉了吧。
  1. public class Demo01 {
  2.         public static void main(String args[]) {
  3.                 int score[] = null;
  4.                 score = new int[10];
  5.                 for (int i = 0; i < 10; i++) {
  6.                         score[i] = i;
  7.                         System.out.print(score[i] + (i == 9 ? "" : ","));
  8.                 }
  9.                 System.out.println();
  10.                 for (int i = 0; i < 5; i++) {
  11.                         score[i] ^= score[9 - i];
  12.                         score[9 - i] ^= score[i];
  13.                         score[i] ^= score[9 - i];
  14.                 }
  15.                 for (int j = 0; j < 10; j++) {
  16.                         System.out.print(score[j] + (j == 9 ? "" : ","));
  17.                 }
  18.         }
  19. }
复制代码

作者: 杨永胜    时间: 2013-4-12 22:26
面向对象编程,代码就简单多了,2楼正确
作者: 杨永胜    时间: 2013-4-12 22:26
面向对象编程,代码就简单多了,2楼正确
作者: 刘策    时间: 2013-4-12 22:40
public class Demo{
        public static void main(String[] args){
                int[] score = new int[10];
                for(int i=0;i<5;i++){
                        if(i==0){
                                for(int x=0;x<10;x++){
                                        score[x]=x;
                                        System.out.println(score[x]+(i==9?"":","));       
                                }
                        }
                        else{
                                score[i] = score[i]^score[9-i];
                                score[9-i] = score[i]^score[9-i];
                                score[i] = score[i]^score[9-i];
                        }
                }       
                for( int s : score){
                        System.out.println(s);       
                }
        }
}这个也可能实现换位置。
作者: 打工人    时间: 2013-4-12 23:12

如果问题未解决,请继续追问,如果没有问题了,请将帖子分类 改为“已解决”,谢谢
作者: zhangx    时间: 2013-4-13 10:54
嗯  看到了  解决了,谢谢你们




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2