统计信息

  • 访问数:9420
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P6Q12

2008-04-15 13:39:34

天气: 晴朗 心情: 高兴

/*filename:P6Q12
 *Name:Wang Chaoran
Desciprtion:
12 (Reversing an array)
The reverse method in §6.5 reverses an array by copying it to a new array.
Rewrite the method without creating new arrays.
*/
class P6Q12{
  public static void main(String[] args){
    double[] test1= {1,2,3,4,5,6,7,8,9};
    double[] test2= {1,2,3,4,5,6,7,8,9,10,11,12};
    System.out.println("Previous:");
    print(test1);
    System.out.println("After:");
    reverse(test1);
     print(test1);
    
    System.out.println("Previous:");
    print(test2);
    System.out.println("After:");
    reverse(test2);
     print(test2);
  }
  static double[] reverse(double[] array){
    double temp =0;
    for(int i=0;i<array.length/2+1;i++){
      temp=array[i];
      array[i]=array[array.length-1-i];
      array[array.length-1-i]=temp;
    }
    return array;
  }
  static void print(double[] array){
    for(int i=0;i<array.length;i++)
      System.out.print(array[i]+" ");
    System.out.println();
   
  }
}
加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd