Java语法基础之数组(一维数组和二维数组)、异常的使用

第四章 数组

4.1 一维数组

  • 定义:
    • 一段连续的内存空间,线性序列,多个相同数据类型数据的有序集合(存储多个数据)
  • 特点:
    • 引用数据类型
    • 是定长的,长度一旦确定不可改变
    • 存储的多个数据类型相同
    • 有序的,有索引
  • 索引 :
    • 连续的内存空间中每一个小空间的序号
    • 从0开始,每次+1
    • 每个数组的第一个空间索引: 0
    • 每个数组的最后一个空间索引: 数组名.length-1
  • 数组长度:
    • 数组名.length
  • 操作数组中的数据:
    • 根据索引操作
    • 数组名[索引]
  • 数组的定义语法:
    • 声明 :
    • 数据类型[] 数组名; ->推荐
      数据类型 数组名[];
      • 数据类型 : 规定存储的数据的类型
    • 初始化:
      • 动态初始化 : 先创建数组,后赋值
        • 数据类型[]数组名 = new 数据类型[长度];
      • 静态初始化 : 创建数组的同时赋值
        • 数据类型[] 数组名 = new 数据类型[] {值列表};
        • 数据类型[] 数组名 = {值列表};(省略new的写法,不推荐)
public class Class001_Array {
    public static void main(String[] args) {
        // 声明
        int[] arr1;

        //动态初始化
        arr1 = new int[3];

        //静态初始化
        double[] arr2 = new double[]{1.1,2.2,3.3,4.4};
        String[] arr3 = {"你好","中国"};

        //为数组负债
        arr1[0] = 101;
        arr1[1] = 102;
        arr1[2] = 103;

        //获取数组中的数据
        System.out.println(arr1[0]);
        System.out.println(arr1[1]);
        System.out.println(arr1[2]);

        System.out.println(arr2[0]);
        System.out.println(arr2[1]);
        System.out.println(arr2[2]);
        System.out.println(arr2[3]);

        System.out.println(arr3[0]);
        System.out.println(arr3[1]);

        //变量重新赋值, arr1引用指向的不同的数组
        arr1 = new int[5];
        arr1 = new int[]{1,2,3};
        //arr1 = {1,2,3};
    }
}

  • 数组的遍历 :

    • 普通for循环
      条件i是索引,需要根据索引操作数组中的数据

      • 普通for循环遍历的是索引,操作索引,使用索引,根据索引操作数组中的数据(通过索引来确定数组中的内容)
    • 增强for循环|for…each
      for(数据类型 变量名:数组名|集合名){
      变量名 : 存储数组中的每一个数据
      }

      • 增强for只能从前到后的获取每一个数据,但是不能操作使用索引(直接遍历所有的数组内容)
public class Class002_Each {
    public static void main(String[] args) {
        char[] arr = {'a','b','c','d'};
        //for : i作为索引
        for(int i=0;i<=arr.length-1;i++){
            System.out.println(arr[i]);
        }

        //for..each
        for(char ch:arr){
            System.out.println(ch);
        }

    }
}

4.2 二维数组(了解)

二维数组,是数组中放数组,

  • 声明
    数据类型[][] 数组名; -> 推荐
    数据类型 数组名[][];
    数据类型[] 数组名[];

  • 初始化

    • 动态初始化 : 先创建数组,后赋值
      • 数据类型[][] 数组名 = new 数据类型[外层的二维数组的长度] [内层一维的长度];
      • 数据类型[][] 数组名 = new 数据类型[外层的二维数组的长度] [];
    • 静态初始化 : 创建数组的同时赋值
      • 数据类型[][] 数组名 = new 数据类型[][]{{1,2,3},{4,5},{6}…};
      • 数据类型[][] 数组名 = {{1,2,3},{4,5},{6}…};
  • 操作数组中的数据:

    • 数组名[外层二维索引] [内层一维索引]
public class Class006_Array {
    public static void main(String[] args) {
        //动态初始化
        int[][] arr1 = new int[3][2];

        //为二维数组赋值
        arr1[0][0] = 1;
        arr1[0][1] = 2;
        arr1[1][0] = 3;
        arr1[1][1] = 4;
        arr1[2][0] = 5;
        arr1[2][1] = 6;

        //获取
        System.out.println(arr1[0][0]);
        System.out.println(arr1[0][1]);
        System.out.println(arr1[1][0]);
        System.out.println(arr1[1][1]);
        System.out.println(arr1[2][0]);
        System.out.println(arr1[2][1]);

        double[][] arr2 = new double[2][];
        arr2[0] = new double[2];
        arr2[0][0] = 0.1;
        arr2[0][1] = 0.2;
        arr2[1] = new double[]{0.3,0.4,0.5};

        System.out.println(arr2[0][0]);
        System.out.println(arr2[0][1]);
        System.out.println(arr2[1][0]);
        System.out.println(arr2[1][1]);
        System.out.println(arr2[1][2]);

        //静态初始化
        //char[][] arr3 = new char[][]{{'1','2','3'},{'4','5'},{'6'}};
        char[][] arr3 = {{'1','2','3'},{'4','5'},{'6'}};
        System.out.println(arr3[0][0]);
        System.out.println(arr3[0][1]);
        System.out.println(arr3[0][2]);
    }
}

  • 二维数组遍历 :
    • 双重for循环嵌套
      普通for嵌套普通
      普通for嵌套增强
      增强for嵌套普通
      增强for嵌套增强
