【Java】Array-简单使用介绍

学而不思则罔,思而不学则殆


介绍

Array作为Java中非常基本的工具类,提供了一些很强大很基础的公共方法。也作为了很多数组工具的基础方法支持者。
在这里插入图片描述
查看方法简介,方法主要分为几类:

分类作用
getxxx获取数组中某个标的元素的值
setxxx设置数组中某个位置的元素值
getLength获取数组的长度
newInstance新建数组(一维,二维三维。。。n维)

测试

测试get

get

        Person[] people = new Person[]{new Person("zy1", 27),
                new Person("zy2", 27),
                new Person("wxf1", 18),
                new Person("wx2", 18)};
        System.out.println(Array.get(people, 0));//Person{name='zy1', age=27}

getBoolean

        boolean[] booleans = new boolean[]{true, false};
        System.out.println(Array.getBoolean(booleans, 0));//true
        System.out.println(Array.getBoolean(booleans, 1));//false

getByte

        byte[] bytes = new byte[]{1, 2, 3, 4};
        System.out.println(Array.getByte(bytes, 0));//1
        System.out.println(Array.getByte(bytes, 1));//2

测试set

    private static void testArraySet() {
        Person[] people = new Person[]{new Person("zy1", 27),
                new Person("zy2", 27),
                new Person("wxf1", 18),
                new Person("wx2", 18)};
        System.out.println(Arrays.toString(people));//[Person{name='zy1', age=27}, Person{name='zy2', age=27}, Person{name='wxf1', age=18}, Person{name='wx2', age=18}]
        Array.set(people, 0, new Person("zy11", 27));
        System.out.println(Arrays.toString(people));//[Person{name='zy11', age=27}, Person{name='zy2', age=27}, Person{name='wxf1', age=18}, Person{name='wx2', age=18}]


        int[] ints = new int[]{1, 2, 3, 4};
        System.out.println(Arrays.toString(ints));//[1, 2, 3, 4]
        Array.set(ints, 0, 1000);
        System.out.println(Arrays.toString(ints));//[1000, 2, 3, 4]


        Integer[] integers = new Integer[]{1, 2, 3, 4};
        System.out.println(Arrays.toString(integers));//[1, 2, 3, 4]
        Array.set(integers, 0, 999);
        System.out.println(Arrays.toString(integers));//[999, 2, 3, 4]
    }

测试getLength

    private static void testArrayGetLength() {
        Person[] people = new Person[]{new Person("zy1", 27),
                new Person("zy2", 27),
                new Person("wxf1", 18),
                new Person("wx2", 18)};
        System.out.println(Array.getLength(people));//4

        System.out.println(Array.getLength(new int[]{1, 2, 3, 4}));//4
        System.out.println(Array.getLength(new byte[]{1, 2, 3, 4}));//4
        System.out.println(Array.getLength(new char[]{1, 2, 3, 4}));//4
        System.out.println(Array.getLength(new long[]{1, 2, 3, 4}));//4
    }

测试newInstance

一维数组

        //创建数组
        Person[] people = (Person[]) Array.newInstance(Person.class, 8);
        System.out.println(people);//[Ltest.ArrayTest$Person;@5e2de80c
        System.out.println(people.length);//8
        System.out.println(Arrays.toString(people));//[null, null, null, null, null, null, null, null]
        people[6] = new Person("wxf", 18);
        System.out.println(Arrays.toString(people));//[null, null, null, null, null, null, Person{name='wxf', age=18}, null]


        Object[] objects = (Object[]) Array.newInstance(Object.class, 4);
        System.out.println(objects);//[Ljava.lang.Object;@1d44bcfa
        System.out.println(objects.length);//4
        objects[3] = new Person("wxf", 18);
        System.out.println(Arrays.toString(objects));//[null, null, null, Person{name='wxf', age=18}]

二维数组

        //创建二维数组
        Object[][] ss = (Object[][]) Array.newInstance(Object.class, 3, 3);
        System.out.println(ss);//[[Ljava.lang.Object;@266474c2
        System.out.println(Arrays.deepToString(ss));//[[null, null, null], [null, null, null], [null, null, null]]

多维数组

    //创建多维数组
        int[][][] ss1 = (int[][][]) Array.newInstance(int.class, 2, 3, 4);
        System.out.println(ss1);//[[[I@63947c6b
        System.out.println(Arrays.deepToString(ss1));//[[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页