1、复制数组
Arrays提供了静态方式System.arraycopy,例子如下:
public class CopyArrays {
public static void main(String[] args){
int[] i = new int[5];
int[] j = new int[15];
//填充数组
Arrays.fill(i, 10);
Arrays.fill(j, 20);
System.out.println(Arrays.toString(j));
//使用System.arraycopy对i数据进行拷贝复制
System.arraycopy(i, 0, j, 0, 3);
System.out.println(Arrays.toString(j));
}
}
结果:[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
[10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
前三个进行了拷由复制
2、数组元素排序比较
public class CompareArray implements Comparable
private int i;
private int j;
public CompareArray(int i,int j){
this.i= i;
this.j = j;
}
@Override
public int compareTo(CompareArray o) {
// TODO Auto-generated method stub
return i
public String toString(){
return "[i="+i+",j="+j+"]";
}
public static void main(String[] args){
Random r = new Random(48);
CompareArray[] compareArray = new CompareArray[10];
for(int i=0;i<10;i++){
compareArray[i]= new CompareArray(r.nextInt(100),r.nextInt(100));
}
System.out.println(Arrays.toString(compareArray));
Arrays.sort(compareArray);
System.out.println(Arrays.toString(compareArray));
}
}
结果:
[[i=68,j=16], [i=31,j=44], [i=77,j=11], [i=22,j=0], [i=87,j=94], [i=83,j=3], [i=40,j=4], [i=62,j=72], [i=69,j=84], [i=26,j=82]]
[[i=22,j=0], [i=26,j=82], [i=31,j=44], [i=40,j=4], [i=62,j=72], [i=68,j=16], [i=69,j=84], [i=77,j=11], [i=83,j=3], [i=87,j=94]]
3、数组查找
如果数组已经是排好序的,可以使用Arrays.binarySearch()进行快速查找,