Step By Step(Java 常用对象篇<二>) (二)

2014-11-24 03:21:36 · 作者: · 浏览: 5
e key)

这个方法与前一个方法类似,但它只搜索a数组中formIndex到toIndex索引的元素。调用该方法时要求数组中元素已经按升序排列,这样才能得到正确结果。

  type[] copyOf(type[] original,int newLength)

这个方法将会把original数组复制成一个新数组,其中length是新数组的长度。如果length小于original数组的长度,则新数组就是原数组的前面length个元素;如果length大于original数组的长度,则新数组的前面元素就是原数组的所个元素,后面补充0(数值型)、false(布尔型)或者null(引用型)。

  type[] copyOfRange(type[] original,int from,int to)

这个方法与前面方法相似,但这个方法只复制original数组的from索引到to索引的元素。

  boolean equals(type[] a,type[] a2)

如果a数组和a2数组的长度相等,而且a数组和a2数组的数组元素也一一相同,该方法将返回true.

  void fill(type[] a,type val)

该方法将会把a数组所有元素值都赋值为val.

  void fill(type[] a,int fromIndex,int toIndex,type val)

该方法与前一个方法的作用相同,区别只是该方法仅仅将a数组的fromIndex到toIndex索引的数组元素赋值为val.

  void sort(type[] a)

该方法对a数组的数组元素进行排序。

  void sort(type[] a,int fromIndex,int toIndex)

该方法与前一个方法相似,区别是该方法仅仅对fromIndex到toIndex索引的元素进行排序。

1) 一个基本的示例代码:

1 public static void main(String[] args) {

2 int[] a1 = new int[]{3,4,5,6};

3   int[] a2 = new int[]{3,4,5,6};

4   //a1数组和a2数组的长度相等,每个元素依次相等,将输出true

5   System.out.println("a1数组和a2数组是否相等:" + Arrays.equals(a1,a2));

6   //通过复制a数组,生成一个新的b数组

7   int[] b = Arrays.copyOf(a1,6);

8   System.out.println("a1数组和b数组是否相等:" + Arrays.equals(a1,b));

9   //输出b数组的元素,将输出[3, 4, 5, 6, 0, 0]

10   System.out.println("b数组的元素为:" + Arrays.toString(b));

11   //将b数组的第3个元素(包括)到第5个元素(不包括)赋为1

12   Arrays.fill(b,2,4,1); //fill方法可一次对多个数组元素进行批量赋值

13   //输出b数组的元素,将输出[3, 4, 1, 1, 0, 0]

14   System.out.println("b数组的元素为:" + Arrays.toString(b));

15   //对b数组进行排序

16   Arrays.sort(b);

17   //输出b数组的元素,将输出[0, 0, 1, 1, 3, 4]

18   System.out.println("b数组的元素为:" + Arrays.toString(b));

19   }

20 /* 输出结果如下:

21 a1数组和a2数组是否相等:true

22 a1数组和b数组是否相等:false

23 b数组的元素为:[3, 4, 5, 6, 0, 0]

24 b数组的元素为:[3, 4, 1, 1, 0, 0]

25 b数组的元素为:[0, 0, 1, 1, 3, 4]

26 */

复制代码

2) 和java.utils.Arrays中提供的数组copy相比,Java中还提供另外一个数组copy的方法:System.arraycopy();

void arraycopy(Object from,int fromIndex,Object to,int toIndex,int count)

将第一个数组中的元素copy到第二个数组中。

from: 任意类型的数组

fromIndex: 原始数组中待copy元素的启示下标

to: 与from同类型的数组

toIndex: 目标数组放置copy元素的起始下标

count: copy的元素数量

1 public static void main(String[] args) {

2 int[] arr1 = {1, 2, 3, 4, 5};

3 int[] arr2 = new int[5];

4 System.arraycopy(arr1, 0, arr2, 0, arr1.length);

5 for(int i = 0; i < arr2.length; i++)

6 System.out.print(arr2[i] + " ");

7 System.out.println();

8 }

9 /* 输出结果如下:

10 1 2 3 4 5

11 */

复制代码

3) 比较System.arraycopy和通过手工赋值方式copy数组数据在执行效率上的差异:

1 public static void main(String args[]) {

2 final int testLength = 100000;

3 int[] arr1 = new int[testLength];

4 int[] arr2 = new int[testLength];

5 for (int i = 0; i < testLength; ++i) {

6 arr1[i] = i;

7 }

8 long start = System.currentTimeMillis();

9 for (int j = 0; j < 1000; ++j)

10 System.arraycopy(arr1, 0, arr2, 0, arr1.length);

11 long elapse = System.currentTimeMillis() - start;

12 System.out.println("The elapse for System.arraycopy is " + elapse + " ms");

13

14 start = System.currentTimeMillis();

15 for (int i = 0; i < 1000; ++i) {

16 for (