第二周预习博客


第一周预习博客

1.JAVA内部类和异常类

1.1.异常类

定义:

java对程序中出现的异常错误进行处理的类体。

当一个方法在执行过程中出现异常的时候,就会抛出异常对象;

1.1.1.try-catch语句

使用方法

操作者将可能出现异常的语句放在try部分里,一旦try抛出异常对象,那么try部分立刻停止运行,转向执行catch部分;
一个try可以有多个catch

格式

try{
something could be wrong
}
catch (ExceptingSubClass1 e{
...
}
catch (ExceptingSubClass1 e{
...
}

从继承理解,catch中各个异常类都是Exception的某个子类,表示try部分可能发生的异常,且catch类之间不能有父子关系;

1.1.2.自定义异常类

有固定的异常类例如NumberFormatException,异常类还可以自己定义,在定义异常类的时候要用throw和throws关键字抛出异常,当抛出异常后,方法执行结束。

代码演示:

public class cly {
    public static void main(String[] args) {
        Bank bank = new Bank();
        try {
            bank.income(200,-100);
            bank.income(300,-100);
            bank.income(400,-100);
            bank.income(200,100);  //抛出以后不再执行(9999,-100)
            bank.income(9999,-100);
        }
        catch(BankException e){
            System.out.println("error !");
            System.out.println(e.warnMess());
        }
    }
}

public class Bank {
    private int money;
    public void income(int in,int out)throws BankException{  throws抛出
        if(in<=0||out>=0||in+out<=0){
            throw new BankException(in,out);  //throw抛出异常对象
        }
        int sum=in+out;
        System.out.println(sum);
    }
}

public class BankException extends Exception{  //定义的异常类,继承Excaption
    String message;
    public BankException(int m,int n){
        message="in$ "+m+" is negativenumber or out$ "+n+" is positivenumber";
    }
    public String warnMess(){
        return message;
    }
}

结果:

100
200
300
error !
in$ 200 is negativenumber or out$ 100 is positivenumber

1.1.2.1.throw抛出

抛出异常对象;

1.1.2.2.throws抛出

声明要产生的若干异常;

1.1.3.断言

改语句主要用于调试代码,一般不用于程序执行过程中,当发生某个错误时候,要求程序立刻停止运行;

格式:

assert booleanException;
assert boolenException:messageException;

例如assert numerical>=0;
表达式的值为true,程序继续执行,false程序停止进行;

用idea使用assert时候一定要设置VM options里为-ea,否则assert将无用;
在这里插入图片描述

2.JAVA常用类

前言
Java提供了强大的类库,帮助开发者快速完成开发任务,这也是其叱咤风云20多年的一个重要原因。我们现在就学习一些Java中常用的类。

字符串相关:String、 StringBuffer、 StringBuilder
包装类:Integer等
日期相关: Date、 Calendar、 SimpleDateFormat
其他:Math、Random、Runtime、System

2.1.String类

2.1.1.创建方式

赋值常量

String str = “hello”;

使用new创建对象

String name = new String(“zhangsan”);

2.1.2.字符串的并置

代码演示:

public class cly {
    public static void main(String args[]) {
        String hello = "你好";
        String testOne = "你" + "好";  //testOne为 “你好”
        System.out.println(hello == testOne); 
        System.out.println("你好" == testOne);
        System.out.println("你好" == hello);
        String you = "你";
        String hi = "好";
        String testTwo = you + hi; //textTwo为 “你好”
        System.out.println(hello == testTwo);
        String testThree = you + hi;  //textThree为 “你好”
        System.out.println(testTwo == testThree);
    }
}

结果

true
true
true
false
false

2.1.3.常用方法

获得字符串长度

int length()

获得子字符串出现的位置

int indexOf(String s)

在字符串中查找子字符串第一次出现的位置,没找到返回-1;

int indexOf(String s,int start)

指定了开始查找的位置

int lastIndexOf(String s)

截取字符串

String substring(int start,int end)
startend-1

替换

String replace(String oldStr,String newStr)

比较字符串内容

boolean equals(String s) 是否完全相同
boolean equalsIgnoreCase(String s) 忽略大小写进行比较
boolean startsWith(String s) 判断字符串是否以某字符串开头
boolean endsWith(String s) 判断字符串是否以某字符串结尾
int compareTo(String s) 比较大小,返回正数(大于)、负数(小于)、0(相等)


2.2.StringBuffer类

StringBuffer代表一个字符序列可变的字符串。

StringBuffer提供的append()、insert()、reverse()以及setCharAt()等方法,可以改变该

StringBuffer对象的字符序列。

提醒:String是不可变类,如果使用String创建字符串,则会额外生成很多字符串,耗费系统资源。

方法名说明
StringBuff append(String str)将字符串str追加到当前StringBuffer对象
StringBuffer insert (intoffset , String str)按顺序将字符串str插入到当前StringBuffer对象的intoffs位置
StringBuffer reverse()将当前StringBuffer对象的字符序列用其反转形式取代
void setCharAt(int index, char ch)将index索引处的字符设置为ch
StringBuffer replace(int startIndex,int endIndex,String str)将指定的StrungBuffer的一个子字符串替换,由start和end决定替换的字符串

代码展示:

public class Example8_ 14 l
public static void main(String args[]) (
StringBuffer str=new StringBuffer();
str.append("大家好");    //创建一个stringbuffer类字符串
System. out.println("str:"+str);
System. out. println("length:"+str. length()) ;
System. out. println ("capacity:"+str .capacity()) ; 
str .setCharAt(o , 'w');    //字符替换
str. setCharAt(1 , 'e');    //字符替换
System.out.println(str);
str. insert(2, " are all");    //插入字符
System.out.println(str);
int index=str. indexOf ("好");    /替换地方
str.replace (index,str.length()," right");    //替换完成
System.out.println(str);

2.3.日期类

2.3.1.Date

1.Date():用来创建当前日期时间的Date对象

2.Date(long date):用于创建指定时间的Date对象,其中date参数表示1970年1月 1日0时0分0以来的毫秒数,即时间截

代码演示

	Date date = new Date();
	System.out.println("一般格式:"+ss);	
	//一般格式:Sat Jun 13 23:10:24 GMT+08:00 2020
    long times = date.getTime();
    System.out.println(times); 		    //输出的是时间截

2.3.2.Calendar类

Calendar类在java.util 包中。使用Calendar类的sate方法getlnstance可以初始化一个日历对象,例如:

Calendar calendar = Calendar .getInstance();

然后利用Calender对象可以调用方法

public final void set (int year,int month,int date)
public final void set (int year,int month, int date, int hour, int minute)
public final void set(int year,int month, int date, int hour, int minute, int second)

例如:

calendar.get (Calendar.MONTH);

返回一个整数,如果该整数是0 表示当前日历是在一月, 该整数是1表示当前日历是在二月。

Ca lendar .get (Calendar .DAY OE WEK);

返回个整数,如果该整数是1,表示星期日,该整数层2表示星期二,依次类推, 7表示星期天。

2.3.3.日期的格式化

format方法

格式:

format(格式化模式,日期列表)

代码演示

Date nowTime = new Date() ;
String s1 = String. format("&tY年%tm月%td日",nowTime,nowTime,nowTime); 
//%ty,%tm,%td分别表示日期中的年月日
String s2 = String. format ("&tF",nowTime) ;  
%tf相当于用%ty,%tm,%td同时格式化一个时间

特别注意:在代码运行前一定要加入import java.util.Date;
在这里插入图片描述

2.4.Math类

即数学计算类
例如:

public BigInteger add(BigInteger val):返回和a的和
public BigInteger subtract(BigInteger val):返回和val的差
public BigInteger multiply(BigInteger val):返回和val的积
public BigInteger divide(BigInteger val):返回和val的商
public BigInteger remainder(BigInteger val):返回和val的余数
public BigInteger compareTo(BigInteger val):返回与val的比较结果,1表示大于val,-1小于val,0等于val
public BigInteger abs():返回绝对值
public BigInteger pow(int a):返回当前对象的a次幂
public BigInteger toString():返回当前对象的十进制字符串表示
public BigInteger toString(int p):返回当前对象的p进制字符串表示

2.5.BigInteger类

public BigInteger add(BigInteger val):返回和a的和
public BigInteger subtract(BigInteger val):返回和val的差
public BigInteger multiply(BigInteger val):返回和val的积
public BigInteger divide(BigInteger val):返回和val的商
public BigInteger remainder(BigInteger val):返回和val的余数
public BigInteger compareTo(BigInteger val):返回与val的比较结果,1表示大于val,-1小于val,0等于val
public BigInteger abs():返回绝对值
public BigInteger pow(int a):返回当前对象的a次幂
public BigInteger toString():返回当前对象的十进制字符串表示
public BigInteger toString(int p):返回当前对象的p进制字符串表示

2.6.Random类

public random();

可以使得返回一个0-1之间的随机数(左闭右开)

random.nextInt(int n);

可以使得返回一个0-n之间的一个整数(不包括你)

random.nextBoolean();

可以使得返回一个随机的boolean值

2.7.数字格式化

大部分格式化方式与C语言同,不同如下;

修饰符:
加号修饰符“+”:格式化正整数时,强制添加上正号,例如,%+d将123格式化为"+123".
逗号修饰符“,”格式化整数时,按“千”分组,例如,对于
String s=String. format("按千分组:%,d.按千分组带正号+,d", 1235678,9876);
字符串s就是

按千分组:1,235,678.按千分组带正号+9,876

2.8.Console类

简称密码类,即键盘输入一行文本,但不让文本回显;
构造方法;

Console cons = System.console();

然后利用readPassword()方法在键盘输入的一行文本,并以数组形式返回

char [] password = cons.readPassword();

2.9.Pattern类和Matcher

(模式类)Pattern与(匹配类)Match就是检索和指定模式匹配的字符串。
建立pattern对象:

Pattern compile(String regex)
//使用Pattern初始化创建一个模式对象(对正则表达式的封装)
Matcher matcher(CharSequence input)
//返回一个Matcher对象,称作匹配对象
public boolean find ()
 //寻找input和regex匹配的下一系列,成功true,失败则false
public boolean matches() 
//判断input是否完全和regex完全匹配

得到matcher对象:

Matcher matcher = pattern.matcher(input); //input是对象

matcher也可以调用方法,这里不列举

3.容器

可以理解为一种装其他东西(对象)的器皿,例如数组
在这里插入图片描述
容器要有一个接口,下面介绍collection接口:

3.1.collection接口:

collection接口定义了存储一组对象的方法,其子接口Set和List分别定义了存储方式
Set:无序存储,无顺序不可重复
List:有序存储,有顺序可重复

构造方法,例如

Collection c = new Arraylist();

父类对象去访问子类,子类的私人方法父类无法访问,后期可以更改,大大提高了灵活性

3.2.Iterator接口:

所有实现了collection接口的容器类都有一个iterator方法用以返回了一个实现iterator接口的对象。
以下理解来自于该博客:
点击这里

4.总结:

容器没有搞懂,在容器方面要下大功夫

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值