# 二维数组及其拷贝方式

    public static int search(int[] array,int val){
int low = 0;//左下标
int high = array.length-1;//右下标
while(low <= high){
int mid = (low+high)>>>1;//中间下标
if(array[mid] > val){//中间值大于要查找的值
high = mid-1;//右边下标指向中间下标左移一位的位置
}else if(array[mid] < val){//中间值小于要查找的值
low = mid+1;//左边下标指向中间下标右移一位的位置
}else{
return mid;//mid为要查找的数字的下标，返回mid
}
}
return -(low+1);//low  查找的数据的插入点位置  -1 0
}

    public static void rerves(int[] array){
for(int i = 0;i < array.length/2;i++){
int tmp = array[i];
array[i] = array[array.length-1-i];
array[array.length-1-i] = tmp;
}
}

    public static void merge(int[] array1,int[] array2){
int[] array3 = new int[array1.length+array2.length];
for(int i = 0;i < array1.length;i++){//将array1 copy至array3中
array3[i] = array1[i];
}
for(int j = 0;j < array2.length;j++){//将array2 copy至array3中
array3[array1.length+j] = array2[j];
}

for(int k = 0;k < array3.length;k++){//遍历输出array3数组
System.out.print(array3[k]+" ");
}
System.out.println();

}

## 二维数组

        int[][] array4 = new int[2][];
array4[0] = new int[3];
array4[1] = new int[2];
array4[0][0] = 100;

for循环遍历输出二维数组

    public static void show1(int[][] array){
for(int i = 0;i < array.length;i++){
for(int j = 0;j < array[i].length;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}

foreach遍历输出二维数组

    public static void show2(int[][] array){
for( int[] brr: array ){
for(int i:brr){
System.out.print(i+" ");
}
System.out.println();
}
}

    public static void show2(TestArray[][] array){
for(int i = 0;i < array.length;i++){
for(int j = 0;j < array[i].length;j++){
System.out.print(array[i][j].getVal()+" ");
}
System.out.println();
}
}

        int[][] array = {{1,2,3},{4,5,6}};
int[][] array2 = new int[2][3];
for(int i = 0;i < array.length;i++){
for(int j = 0;j < array[i].length;j++){
array2[i][j] = array[i][j];//copy的是值
}
}
show1(array);
System.out.println("=======");
show1(array2);
array2[0][0] = 100;
System.out.println("===改变====");
show1(array);
System.out.println("=======");
show1(array2);

        int[][] array = {{1,2,3},{4,5,6}};
int[][] array2 = new int[2][3];
array2 = array;//copy的是引用地址
show1(array);
System.out.println("=======");
show1(array2);
array2[0][0] = 100;
System.out.println("===改变====");
show1(array);
System.out.println("=======");
show1(array2);

        TestArray[][] t1 = new TestArray[2][3];
for(int i = 0;i < t1.length;i++){
for(int j = 0;j < t1[i].length;j++){
t1[i][j] = new TestArray();
}
}
TestArray[][] t2 = new TestArray[2][3];
for(int i = 0;i < t1.length;i++){
for(int j = 0;j < t1[i].length;j++){
t2[i][j] = t1[i][j];
}
}
show2(t1);
System.out.println("=======");
show2(t2);
t2[0][0].setVal(1000);
System.out.println("===改变====");
show2(t1);
System.out.println("=======");
show2(t2);

        int[][] array = {{1,2,3},{4,5,6}};
int[][] array2 = new int[2][3];
//array2 = array.clone();   每一个一维数组的引用（浅拷贝）
for(int i = 0;i < array.length;i++){
array2[i] = array[i].clone();
}
show1(array);
System.out.println("======");
show1(array2);
array2[0][0] = 1000;
System.out.println("====改变==");
show1(array);
System.out.println("======");
show1(array2);

        TestArray[][] t1 = new TestArray[2][3]
for(int i = 0;i < t1.length;i++){
for(int j = 0;j < t1[i].length;j++){
t1[i][j] = new TestArray();
}
}
TestArray[][] t2 = new TestArray[2][3]
for(int i = 0;i < t1.length;i++){
t2[i] = t1[i].clone();
}
show2(t1);
System.out.println("=======");
show2(t2);
t2[0][0].setVal(1000);
System.out.println("===改变====");
show2(t1);
System.out.println("=======");
show2(t2);

System.arraycopy 拷贝基本类（深拷贝）
Arrays.copyOf() 拷贝基本类（浅拷贝）

        int[][] array = {{1,2,3},{4,5,6}};
int[][] array2 = new int[2][3];
for(int i = 0;i < array.length;i++){
System.arraycopy(array[i],0,array2[i],0,array[i].length);
//array2[i]=Arrays.copyOf(array[i],array2[i].length);(同为深拷贝)
}
show1(array);
System.out.println("======");
show1(array2);
array2[0][0] = 1000;
System.out.println("====改变==");
show1(array);
System.out.println("======");
show1(array2);

System.arraycopy 拷贝引用类（浅拷贝）
Arrays.copyOf() 拷贝引用类（浅拷贝）

        TestArray3[][] t1 = new TestArray3[2][3];
for(int i = 0;i < t1.length;i++){
for(int j = 0;j < t1[i].length;j++){
t1[i][j] = new TestArray3();
}
}
TestArray3[][] t2 = new TestArray3[2][3];
for(int i = 0;i < t1.length;i++){
System.arraycopy(t1[i], 0, t2[i], 0, t1[i].length);
//t2[i] = Arrays.copyOf(t1[i],t1[i].length);（同为浅拷贝）
}
show2(t1);
System.out.println("=======");
show2(t2);
t2[0][0].setVal(1000);
System.out.println("===改变====");
show2(t1);
System.out.println("=======");
show2(t2);

    public static int sum(int a,int b){
return a+b;
}

public static int sum(int a,int b,int c){
return a+b+c;
}

public static int sum(int a,int b,int c,int d){
return a+b+c+d;
}

1. 这种可变参数的写法本质就是一种数组。
2. 方法最多只能接收一个可变参数
3. 当有另外一个或多个参数时,可变参数必须放在最后一个

    public static int sum(int... array){
}