Java——数组

1 数组声明创建

在这里插入图片描述
在这里插入图片描述

public class Demo1 {
    //变量类型  变量的名字
    public static void main(String[] args) {
        //两种方法
        int[] nums; //首选方法 1.声明一个数组
        //int nums2[];

        //2.创建一个数组
        nums = new int[5];
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;
        System.out.println(nums[0]);

        //计算所有元素的和
        int sum = 0;
        for (int i = 0; i < nums.length; i++){
            sum += nums[i];
        }
        System.out.println(sum);
    }
}

在这里插入图片描述

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

        //动态初始化:包含默认初始化,里面默认是0或null
        int[] b = new int[10];
        b[0] = 1;
        b[1] = 2;

        System.out.println(b[0]);
    }
}

数组的四个基本特点:
在这里插入图片描述
在这里插入图片描述

2 数组使用

在这里插入图片描述

package com.milk.array;

public class Demo4 {
    public static void main(String[] args) {
        int[] arrays = {1, 2, 3, 4, 5};
        int[] reverse = reverse(arrays);
        //foreach打印整个数组,不能索引下标
        for (int array: reverse) {
            System.out.println(array);
        }
    }
    // 反转数组public
    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; //数组作为返回值
    }
}

3 多维数组

在这里插入图片描述

package com.milk.array;

public class Demo5 {
    public static void main(String[] args) {
        int[][] array = {{1,2},{3,4},{5,6}};
        System.out.println(array[1][1]);
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.println(array[i][j]);
            }
        }
    }
}

4 Arrays类

在这里插入图片描述

package com.milk.array;

import java.util.Arrays;

public class Demo6 {
    public static void main(String[] args) {
        int[] a = {1,2,3,4,100,900,212,5,7,9,3222};
        Arrays.sort(a);  ///对数组进行排序:升序
        System.out.println(Arrays.toString(a)); //需要使用toString方法才能正确输出

        Arrays.fill(a, 2, 4, 0); //索引2-4会被填充成0,左闭右开
        System.out.println(Arrays.toString(a));
    }
}

冒泡排序:

package com.milk.array;

import java.util.Arrays;
//冒泡排序
public class Demo7 {
    public static void main(String[] args) {
        int[] a = {1,2,9,3,5,2,5,999,333,456,123};
        int[] sort = sort(a);
        System.out.println(Arrays.toString(a));
    }
    public static int[] sort(int[] a){
        int temp = 0;
        //外层循环,判断要走多少次
        for (int i = 0; i < a.length; i++) {
            //内层循环,进行一轮比较,比较两个数的大小,如果第一个数比第二个数大,则交换位置
            for (int j = 0; j < a.length-1-i; j++) {
                if (a[j+1] < a[j]){
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        return a;
    }
}

5 稀疏数组

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值