Arrays的用法

来源:互联网 发布:2017乒超联赛网络直播 编辑:程序博客网 时间:2024/06/11 20:02

Arrays是Sun公司就在JDK中提供了一个数组的工具类,此类包含用来操作数组(比如排序和搜索)的各种方法。此类还包含一个允许将数组作为列表来查看的静态工厂。Arrays类在myeclipse的安装路径下binary的src.zip\java\ util中的Arrays类中

1. 关于Arrrays类的方法的使用:

(1)先导入包,再使用Arrays类中的方法。

import java.util.Arrays; //或import java.util.*;//表示找到整个util包下的所有类
Arrays.toString(arr1);

(2)直接使用类中的方法

java.util.Arrays.toString(arr1);

注意:使用Arrays类需要找到java.util这个包下的Arrays类,java才能使用Arrays类,System类是因为Java默认会找到java.lang包。所以System类不需要添加java.lang
Arrays常用的方法有:

*fill:给数组赋值
*toString:打印出数组的每个元素
*sort:对数组进行升序排序
*binarySearch:对排序好的数组进行二分查找法操作
*copyOf:对指定的数组进行复制,以使副本有指定的长度(从索引为0开始复制)

* 2.Arrays类中的fill:分配值给指定数组元素*

对于整个数组的赋值: public static void fill(int[] a,int val)
将指定的int值分配给指定int型数组的每个元素。

 参数: a - 要填充的数组 val - 要存储在数组所有元素中的值

对于数组的某一段进行赋值: public static void fill(float[] a,int fromIndex,int toIndex,float val);
参数:
a - 要填充的数组
fromIndex - 要使用指定值填充的第一个元素的索引(包括)
toIndex - 要使用指定值填充的最后一个元素的索引(不包括)
val - 要存储在数组所有元素中的值
赋值范围:[fromIndex,toIndex)

package com.JAVABASIS5;
import java.util.Arrays;;
public class Practice {
public static void main(String[] args){
int[] arr1=new int[]{5,4,8,2,6,1,3,9,7,10};
Arrays.fill(arr1,1,4,0);//对数组的某一段进行赋值
System.out.println(Arrays.toString(arr1));//输出arr1数组为:[5, 0, 0, 0, 6, 1, 3, 9, 7, 10]
int[] arr2= new int[5];
Arrays.fill(arr2, 0);//对整个数组进行赋值为0
System.out.println(Arrays.toString(arr2));//输出arr2数组为:[0, 0, 0, 0, 0]
}

}

3.Arrays类中的toString:返回指定数组内容的字符串表示形式。

package com.JAVABASIS5;
import java.util.Arrays;
public class Practice {
public static void main(String[] args){
int[] arr1=new int[]{5,4,8,2,6,1,3,9,7,10};
String ret =Arrays.toString(arr1);//ret是String类型的原因是因为输出是String类型
System.out.println(ret);
}
}

4.Arrays类中的sort:对对指定的数组按数字升序进行排序

对整个数组进行升序排序: public static void sort(long[] a)
参数:
a - 要排序的数组
对数组的一段元素进行升序排序: public static void sort(long[] a,int fromIndex,int toIndex)
对指定 long 型数组的指定范围按数字升序进行排序。排序的范围从索引 fromIndex(包括)一直到索引 toIndex(不包括)。(如果 fromIndex==toIndex,则排序范围为空。)
参数:
a - 要排序的数组
fromIndex - 要排序的第一个元素的索引(包括)
toIndex - 要排序的最后一个元素的索引(不包括)
排序范围:[fromIndex,toIndex)

package com.JAVABASIS5;
import java.util.Arrays;
public class Practice {
public static void main(String[] args){
//对整个数组进行升序排序
int[] arr1=new int[]{5,4,8,2,6,1,3,9,7,10};
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
//对数组的索引选取范围进行升序排序,对索引[1,6)的元素进行升序排序
int[] arr2=new int[]{11,1,17,8,19,5,20,15,18};
Arrays.sort(arr2,1,6); //输出为[11, 1, 5, 8, 17, 19, 20, 15, 18]
System.out.println(Arrays.toString(arr2));
}
}

5.Arrays类中的binarySearch:对已排序好的数组进行二分法查找

对整个数组进行查找: public static int binarySearch(char[] a,char key)
必须在进行此调用之前对数组进行排序。如果没有对数组进行排序,则结果是不确定的。如果数组包含多个带有指定值的元素,则无法保证找到的是哪一个。
参数:
a - 要搜索的数组
key - 要搜索的值
对指定的数组的范围进行查找: public static int binarySearch(char[] a,int fromIndex,int toIndex,char key)
参数:
a - 要搜索的数组
fromIndex - 要搜索的第一个元素的索引(包括)
toIndex - 要搜索的最后一个元素的索引(不包括)
key - 要搜索的值
搜索范围:[fromIndex,toIndex)

package com.JAVABASIS5;
import java.util.Arrays;
public class Practice {
public static void main(String[] args){
int[] arr1=new int[]{1,2,3,4,5,6,7,8,9,10};
//对整个数组进行搜索
System.out.println(Arrays.binarySearch(arr1,7));//其索引为6
//对指定的数组范围进行搜索
System.out.println(Arrays.binarySearch(arr1,1,8,5));//元素5的索引为4
//使用指定范围进行搜索是为了优化其性能,减少损耗
}
}

6.Arrays类中的copyOf:对指定数组进行复制, 以使副本具有指定的长度

public static boolean[] copyOf(boolean[] original,
int newLength)

    参数:        original - 要复制的数组        newLength - 要返回的副本的长度 

package com.JAVABASIS5;
import java.util.Arrays;
public class Practice {
public static void main(String[] args){
int[] arr1=new int[]{1,2,3,4,5,6,7,8,9,10};
int[] arr2=Arrays.copyOf(arr1, 4);//需要赋值给一个新数组才能体现出是否复制成功
System.out.println(Arrays.toString(arr2));//输出结果为:[1, 2, 3, 4]
int[] dest = new int[10];
System.arraycopy(arr1,2,dest,4,4);
System.out.println(Arrays.toString(dest));//输出结果为:[0, 0, 0, 0, 3, 4, 5, 6, 0, 0]
//使用copyOf是从数组的索引为0开始复制到数组长度为4为止,而System类则可以选择从哪一个位置开始复制
}
}

原创粉丝点击