API_Exception

Map
Map是集合容器,存放的元素由键与值两部分组成,通过键可以找对所对应的值,键和值必须是引用类型,键唯一不能重复,没有顺序
HashMap是Map的一个实现类
public class MapDemo {
    //1.无序 2.异步 效率高 3.键值是唯一的,值是可以重复的  4.只能是引用类型

    public static void main(String[] args) {
        Map map = new HashMap();//<>不写默认object

        //存储元素  put()
        map.put(1, "张");
        map.put(2, "庄");
        System.out.println(map);//{1=张, 2=庄}
        map.put(1, "丽");
        System.out.println(map);//{1=丽, 2=庄}  键值重复被覆盖
        map.put(3, "张");
        map.put(4, "张");
        System.out.println(map);//{1=丽, 2=庄, 3=张, 4=张} value值可以重复
        //取出元素 get()
        Object o = map.get(1);
        Object o1 = map.get(2);
        System.out.println(o);//
        System.out.println(o1);////删除元素  remove()
        Object remove = map.remove(1);//根据键值删除
        System.out.println(remove);//
        System.out.println(map);//{2=庄, 3=张, 4=张}
        //遍历
Set set = map.keySet();

for (Object o2 : set) {
System.out.print(map.get(o2)+"\t");//庄 张 张
    }
}

Date

public class DateDemo {
    public static void main(String[] args) {
        //public class Date extends Object implements Serializable, Cloneable, Comparable<Date>
       //获取当前时间的值
        Date date = new Date();
        System.out.println(date);//Wed Jul 01 10:26:36 CST 2020
        //获取当前时间对应的毫秒值
        long time = date.getTime();
        System.out.println(time);//1593570879343
    }
}
DateFormat  
格式化日期类,通常用于日期和String的转换
public static void main(String[] args) throws ParseException {

//        public abstract class DateFormat extends Format
//        DateFormat 为抽象类  需要使用SimpleDateFormat子类实现
        Date date = new Date();
        DateFormat df= new SimpleDateFormat("yyyy-MM-dd");
        //按格式输出日期 format()
        String format = df.format(date);
        System.out.println(format);//2020-07-01
        //字符串转日期 parse()
        String s = "2025年6月3日";
        DateFormat df02= new SimpleDateFormat("yyyy年MM月dd日");
        Date parse = df02.parse(s);
        System.out.println(parse);//Tue Jun 03 00:00:00 CST 2025


    }
}
Calendar 
创建对象 
Calendar c = Calendar.getInstance();
标志       含义
G          年代标志符
y           年
M         月
d          日
h          时 在上午或下午 (1~12)
H          时 在一天中 (0~23)
m         分
s          秒
S         毫秒
E        星期
D         一年中的第几天
F         一月中第几个星期几
w        一年中第几个星期
W       一月中第几个星期
a        上午 / 下午 标记符
k         时 在一天中 (1~24)
K        时 在上午或下午 (0~11)
z         时区
public class CalendarDemo {
    //public abstract class Calendar extends Object
    //implements Serializable, Cloneable, Comparable<Calendar>
    //抽象类
    public static void main(String[] args) {
        //获取calendar对象   getInstance()
        Calendar calendar = Calendar.getInstance();
        //
        int year = calendar.get(Calendar.YEAR);
       //
        int month = calendar.get(Calendar.MONTH)+1;
        //
        int day = calendar.get(Calendar.DATE);
        //小时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        //
        int minuete = calendar.get(Calendar.MINUTE);
        //
        int second = calendar.get(Calendar.SECOND);
        System.out.println(year+"年"+month+"月"+day+"日"+hour+":"+minuete+":"+second);//2020年7月1日11:16:50

   //转换为日期对象
        Date date = calendar.getTime();
        System.out.println(date);//Wed Jul 01 11:16:50 CST 2020
        //设置时间值
        calendar.set(Calendar.YEAR,2021);
        System.out.println(calendar.get(Calendar.YEAR));//2021

        //修改当前时间为3天后
        calendar.add(Calendar.DATE,3);
        System.out.println(calendar.get(Calendar.DATE));
    }
}
基本数据类型和对应的包装类
包装类的作用实现基本数据类型和字符串之间的转换
public class PackageBasicDemo {
    
    private Integer age;//表现出第三种状态  null状态

    public static void main(String[] args) {
        //包装类 ---> 引用类型
        int a =123;
        //把基本数据类型转换成对应的包装类型 --> 装箱
        Integer b = a;
        //把包装类型转换成对应的基本数据类型 --> 拆箱
        int c = b;
        //int 转string
        String s = String.valueOf(a);
        // String 转int
        Integer integer = Integer.parseInt(s);
        System.out.println(s);
        System.out.println(integer);
    }
}

 

 

public class SystemDemo {
    //System类包含几个有用的类字段和方法。 它不能被实例化。

