Java数组

数组

public class Array {

    public static void main(String[] args) {

                // 1、静态初始化

        int[] arrayIntA = new int[] { 11, 22, 33, 44, 55 };

        for (int i = 0; i <= 4; i++) {

                        System.out.println("数组值" + "[" + i + "] " + arrayIntA[i]);

        }

        System.out.println('\r');// 换行

        for (int j : arrayIntA) {

            System.out.println("数组值:"+j);

        }

        // 赋值

        arrayIntA[2] = 9;

        // 取值

        System.out.println("arrayIntA的第三个元素是:" + arrayIntA[2]);

        // 2、动态初始化

        String[] arrayStringA = new String[8];

        //System.out.println(arrayStringA);

        System.out.println('\r');// 换行

        // 遍历数组赋值

        for (int i = 0; i < arrayStringA.length;i++ ) {       

            arrayStringA[i] = String.valueOf(i);       

        }

        // 遍历数组取值

        for (int i = 0; i < arrayStringA.length; i++) {

            System.out.print(arrayStringA[i]+"  ");

        }

        System.out.println('\r');// 换行

        int[] aaa = new int[] { 1, 3, 5, 9, 18, 32 };

        for (int b : aaa) {

            System.out.println("aaa的值:" + b);

        }

        for (int m = 0; m < aaa.length; m++) {

            System.out.println("数组aaa["+m+"]的值:"+aaa[m]);

        }

        int[][] arrayA = new int[][] { { 1, 2 }, { 3, 4 }, { 5, 6 } };

        System.out.println("------使用for循环将星期遍历出来------");

        for (int i = 1; i < 8; i++) {

            int weekDay = i;

            switch (weekDay) {

            case 1:            System.out.println("星期一");

                break;

            case 2:

                System.out.println("星期二");

                break;

            case 3:

                System.out.println("星期三");

                break;

            case 4:

                System.out.println("星期四");

                break;

            case 5:

                System.out.println("星期五");

                break;

            case 6:

                System.out.println("星期六");

                break;

            case 7:

                System.out.println("星期日");

                break;

            default:

                System.out.println("无效输入");

            }

        }

        // for循环

        int sum = 0;

        for (int i = 1; i < 101; i++) {

            sum += i;

        }

        System.out.println("for_1加到10的和为 " + sum);

        // while循环

        int i = 1;

        int total = 0;

        while (i < 11) {

            total += i;

            i++;

        }

        System.out.println("while_1加到10的和为 " + total);

        int[] ints = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        for (int x : ints) {

            System.out.print(x);

            System.out.print(",");

        }

        System.out.println('\r');// 换行

        String[] strs = { "string1", "string2", "string3", "string4" };

        for (String str : strs) {

            System.out.print(str);

            System.out.print(",");

        }

    }

}

效果图展示:

Java数组