数组

1.数组静态初始化【应用】

1.1什么是静态初始化

​ 在创建数组时,直接将元素确定

1.2静态初始化格式
  • 完整版格式

    数据类型[] 数组名 = new 数据类型[]{元素1,元素2,...};
    
  • 简化版格式

    数据类型[] 数组名 = {元素1,元素2,...};
    
1.3示例代码
public class ArrayDemo {
    public static void main(String[] args) {
        //定义数组
        int[] arr = {1, 2, 3};

        //输出数组名
        System.out.println(arr);

        //输出数组中的元素
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
    }
}

2数组操作的两个常见小问题【应用】

2.1索引越界异常
  • 出现原因

    public class ArrayDemo {
        public static void main(String[] args) {
            int[] arr = new int[3];
            System.out.println(arr[3]);
        }
    }
    

    数组长度为3,索引范围是0~2,但是我们却访问了一个3的索引。

    程序运行后,将会抛出ArrayIndexOutOfBoundsException 数组越界异常。在开发中,数组的越界异常是不能出现的,一旦出现了,就必须要修改我们编写的代码。

  • 解决方案

    将错误的索引修改为正确的索引范围即可!

2.2空指针异常
  • 出现原因

    public class ArrayDemo {
        public static void main(String[] args) {
            int[] arr = new int[3];
    
            //把null赋值给数组
            arr = null;
            System.out.println(arr[0]);
        }
    }
    

    arr = null 这行代码,意味着变量arr将不会再保存数组的内存地址,也就不允许再操作数组了,因此运行的时候会抛出 NullPointerException 空指针异常。在开发中,数组的越界异常是不能出现的,一旦出现了,就必须要修改我们编写的代码。

  • 解决方案

    给数组一个真正的堆内存空间引用即可!

3.数组遍历【应用】

  • 数组遍历:就是将数组中的每个元素分别获取出来,就是遍历。遍历也是数组操作中的基石。

    public class ArrayTest01 {
    	public static void main(String[] args) {
    		int[] arr = { 1, 2, 3, 4, 5 };
    		System.out.println(arr[0]);
    		System.out.println(arr[1]);
    		System.out.println(arr[2]);
    		System.out.println(arr[3]);
    		System.out.println(arr[4]);
    	}
    }
    

    以上代码是可以将数组中每个元素全部遍历出来,但是如果数组元素非常多,这种写法肯定不行,因此我们需要改造成循环的写法。数组的索引是 0 到 lenght-1 ,可以作为循环的条件出现。

    public class ArrayTest01 {
        public static void main(String[] args) {
            //定义数组
            int[] arr = {11, 22, 33, 44, 55};
    
            //使用通用的遍历格式
            for(int x = 0; x < arr.length; x++) {
                System.out.println(arr[x]);
            }
        }
    }
    

4.数组最值【应用】

  • 最大值获取:从数组的所有元素中找出最大值。

  • 实现思路:

    • 定义变量,保存数组0索引上的元素
    • 遍历数组,获取出数组中的每个元素
    • 将遍历到的元素和保存数组0索引上值的变量进行比较
    • 如果数组元素的值大于了变量的值,变量记录住新的值
    • 数组循环遍历结束,变量保存的就是数组中的最大值
  • 代码实现:

    public class ArrayTest02 {
        public static void main(String[] args) {
            //定义数组
            int[] arr = {12, 45, 98, 73, 60};
            //定义一个变量,用于保存最大值
            //取数组中第一个数据作为变量的初始值
            int max = arr[0];
            //与数组中剩余的数据逐个比对,每次比对将最大值保存到变量中
            for(int x=1; x<arr.length; x++) {
                if(arr[x] > max) {
                    max = arr[x];
                }
            }
            //循环结束后打印变量的值
            System.out.println("max:" + max);
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值