数组的基本使用

代码:

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

/**
 * 数组的基本使用
 */
public class ArrayDemo {

    /**
     * 数组的创建方法
     * ------------------------------------------------------------
     * 1)定值数组
     * 2)定长数组
     */
    @Test
    public void testName1() throws Exception {

        // ==============================定值数组================================= //

        // 定值数组1
        int[] intArr = { 1, 2, 3 };
        Object[] objArr = { "", "", "" };

        // 定值数组2
        int[] intArr2 = new int[] { 1, 2, 3 };
        Object[] objArr2 = new Object[] { "", "", "" };

        // ==============================定长数组================================ //

        // 定长数组1
        int[] intArr3 = new int[3];
        Object[] objArr3 = new Object[3];

        // 定长数组2
        // |- 一维数组
        int[] intArr4 = (int[]) Array.newInstance(int.class, 3);
        Object[] objArr4 = (Object[]) Array.newInstance(Object.class, 3);

        // |- 二维数组
        int[] intArr5 = (int[]) Array.newInstance(int.class, 3, 3);
        Object[] objArr5 = (Object[]) Array.newInstance(Object.class, 3, 3);

        // |- 三维数组
        int[] intArr6 = (int[]) Array.newInstance(int.class, 3, 3, 3);
        Object[] objArr6 = (Object[]) Array.newInstance(Object.class, 3, 3, 3);

        // ...可见创建到N维....
    }

    /**
     * 定值数组作为参数使用
     * ------------------------------------------------------------
     * 类似'{ 1, 2, 3 }、{ "", "", "" }'这样的数组,是不能直接作为参数使用的。
     * 必须先赋值给某个声明为数组的变量,再使用那个变量作为参数(赋值给变量的过程,应该是做了某种封装)。
     * 或、使用new字段创建一个直接赋值的数组对象作为参数。
     */
    @Test
    public void testName2() throws Exception {
        // 定值数组1
        int[] intArr = { 1, 2, 3 };
        Object[] objArr = { "", "", "" };

        // 作为参数时使用
        List<Object> list = new ArrayList<Object>();

        // 用法1:ok
        list.add(intArr);
        list.add(objArr);

        // 用法2:编辑不能通过
        /*
        list.add({ 1, 2, 3 });
        list.add({ "", "", "" });
        */

        // 用法3:ok
        list.add(new int[] { 1, 2, 3 });
        list.add(new Object[] { "", "", "" });

        // 用法4:编辑不能通过
        // test2( { 1, 2, 3 });
    }

    public static void test2(Object[] objs) {
        System.out.println("参数个数:" + objs.length);
        System.out.println(Arrays.toString(objs));
    }

    /**
     * Array的基本用法
     * ----------------------------------------------------------------
     * 使用Array的何种get方法取数据,取决于创建数组时声明的元素类型。
     * 如果创建数组时,声明元素类型为Object,就算存入一个int类型的值,也不能使用getInt方法获取数组中的数据。
     * 只有创建数组时声明的元素类型是int,才可以使用getInt方法获取数组中的数据。
     */
    @Test
    public void testName3() throws Exception {
        Object[] objArr = (Object[]) Array.newInstance(Object.class, 3);

        // 设值
        Array.set(objArr, 0, 1); // 插入一个int型数据
        Array.set(objArr, 1, 1F); // 插入一个Float数据
        Array.set(objArr, 2, ""); // 插入一个String数据

        // 取值
        // |- 错误的取法
        //         会报错:java.lang.IllegalArgumentException: Argument is not an array
        // int obj1 = Array.getInt(objArr, 0);

        // |- 正确的用法
        Object obj2 = Array.get(objArr, 0);
        int num = (int) obj2;
        System.out.println(num); // 1
    }

    /**
     * 关于数组传参的另一个问题
     * --------------------------------------------------------------------------
     * 1){ "1", "2", "3" }这样的值,只能赋值给用数组形式声明的变量。直接赋值给Object是编译不能通过的。
     * 2)Object obj和Object[] objArr虽然引用对象是同一个,但是因为声明的类型不一致,作为参数传递时,有很大差异。
     *         Object obj会作为一个参数进行传递。
     *         Object[] objArr会作为多个参数进行传递。
     */
    @Test
    public void testName4() throws Exception {
        // 定值数组1
        Object[] objArr = { "1", "2", "3" };

        test4(objArr);
        /*
         * 参数个数:3
         * [1, 2, 3]
         */

        // Object obj = { "1", "2", "3" }; // 编译不通过
        Object obj = objArr;
        test4(obj);
        /*
         * 参数个数:1
         * [[Ljava.lang.Object;@e03bb5]
         */
    }

    public static void test4(Object... objs) {
        System.out.println("参数个数:" + objs.length);
        System.out.println(Arrays.toString(objs));
    }

}

 

posted @ 2018-01-12 17:13  迷失之路  阅读(397)  评论(0编辑  收藏  举报