public class Class007_Array {
    public static void main(String[] args) {
        int[][] arr = {{3,2,1},{7,8},{9}};

        //普通for嵌套增强
        //i 作为二维数组的索引
        for(int i=0;i< arr.length;i++){
            for(int num:arr[i]){
                System.out.println(num);
            }
        }

        //增强for嵌套增强
        for(int[] array:arr){
            for(int i:array){
                System.out.println(i);
            }
        }

        System.out.println(arr[3]);
    }
}

第五章 异常

5.1 异常

程序运行时,发生的不被期望的事件,它阻止了程序按照程序员的预期正常执行,这就是异常。“程序生病了”。

  • 异常体系 :
    Throwable
    /
    Error Exception
    /
    Runtime Checked
    Exception Exception

    • Error : 错误一般为虚拟机生成并脱出的,不由程序猿管理
    • RuntimeException : 运行时异常
      运行时期锁产生的异常
      一般通过增强程序健壮性的代码处理 if
    • CheckedException : 检查时异常|编译时异常
      发生在编译期间
      只能通过异常处理方案处理
      如果不处理程序无法运行
  • 常见的运行时异常:

    1.空指针异常 NullPointerException
    2.数组索引越界异常 ArrayIndexOutOfBoundsException
    字符串索引越界异常 StringIndexOutOfBoundsException
    索引越界异常 IndexOutOfBoundsException
    3.数学异常 ArithmeticException
    4.类型转换异常 ClassCastException
    5.数字转格式异常 NumberFormatException

  • 注意:

    • 如果一旦遇到异常,如果不处理程序无法继续向下执行
public class Class001_Exception {
    public static void main(String[] args) {
        //System.out.println(5/0);

        String str = null;
        if(str!=null){
            System.out.println(str.length());
        }
        str = "123abc";

        System.out.println(Integer.valueOf(str));

        //System.out.println(str.charAt(5));

        int[] arr = {1,2,3};
        System.out.println(arr[3]);

    }
}
  • 异常处理方案 :

    1.异常抛出 throws
    把异常抛出到上一层处理
    2.异常捕获

       try {
                    有可能出现异常的代码段;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                } ...
                  catch (Exception e) { --> 接盘侠
                    e.printStackTrace();
                } finally{
                    一般为资源的关闭等等
                }
    
    • 如果try中的代码一旦出现异常,try后面的代码不执行,直接执行catch进行捕获
    • 从上到下的判断catch,哪一个catch能够捕获当前异常对象,就执行对应catch后面的{}中的代码
    • 如果所有的catch都不满足,这个异常最终没有处理,依旧影响执行的继续执行
    • 一个try后面可以跟1~n个catch,且catch从上到下,要求类型范围从小到大
    • finally : 无论try中是否遇到异常,最后都会执行finally中的代码
public class Class002_Exception {
    public static void main(String[] args){
        System.out.println("main方法开始");
        try {
            System.out.println("try开始");
            haha();
            System.out.println("try结束");
        } catch (FileNotFoundException e) {
            System.out.println("出现文件未找到异常喽,处理了");
            e.printStackTrace(); //打印异常的提示信息
        } finally {
            System.out.println("最后的最后,我们都要离开....");
        }
        System.out.println("main方法结束");
    }

    public static void haha() throws FileNotFoundException {
        test();
    }

    public static void test() throws FileNotFoundException {
        InputStream is = new FileInputStream("D://xixi.txt");
    }
}

  • 自定义异常
    • 自定义的异常类型都会直接或者间接的继承自Exception
    • 直接继承自Exception为编译时异常
    • 继承自RuntimeException为运行时异常
    • throw 制造异常
public class Class003_Exception {
    public static void main(String[] args){
        User user = new User();
        user.setName("棒棒糖的爱");
        int age = 25;
        //运行时
        /*if(age>=18 && age<=150){
            user.setAge(age);
        }else{
            user.setAge(18);
        }*/

        //编译时
        try {
            user.setAge(15);
        } catch (AgeException e) {
            e.printStackTrace();
            try {
                user.setAge(18);
            } catch (AgeException ageException) {
                ageException.printStackTrace();
            }
        }

        System.out.println(user);

        System.out.println("main方法结束了...");
    }
}

//年龄不合法异常
//class AgeException extends RuntimeException{ //运行时异常
class AgeException extends Exception{ //编译时异常
    public AgeException() {
    }

    public AgeException(String message) {
        super(message);
    }
}

class User{
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public User() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeException {
        if(age<18 || age>150){
            //制造一个异常
            throw new AgeException(age+"不合法了...");
        }
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值