JAVA萌新学习day14 常用类和异常

JAVA萌新学习day14 常用类和异常
一.
二.

ArrayDemo

package com.qf.java1904.day14;

public class ArrayDemo {
    public static void main(String[] args) throws Exception{
        System.out.println("ArrayDemo.main");


        StudentManager.arrayListTest();
    }
}

Student.java Level

package com.qf.java1904.day14;

//声明一个枚举,枚举类型实际也是calss,但它里面一般只有常量;
// static final Level.SMALL
enum Level{
    SMALL,MIDDLE,LARGE
}

public class Student implements Comparable <Student>{
    private int number;
    private String name;
    private Level level;

    public Student(){
        this(0,"",Level.SMALL);
    }

    public Student(int number, String name ,Level level){
        this.number = number;
        this.name = name;
        this.level = level;
    }

    public String toString(){
        String info = "";
        switch(level){
            case LARGE:
                info += "我是大学生";
                break;
            case MIDDLE:
                info += "我是中学生";
                break;
            case SMALL:
                info += "我是小学生";
                break;
            default:
                break;
        }
        info += "名字:" + this.name + "学号:" + this.number;
        return info;
    }

    public boolean compare(Student other){
        if(this.number > other.number){
            return true;
        }
        return false;
    }


    //
    @Override
    public int compareTo(Student o) {

        //this.name.compareTo(o.name)

        return this.number - o.number;
    }
}

Student.java Student

ackage com.qf.java1904.day14;

//声明一个枚举,枚举类型实际也是calss,但它里面一般只有常量;
// static final Level.SMALL
enum Level{
    SMALL,MIDDLE,LARGE
}

public class Student implements Comparable <Student>{
    private int number;
    private String name;
    private Level level;

    public Student(){
        this(0,"",Level.SMALL);
    }

    public Student(int number, String name ,Level level){
        this.number = number;
        this.name = name;
        this.level = level;
    }

    public String toString(){
        String info = "";
        switch(level){
            case LARGE:
                info += "我是大学生";
                break;
            case MIDDLE:
                info += "我是中学生";
                break;
            case SMALL:
                info += "我是小学生";
                break;
            default:
                break;
        }
        info += "名字:" + this.name + "学号:" + this.number;
        return info;
    }

    public boolean compare(Student other){
        if(this.number > other.number){
            return true;
        }
        return false;
    }


    //
    @Override
    public int compareTo(Student o) {

        //this.name.compareTo(o.name)

        return this.number - o.number;
    }
}

package com.qf.java1904.day14;

import com.qf.java1904.day13.StudyDemo;

import java.io.PrintStream;
import java.util.*;

public class StudentManager {





    public static void arrayListTest(){

        try {
            //用指定文件创建一个打印流对象
            PrintStream ps = new PrintStream("e:\\log.txt");
            //修改系统标准输出流
            System.setOut(ps);
        }
        catch (Exception e){
            System.out.println("StudentManager.arrayListTest");
        }

        //代码块中可能发生的异常代码
        try{
            String a = null;
            a.charAt(0);
        }
        //catch代码块中写处理异常的代码(括号内代表异常的类型)
        catch(NullPointerException e){
            //如果不想处理直接向外抛

            //根据捕获的异常类型,程序做不同处理
            e.printStackTrace();
            System.out.println("StudentManager.arrayListTest" + e.getMessage());
        }



        //创建一个ArrayList对象,通过泛型指定只能保存字符串
        ArrayList<String> strList = new ArrayList<>();

        //增 删 改 查
        //向list中增加元素,使用add()方法
        for (int i = 0; i < 10; i++) {

            String str = StudyDemo.randomString(10);

            if(i == 5){
                strList.add("java");
            }

            strList.add(str);

        }
        //从list上获得元素,用get()方法
        for (int i = 0; i < 10; i++) {
            String str = strList.get (i);

            System.out.println("第" + i + "个元素是:" + str);
        }

        //从集合中查找指定元素第一次出现的位置
        int index = strList.indexOf("java");
        System.out.println("java出现的位置是:" + index);
        if(index != -1){
            //删除指定位置的元素
            strList.remove(index);
            //strList.remove("java");
        }
        //遍历集合
        //获得集合的迭代器
        Iterator<String> it = strList.iterator();
        //检查是否有可用对象
        while(it.hasNext()){
            //获得当前的可用对象
            String str = it.next();
            System.out.println(str);

        }
        System.out.println("-------------------------");

        //修改集合中索引为五的元素的值
        strList.set(5 ,"1904");

        for(Iterator<String> it1 = strList.iterator();it1.hasNext();){
            String str = it1.next();
            System.out.println(str);
        }
        System.out.println("-------------------------");

        //截取子集和
        List<String> subList = strList.subList(2,6);

        for(Iterator<String> it1 = strList.iterator(); it1.hasNext();){
            String str = it1.next();
            System.out.println(str);
        }
        System.out.println("-------------------------");

        //反向遍历
        ListIterator<String> listIterator = subList.listIterator();

        //将迭代器移到集合结尾
        while(listIterator.hasNext()){
            listIterator.next();
        }
        while (listIterator.hasPrevious()){
            System.out.println("StudentManager.arrayListTest" + listIterator.previous());
        }
        System.out.println("-------------------------");








    }

    public static void test() throws Exception {
        Student[] students = new Student[10];

        Random random  = new Random();

        for (int i = 0; i < students.length; i++) {
            int number = random.nextInt(100);
            String name = StudyDemo.randomString(8);
            Level level = Level.SMALL;

            Student student = new Student(number,name,level);

            students[i] = student;
        }
        //调用系统数组类的静态方法排序我们的自定义对象数组
        Arrays.sort(students);

        /*
        1.我们要调用对象的方法,首先需要知道什么
        2.一定要知道对象类型
        3.排序:如果你自己写一个方法排序自己写的类对象,你明确知道如何比较自己两个对象的大小
        4.现在不是我们自己排序,交给系统排序
            4.1第一系统不知道你的对象类型
            4.2它定义一个泛型参数来解决参数类型不确定的问题
            4.3第二系统不知道你的对象有何方法
            4.4系统定个比较接口(定义了一个比较方法),系统只要知道排序的对象是否实现,这个接口,就能
            知道你自定义对象如何比较(就是调用对象自己的比较方法)

        */

        //使用增强for循环,遍历数组
        //和正常循环的区别在于,没有循环变量
        //语法 变量类型 变量名 : 数组名
        //系统自动将数组每一个元素赋值给变量一次
        for(Student s: students){
            System.out.println("StudentManager.test" + s);
        }


        //异常

        //可能发生异常的代码,如果我们正常写处理的代码,程序就会崩溃
        //try{} catch(){}捕获异常,使我们的程序更加简装,容错性更强,更稳定。

        try{
            students[11] = new Student();
        }
        catch(Exception e){
            System.out.println("数组越界");
            System.out.println("我无能为力,交给别人处理");
            throw new Exception("数组越界,请帮我处理");
        }

    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值