a3[i][j]=newInteger(i*j);
}
}
}
1.2填充数组—Arrays.fill()
Arrays的fill()方法,用一个值填充指定数组的各个位置。
对于保存对象的数组,就是复制同一个引用进行填充。
intsize=5;
boolean[]a1=newboolean[size];
byte[]a2=newbyte[size];
char[]a3=newchar[size];
short[]a4=newshort[size];
int[]a5=newint[size];
long[]a6=newlong[size];
float[]a7=newfloat[size];
double[]a8=newdouble[size];
String[]a9=newString[size];
Arrays.fill(a1,true);//a1:
[true,true,true,true,true]
Arrays.fill(a2,(byte)2);//a2:
[2,2,2,2,2]
Arrays.fill(a3,'三');//a3:
[三,三,三,三,三]
Arrays.fill(a4,(short)4);//a4:
[4,4,4,4,4]
Arrays.fill(a5,5);//a5:
[5,5,5,5,5]
Arrays.fill(a6,6);//a6:
[6,6,6,6,6]
Arrays.fill(a7,7);//a7:
[7,7,7,7,7]
Arrays.fill(a8,8);//a8:
[8,8,8,8,8]
Arrays.fill(a9,"a9");//a9:
[a9,a9,a9,a9,a9]
Arrays.fill(a9,1,3,”Hi”);//a9:
[a9,Hi,Hi,a9,a9]
//fillrange:
[1,3)
可以指定填充位置的方法说明:
publicstaticvoidfill(int[] a,
int fromIndex,
int toIndex,
int val)
将指定的int值分配给指定int型数组指定范围中的每个元素。
填充的范围从索引fromIndex(包括)一直到索引toIndex(不包括)。
(如果fromIndex==toIndex,则填充范围为空。
)
参数:
a-要填充的数组
fromIndex-要使用指定值填充的第一个元素的索引(包括)
toIndex-要使用指定值填充的最后一个元素的索引(不包括)
val-要存储在数组所有元素中的值
抛出:
IllegalArgumentException-如果fromIndex>toIndex
ArrayIndexOutOfBoundsException-如果fromIndex<0或toIndex>a.length
1.3复制数组—System.arraycopy()
使用System.arraycopy()方法复制数组,比用for循环复制要快很多。
int[]a1=newint[5];
int[]a2=newint[8];
Arrays.fill(a1,47);//a1:
[47,47,47,47,47]
Arrays.fill(a2,99);//a2:
[99,99,99,99,99,99,99,99]
System.arraycopy(a1,0,a2,0,a1.length);//a2:
[47,47,47,47,47,99,99,99]
//填充复制Object
Integer[]a3=newInteger[4];
Integer[]a4=newInteger[8];
Arrays.fill(a3,newInteger(47));//a3:
[47,47,47,47]
Arrays.fill(a4,newInteger(99));//a4:
[99,99,99,99,99,99,99,99]
System.arraycopy(a3,0,a4,a4.length/2,a3.length);
//a4:
[99,99,99,99,47,47,47,47]
方法使用说明:
publicstaticvoidarraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
从src引用的源数组到dest引用的目标数组,数组组件的一个子序列被复制下来。
被复制的组件的编号等于length参数。
源数组中位置在srcPos到srcPos+length-1之间的组件被分别复制到目标数组中的destPos到destPos+length-1位置。
参数:
src-源数组。
srcPos-源数组中的起始位置。
dest-目标数组。
destPos-目标数据中的起始位置。
length-要复制的数组元素的数量。
抛出:
IndexOutOfBoundsException-如果复制会导致对数组范围以外的数据的访问。
ArrayStoreException-如果因为类型不匹配而使得无法将src数组中的元素存储到dest数组中。
NullPointerException-如果src或dest为null。
1.4数组的比较—Arrays.equals()
使用Arrays类的equals()方法比较数组。
数组相等的条件时元素个数必须相等,并且对应位置的元素也相等,这可以通过对每一个元素使用equals()做比较来判断。
对于基本类型,需要使用基本类型的包装类的equals()方法,例如,对于int类型使用Integer.equals()作比较。
int[]a1=newint[5];
int[]a2=newint[5];
Arrays.fill(a1,47);//a1:
[47,47,47,47,47]
Arrays.fill(a2,47);//a2:
[47,47,47,47,47]
System.out.println(Arrays.equals(a1,a2));//output:
true
a1[0]=5;//a1:
[5,47,47,47,47]
System.out.println(Arrays.equals(a1,a2));//output:
false
String[]s1=newString[5];
Arrays.fill(s1,"Hi");//s1:
["Hi","Hi","Hi","Hi","Hi"]
String[]s2={"Hi","Hi","Hi","Hi","Hi"};
System.out.println(Arrays.equals(s1,s2));//output:
true
1.5数组的排序—Arrays.sort()
1.5.1基本类型数组的排序
基本类型数组的排序,可以直接使用Arrays的sort()方法,按照数字升序进行排序。
对String数组排序时,排序算法依据词典顺序排序,大写字母开头的词都会放在前面,然后才是小写字母开头的词。
static voidsort(int[] a)//对指定的int型数组按数字升序进行排序。
static voidsort(int[] a,int fromIndex,int toIndex)
//对指定int型数组的指定范围按数字升序进行排序。
范例:
int[]a={2,4,1,3,6,5};//排序前:
a:
[2,4,1,3,6,5]
Arrays.sort(a);//排序后:
a:
[1,2,3,4,5,6]
//对于字符串数组是按照“自然顺序”排序,即“字典顺序”
String[]s={"cannon","a","abuse","accurate","desk","block"};
Arrays.sort(s);//排序后:
s:
[a,abuse,accurate,block,cannon,desk]
Arrays.sort(s,3,6);//部分排序:
s:
[cannon,a,abuse,accurate,block,desk]
Arrays.sort(s,Collections.reverseOrder());//指定比较器Comparator
//逆序排序:
s:
[desk,cannon,block,accurate,abuse,a]
1.5.2对象数组的排序
针对对象数组中对象进行排序,首要解决的问题是,需要一个可以在两个对象之间进行比较的方法。
Java有两种方式提供比较功能。
第一种是实现java.lang.Comparable接口。
即被比较的对象的类型需要实现Comparable接口,覆盖接口中的CompareTo()方法。
Comparable接口定义如下:
publicinterfaceComparable{
intcompareTo(T o);
}
如果待排序的对象所属的类型实现了上述接口,那么,在调用Arrays类型的以下两种排序方法时,sort会自动去调用CompareTo()方法,完成两个对象之间的比较,最终完成数组的排序。
static voidsort(Object[] a)//根据元素的自然顺序对指定对象数组按升序进行排序。
static voidsort(Object[] a,int fromIndex,int toIndex)//根据元素的自然顺序对指定对象数组的指定范围
//按升序进行排序。
使用范例:
importjava.util.*;
classElementClassimplementsComparable
{
privateintnumber;
publicElementClass(intnum){
number=num;
}
publicintgetNumber(){
returnnumber;
}
publicintcompareTo(Objectobj){
return(number>((ElementClass)obj).number)?
1:
(number==((ElementClass)obj).number?
0:
-1);
}
}
classCompareTest
{
publicstaticvoidmain(String[]args)
{
Randomrand=newRandom();
ElementClass[]ec=newElementClass[5];
for(inti=0;iec[i]=newElementClass(rand.nextInt(10));
System.out.print(ec[i].getNumber()+"");//排序前:
[37952]
}
System.out.println();
Arrays.sort(ec);//sort()方法会自动调用ElementClass实现的compareTo函数
for(inti=0;iSystem.out.print(ec[i].getNumber()+"");//排序后:
[23579]
}
}
第二种是实现java.util.Comparator接口。
得到一个可以对数组中对象进行比较的比较器,然后把这个比较器传递给sort()方法,完成比较与最终的数组排序(基本类型的数组无法用Comparator进行排序)。
Comparator接口定义如下:
publicinterfaceComparator{
intcompare(T o1,T o2);//比较用来排序的两个参数。
booleanequals(Object obj);//指示某个其他对象是否“等于”此Comparator。
}
Comparator接口中的函数需要同时实现。
假设实现的比较器名为MyComparator,其实现形式如下:
publicMyComparatorimplementsComparator{
publicintcompare(T o1,T o2){……}
publicbooleanequals(Object obj){……}
}
有了比较器之后,调用下面的sort()方法完成比较与排序:
staticvoidsort(T[] a,newMyComparator)//根据指定比较器产生的顺序对指定对象数组进行排序。
staticvoidsort(T[] a,int fromIndex,int toIndex,newMyComparator)//根据指定比较器产生的顺序对指
//定对象数组的指定范围进行排序。
使用范例:
importjava.util.*;
classElementClass
{
privateintnumber;
publicElementClass(intnum){
number=num;
}
publicintgetNumber(){
returnnumber;
}
}
classMyComparatorimplementsComparator
{
publicintcompare(Objectobj1,Objectobj2){
intnum1=((ElementClass)obj1).getNumber();
intnum2=((ElementClass)obj2).getNumber();
return(num1>num2?
1:
(num1==num2?
0:
-1));
}
}
classCompareTest
{
publicstaticvoidmain(String[]args)
{
Randomrand=newRandom();
ElementClass[]ec=newElementClass[5];
for(inti=0;iec[i]=newElementClass(rand.nextInt(10));
System.out.print(ec[i].getNumber()+"");//排序前:
[91765]
}
System.out.println();
Arrays.sort(ec,newMyComparator());//将比较器传递给sort进行排序
for(inti=0;iSystem.out.print(ec[i].getNumber()+"");//排序后:
[15679]
}
}
1.6在已排序的数组中查找—Arrays.binarySearch()
如果数组已经排好序了,可以使用Arrays.binarySearch()执行快速查找。
对未排序的数组使用binarySearch()的结果不可预料。
如果找到了目标,Arrays.binarySearch()的返回值等于或大于0。
否则,返回负值,表示为了保持数组的排序状态,此目标元素应该插入的位置。
这个负值的计算方式是:
-(插入点)–1
“插入点”是指,第一个大于查找对象的元素在数组中的位置,如果所有数组元素都小于要查找的对象,“插入点”就等于a.size()。
如果数组包含重复的元素,则无法保证找到的是哪一个。
此算法并不是为包含重复元素的数组专门设计的,但是仍然可用。
如果使用Comparator排序某个对象数组,在使用binarySearch()时必须提供同样的Comparator。
static intbinarySearch(Object[] a,Object key)//使用二分搜索法来搜索指定数组,以获得指定对象。
static intbinarySearch(Object[] a,int fromIndex,int toIndex,Object key)//使用二分搜索法来搜索指定数
//组的范围,以获得指定对象。
例如:
importjava.util.*;
classElementClass
{
privateintnumber;
publicElementClass(intnum){
number=num;
}
publicintgetNumber