Java数组的使用详细

    xiaoxiao2022-07-02  111

    package com.liming.array; public class ArrayTest { public static void main(String[] args) { /**方式一:直接使用列表初始化,不使用类型声明*/ int[] aa1 = {123,123,123,123}; String[] ss1 = {"abc","abc","abc"}; /**方式二:使用列表初始化,使用类型声明,并使用new关键字*/ int[] aa2 = new int[]{123,123,123,123}; String[] ss2 = new String[]{"abc","abc","abc"}; /**方式三:直接赋值*/ int[] aa3 = new int[3]; //不初始化数组 int[] aa33 = new int[0];//这是一个空数组 /**方式四:列表赋一个空值*/ int[] aa4 = {}; int[] aa44 = null; /**方式五:多维数组直接赋值*/ String[][] ss5 = {new String[]{},new String[]{}};//数组内赋值数组 空赋值 String[][] ss55 = {{},{}}; //空值 String[][] ss555 = {null,null}; //空值 String[][] ss5555 = {new String[5],new String[5]}; /**方式六:多维数组成员数组赋值*/ String[][] ss6 = {new String[]{"def","def","def"},new String[]{"abc","abc","abc"}}; String[][] ss66 = {new String[3],new String[3]}; ss66[0][1] = "Hello,World"; /**方式七:多维数组赋值*/ String[][] ss7 = new String[3][3]; ss7[0] = new String[]{}; ss7[1] = new String[]{"qwe","rty","zxc","kkk","jjj"};//更改了ss7数组之前的结构 ss7[2] = new String[]{"abc","def","hij"}; ss7[2][2] = "Hell Door"; /**方式八:三维数组赋值*/ String[][][] ss8 = new String[4][3][3]; ss8[0] = new String[][]{}; ss8[1] = new String[3][3]; ss8[2] = new String[][]{new String[2],new String[2],new String[2]}; ss8[2][1] = new String[]{"abc","abc","abc"}; ss8[3] = new String[][]{new String[]{"aaa","bbb","ccc"},new String[]{"ddd","eee","fff"}}; /**方式九:四维数组赋值*/ String[][][][] ss9 = new String[][][][]{}; String[][][][] ss99 = new String[][][][]{new String[][][]{new String[][]{new String[]{"Helo"}}}}; String[][][][] ss999 = new String[4][4][4][4]; ss999[0] = new String[][][]{new String[][]{new String[]{"这是什么鬼"}}}; ss999[1] = new String[3][3][3];//改变了数组之前的结构 ss999[1][2][2][2] = "四维数组的演示"; System.out.println(ss999[1][2][2][2]); } }
    最新回复(0)