copyOf和copyOfRange()的使用方法

来源:互联网 发布:英语图表数据作文模板 编辑:程序博客网 时间:2024/06/04 00:48


import java.util.*;;
public class Example7_4 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
   int [] a={11,22,33,44,55},b,c,d;
    System.out.println("数组a中各元素的值为:");
    System.out.println(Arrays.toString(a));
    b=Arrays.copyOf(a,8);
    System.out.println("数组b中各元素的值为:");
    System.out.println(Arrays.toString(b));
    c=Arrays.copyOfRange(a,0,4);
    System.out.println("数组c中各元素的值为:");
    System.out.println(Arrays.toString(c));
    d=Arrays.copyOfRange(a,3,10);
    System.out.println("数组d中各元素的值为:");
    System.out.println(Arrays.toString(d));
 }

}

//注意:Arrays.copyOf(a,length)和Arrays.copyOfRange(a,to,from)中,length代表新数组的长度,而对Arrays.copyOfRange(a,to,from)而言,其长度为from-to,数组是从a{from]到

a[to-1]的。