    /*
    1.获取当前时间的毫秒值
    2.结束当前程序
    3.数组的拷贝
     */
    public static void main(String[] args) {

//        1.获取当前时间的毫秒值
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        System.out.println(date);//Wed Jul 01 11:38:56 CST 2020

//        2.结束当前程序
        System.exit(0);

        //public static void arraycopy(Object src(来源),
        //                             int srcPos(起始),
        //                             Object dest(目标),
        //                             int destPos(目标起始),
        //                             int length(长度)
        //3.数组的拷贝
        int[] arr = {1,2,3,4};
        int []  arr02 ={4,5,6,7,8,9};
        System.arraycopy(arr,0,arr02, 2, 4);
        System.out.println(Arrays.toString(arr02));//[4, 5, 1, 2, 3, 4]
    }
}
 //max方法,返回两个参数值中较大的值
        int max = Math.max(2, 3);//3
        //min方法,返回两个参数值中较小的值
        int min =Math.min(2, 3);//2
        //round方法,返回参数值四舍五入的结果
        long round = Math.round(-3.5);//-3
        long round02 = Math.round(-3.6);//-4
        //ceil方法,结果为比参数值大的最大整数的double值
        double ceil = Math.ceil(3.1);//4.0
        //floor方法,结果为比参数值小的最大整数的double值
        double floor = Math.floor(3.9);//3.0
        //pow方法,返回第一个参数的第二个参数次幂的值
        double pow = Math.pow(3, 3);//27.0
        double random = Math.random();//得到的 是[0,1)的小数
        //锁定范围获取 a-b整数int a = (int)(Math.random()*(b-a+1))+a;

        int abs = Math.abs(-10);//10
public class ArrayDemo {

    public static void main(String[] args) {

        int [] arr ={1,5,4,3,1,8,10};
        //数组升序
        Arrays.sort(arr);
        //toString 将数组的内容以字符串形式呈现
        System.out.println(Arrays.toString(arr));//[1, 1, 3, 4, 5, 8, 10]

        //使用二分查找搜索指定int类型数组
        int i = Arrays.binarySearch(arr, 5);//4
        System.out.println(i);

    }
//大数据封装为BigInteger对象
 BigInteger big1 = new BigInteger("12345 678909876543210"); 
BigInteger big2 = new BigInteger("98765 432101234567890"); 
//add实现加法运算 
BigInteger bigAdd = big1.add(big2);
 System.out.println(bigAdd); 
//subtract实现减法运算 
BigInteger bigSub = big1.subtract(big 2); 
System.out.println(bigSub); 
//multiply实现乘法运算 
BigInteger bigMul = big1.multiply(big 2);
System.out.println(bigMul); 
//divide实现除法运算 
BigInteger bigDiv = big2.divide(big1);
 System.out.println(bigDiv);
//大数据封装为BigDecimal对象 
BigDecimal big1 = new BigDecimal("0.09"); 
BigDecimal big2 = new BigDecimal("0.01");
 //add实现加法运算
 BigDecimal bigAdd = big1.add(big2);
 System.out.println(bigAdd );
 BigDecimal big3 = new BigDecimal("1.0"); 
BigDecimal big4 = new BigDecimal("0.32");
 //subtract实现减法运算
 BigDecimal bigSub = big3.subtract(big4); 
System.out.println(bigSub); 
BigDecimal big5 = new BigDecimal("1.10 5"); 
BigDecimal big6 = new BigDecimal("100");
 //multiply实现乘法运算 
BigDecimal bigMul = big5.multiply(big6); 
System.out.println(bigMul); 
//divide实现除法 
BigDecimal bigDiv = big5.divide(big6,2,Bi gDecimal.ROUND_UP);

异常:程序在运行过程中,发生的非正常情况,导致JVM程序异常终止

异常 顶级父类是Throwable  Throwable异常类的下面有两个子类:ErrorException

检查异常:RuntimeException

非检查异常:一个是Throwable (原因),另一个是String (详细信息)和一个Throwable (原因)。

Throwable()

构造一个新的可抛出的 null作为其详细信息。

Throwable(String message)

构造一个具有指定的详细消息的新的throwable

 

Throwable类中

public String getMessage() 

返回此Throwable的详细消息字符串。

 

public void printStackTrace()

将此Throwable和其追溯打印到标准错误流。

Throwable类下的子类Error异常情况是无法解决的,属于硬伤

作为程序员能够解决的是Exception这种类型的异常情况

Exception:

编译期异常:通过使用集成开发工具编写程序的时候,就出现异常问题,如果不处理,程序无法继续向下编写

           需要程序员立刻对异常做出处理,否则无法编写

运行期异常:在编译之后,程序运行过程中产生的异常问题,JVM把异常信息抛出  

            一般是由于逻辑错误导致的异常情况,通过优化代码可以解决掉的

 

异常解决方法:1.add exception to method  往上抛  抛给调用者

              2. try/catch    在异常的位置直接处理掉,经过try /catch 处理之后,储中没有异常,程序会继续往下加载

如果产生异常的地方没有对它进行处理,那么会向上抛出,抛出给调用者。如果调用者还没有处理,会抛给JVM处理,JVM会找到自己内部能不能识别到该异常信息对应的类,如果找到了会构建一个该异常的对象,会把异常产生的原因,异常产生的位置、异常产生的内容封装进该对象中在控制台现象出来,并且立刻终止程序

编译期异常:除了runtimeexception异常之外,其余都是编译期异常

运行期异常:

 

当程序开发过程中,需要提醒调用者使用该代码块时按照已经制定好的规则使用,如果不是,那么我们会抛出异常,警告调用者慎用。可以使用 throw 语句

 

posted @ 2020-07-01 20:51  怎漾  阅读(441)  评论(0)    收藏  举报