其他分享
首页 > 其他分享> > 数组重点解析

数组重点解析

作者:互联网

数组的创建

package com.wtdm.array;

public class ArrayDemo01 {
    public static void main(String[] args) {
        int[] nums;//1.声明一个数组

        nums = new int[10];//2.创建一个数组
        //3.给数组元素中赋值
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;
        nums[5] = 6;
        nums[6] = 7;
        nums[7] = 8;
        nums[8] = 9;
        nums[9] = 10;

        //计算所有元素的和
        int sum = 0;
        //获取数组长度:arrays.length
        for(int i = 0;i<nums.length;i++){
            sum = sum + nums[i];
        }
        System.out.println("总和为:"+sum);
    }
}

数组初始化

package com.wtdm.array;

public class ArrayDemo02 {
    public static void main(String[] args) {
        //静态初始化:创建 + 赋值
        int[] a = {1,2,3,4,5,6,7,8,9};
        System.out.println(a[0]);

        //动态初始化
        int[] b = new int[10];
        b[0] = 10;
        //b[1]、b[2]...没被赋值也有默认值
        System.out.println(b[0]);
    }
}

数组的使用

package com.wtdm.array;

public class ArrayDemo03 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};
        //打印全部的数组元素
        for (int i = 0; i < arrays.length; i++) {
            System.out.println(arrays[i]);
        }
        //计算所有元素的和
        int sum = 0;
        for(int i = 0;i<arrays.length;i++){
            sum = sum + arrays[i];
        }
        System.out.println("sum="+sum);
        //查找最大元素
        int max = arrays[0];
        for (int i = 1; i < arrays.length ; i++) {
            if (arrays[i]>max){
                max = arrays[i];
            }
        }
        System.out.println("max="+max);
    }
}

package com.wtdm.array;

public class ArrayDemo04 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};
//        //JDK1.5,没有下标
//        for(int array:arrays){
//            System.out.println(array);
//        }

//        printArray(arrays);

        int[] reverse = reverse(arrays);
        printArray(reverse);
    }
    //反转数组
    public static int[] reverse(int[] arrays){
        int[] result = new int[arrays.length];

        //反转操作
        for (int i = 0, j=result.length-1;i < arrays.length; i++,j--) {
            result[j] = arrays[i];
        }
        return result;
    }


    //打印数组元素
    public static void printArray(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }
    }

}

二维数组

package com.wtdm.array;

public class ArrayDemo05 {
    public static void main(String[] args) {
        //[4][2]
        /*
         * 1,2   array[0]
         * 2,3   array[1]
         * 3,4   array[2]
         * 4,5   array[3]
         * */
        int[][] array = {{1,2},{2,3},{3,4},{4,5}};

        for (int i = 0; i < array.length; i++){
            for (int j = 0; j < array[i].length; j++) {
                System.out.println(array[i][j]);
            }
        }
    }
    //打印数组元素
    public  static  void printArray(int[] arrays){
        for(int i = 0; i < arrays.length;i++){
            System.out.println(arrays[i]+" ");
        }
    }

}

标签:nums,int,arrays,数组,array,解析,重点,public,out
来源: https://blog.csdn.net/qq_46372463/article/details/122453699