Java中数组的进阶学习

数组进阶

1.二维数组的初始化
静态初始化
int [ ][ ] = new int [ ] [ ] {{1,2,3},{4,5},{6,7,8}}
动态初始化
String [ ] [ ] arr2 = new String [ 3 ] [ 2]
String [ ] [ ] arr3 = new String [ 3 ] [ ]
2.题目
//创建一个长度为6的int型数组,要求数组元素的值都在1到30之间,且是随机赋值。同时,要求元素的值各不相同。

package zifuchuan;

import java.util.Random;

//创建一个长度为6的int型数组,要求数组元素的值都在1到30之间,且是随机赋值。同时,要求元素的值各不相同。
public class Test {
    public static void main(String[] args) {
        int []arr = new int[6];

        for (int i = 0; i <arr.length ; i++) {
            arr[i] = (int)(Math.random()*30) + 1;
            for (int j = 0; j <i ; j++) {
                if (arr[i] == arr[j]){
                    i--;
                    break;
                }
            }
        }

        for (int temp:arr) {
            System.out.println(temp);
        }
    }

}

3.数组的复制和反转

package zifuchuan;


public class Test {
    public static void main(String[] args) {
        int [] arr1 = {1,2,3,4,5,6};
        //数组的复制
        int [] arr2 = new int[arr1.length];
        for (int i = 0; i <arr1.length ; i++) {
            arr2[i] = arr1[i];
        }

/*        for (int temp:arr2) {
            System.out.println(temp);
        }*/

        //数组的反转

        //方法一
/*        for (int i = 0; i < arr1.length / 2 ; i++) {
            int temp = arr1[i];
            arr1[i] = arr1[arr1.length -1 - i];
            arr1[arr1.length - 1 - i] = temp;
        }*/

        //方法二
        for (int i = 0,j = arr1.length -1 ; i < j ; i++,j--) {
            int temp = arr1[i];
            arr1[i] = arr1[j];
            arr1[j] = temp;
        }

        for (int temp:arr1) {
            System.out.println(temp);
        }
    }
}

4.Arrays工具类
二分查找

package zifuchuan;


import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int [] arr = new int[]{2,4,15,28,35};
        int a = Arrays.binarySearch(arr,28);
        System.out.println(a);
    }
}

排序

package zifuchuan;


import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int [] arr = new int[]{36,1,67,13,24};
        Arrays.sort(arr);
        for (int temp:arr) {
            System.out.println(temp);
        }
    }
}

格式化输出

package zifuchuan;


import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int [] arr = new int[]{36,1,67,13,24};
        System.out.println(Arrays.toString(arr));//[36, 1, 67, 13, 24]

    }
}

复制

package zifuchuan;


import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int [] arr = new int[]{36,1,67,13,24};
        int arr1 [] = Arrays.copyOf(arr,8);
        System.out.println(Arrays.toString(arr1));
    }
}

判断两个数组是否相等

package zifuchuan;


import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int [] arr = new int[]{36,1,67,13,24};
        int [] arr1 = arr;
        boolean a = Arrays.equals(arr,arr1);
        System.out.println(a);
    }
}

指定元素填充数组

package zifuchuan;


import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int [] arr = new int[6];
        Arrays.fill(arr,1);
        System.out.println(Arrays.toString(arr));//[1, 1, 1, 1, 1, 1]
    }
}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读