数组的快速排序:
import java.util.Arrays;
public class TestArr {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] a = new int[]{2,3,4,5,2,4,5};
Arrays.sort(a);
for (int i : a) {
System.out.println(i+"\t");
}
}
}
数组的复制
1. 用for循环复制
public class Testcopy {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 使用循环语句进行赋值
int [] a = new int []{1,2,3};
int [] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
}
System.out.println("数组b的值为:");
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}
}
}
//用Arrays类的copyOf方法和copyOfRange()方法来实现数组的复制。
int [] c = new int[]{4,5,3,5,6};
System.out.println("数组c的值为:");
for (int i : c) {
System.out.println(i+"\t");
}
int [] d = Arrays.copyOf(c, 3);
System.out.println("数组d的值为");
for ( int j : d) {
System.out.println(j+"\t");
}
使用
copyOfRange()
方法实现数组的复制
Arrays.copyOfRange(数组名, 起始下标,结束下标)
一定要注意,下标是从0开始的
int [] a = {2,3,4,6,4,5};
System.out.println("数组a的值:");
for (int i : a) {
System.out.print(i+"\t");
}
int[] b = Arrays.copyOfRange(a, 2, 4);
System.out.println("\n"+"数组b的值为:");
for (int j : b) {
System.out.print(j+"\t");
}
//使用arrayCopy()复制
//arryCopy(原数组,原数组起始下标,目标数组,目标数组的起始下标,复制长度);
int [] a = new int[]{2,3,4,5,6,2,7};
System.out.print("数组a的元素:");
for (int i : a) {
System.out.print(i+"\t");
}
int []b =new int[a.length];
System.arraycopy(a, 2, b, 0, 5);
System.out.print("\n"+"数组b的元素");
for (int i : b) {
System.out.print(i+"\t");
}
数组的替换
public static void main(String[] args) {
// TODO Auto-generated method stub
/*数组的替换
* fill(数组名,替换的值);
* 或fill(数组名,起始下标,结束下标,替换的值);
*/
int [] a = new int[]{1,2,3,4,5,6,7,8,9};
System.out.print("数组a的元素:");
for (int i : a) {
System.out.print(i+"\t");
}
System.out.print("\n"+"第一次替换:");
Arrays.fill(a, 5);
for (int i : a) {
System.out.print(i+"\t");
}
System.out.print("\n"+"第二次替换:");
Arrays.fill(a, 2,5,0);
for (int i : a) {
System.out.print(i+"\t");
}
}