> 文档中心 > Java基础类库

Java基础类库

文章目录

  • 前言
  • Scanner
  • Math类&Random
  • Arrays和ArrayList
    • Arrays
    • ArrayList
  • Date类
  • Calendar类
  • Object类与Objects
  • System类
  • StringBuilder类
  • 基本类型包装类

前言

提示:本文来自老师授课内容,已获得老师同意在此当做本人学习笔记记录:

API程序的编程接口,把好多方法从底层进行了封装,我们不需要关注如何实现(\Java8\jdk\src\java),我们只要求会使用就行。


提示:以下是本篇文章正文内容,下面案例可供参考

Scanner

//输入2个数据,把最大的输出public static void main(String[] args) {//1.引入包   2.创建  3.使用对应的方法Scanner sc = new Scanner(System.in);  //系统的输入,也就是通过键盘录入数据int number = sc.nextInt();  System.out.println("您输入的数字是:"+number);//next()    }  

Math类&Random

//先随机生成一个数   循环输入各种整数public static void main(String[] args) {int guessnumber = 0;//guessnumber = (int) (Math.random()*100+1);  //0-100Random r = new Random();guessnumber = r.nextInt(100)+1;   //[0,100)System.out.println("guessnumber是:"+guessnumber);Scanner sc = new Scanner(System.in);  //系统的输入,也就是通过键盘录入数据int number = 0;while(true) {System.out.println("请输入您的数字!");number = sc.nextInt(); if(guessnumber==number) {System.out.println("猜对了!");break;}if(guessnumber>number) {System.out.println("你输入的数小了!");}else {System.out.println("你输入的数大了!");}}//生成一个50-100的数如何写?    }  

Arrays和ArrayList

数组工具类排序,搜索

Arrays

public static void main(String[] args) {int[] num = {15,4000,10000,23,18};Arrays.sort(num);System.out.println(Arrays.toString(num));//思考如何倒序输出}  

ArrayList

增add删remove查get /size

public static void main(String[] args) {ArrayList<String> list = new ArrayList<String>();list.add("helloworld");list.add("helloNeusoft");//问题:如何把list的所有元素都输出System.out.println(list.get(0) + "长度"+list.size());}  

Date类

format parse 格式解析

public static void main(String[] args) throws ParseException {Scanner sc = new Scanner(System.in);  //系统的输入,也就是通过键盘录入数据System.out.println("请输入");String birth = sc.next();  System.out.println("您输入的时间是"+birth);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date birthDate = sdf.parse(birth);long time = birthDate.getTime();long todaytime = new Date().getTime();long result = (todaytime - time)/1000/60/60/24;System.out.println("您出生的天数"+result);}  

Calendar类

get set add gettime 获取设置添加获取时间

public static void main(String[] args) throws ParseException {Calendar c1 = Calendar.getInstance(); //返回一个对象Calendar c2 = Calendar.getInstance(); //不是单例模式的System.out.println(c1.hashCode());System.out.println(c2.hashCode());int year = c1.get(Calendar.YEAR);  //获取System.out.println(year);c1.set(9999,9,9);//设置   c1.set(Calendar.YEAR,9999)System.out.println(c1.get(Calendar.YEAR));c1.add(Calendar.YEAR, -2);  //减2年System.out.println(c1.get(Calendar.YEAR));System.out.println(c1.getTime()); //把日历转换成日期输出}  

Object类与Objects

toString equals,toString 等于

public static void main(String[] args)  {String str1 = null;String str2 = "123";System.out.println(Objects.equals(str1, str2));//没有抛异常,增加了健壮性}  

System类

currentTimeMillis获取当前的时间(毫秒)

public static void main(String[] args)  {//测试程序的效率,计算程序运行的时间long s = System.currentTimeMillis();//for(int i =0; i <12345; i++) {System.out.println(i);}long result = System.currentTimeMillis()-s;System.out.println("程序运行"+result+"毫秒");}  

StringBuilder类

append 将字符串追加到序列里
to String() 将序列里的数据转换成字符串的形式

public static void main(String[] args)  {StringBuilder bu = new StringBuilder("哈哈哈"); //new StringBuilder();bu.append("电脑").append("主机").append("固态硬盘").append("真好啊");System.out.println(bu);String s = bu.toString();  //StringBuilder转String用的是toString()方法StringBuilder bu2 = new StringBuilder(s);  //String转换成StringBuilderSystem.out.println(bu2);}  

基本类型包装类

集合中不放基本的数据类型,所以集合使用到基本数据类型的时候就需要包装类转换成包装类对象
包装类里提供了很多方法可以用,比如查看最大值、小、二进制
更加健壮,比如Integer不仅包括0,还包括null 8种,Integer Character其他的基本数据类型的包装类就是首字母大写Short、Long、Boolean

Integer int1 = 4;  //new Integer(10)Integer int2 = Integer.parseInt("123"); //new Integer("123")String s = int1.toString();System.out.println(Integer.MAX_VALUE);

BMI经典题
BMI:身体质量指数
计算公式为:BMI=体重÷身高2
BMI正常值在20至25之间,超过25为超重,30以上则属肥胖

public static void main(String[] args) {/* * /*从键盘输入性别,身高,体重 其中输入整数0表示女,1表示男 身高使用浮点数,精确到小数点后两位,单位(米) 体重使用公斤数 * BMI=体重(千克)除以身高(米)的平方 成人标准值是BMI18.5-23.9才算标准体重。 * 正常:18.5-23.9、超重:≥24、偏胖bai:24~27.9、肥胖:≥28。 */Scanner sc = new Scanner(System.in);System.out.println("请输入您的性别,0表示女,1表示男");int sex = sc.nextInt();System.out.println("请输入您的身高,身高使用浮点数,精确到小数点后两位,单位(米)");double height = sc.nextDouble();System.out.println("请输入您的体重,体重使用公斤数");double weight = sc.nextDouble();double bmi = weight / Math.pow(height, 2);System.out.println("您的体重指数是:" + bmi);System.out.println("您的健康程度是:");if (sex == 0) {if (bmi < 19) {System.out.println("女士,您的体重过轻,请加强膳食的补充!");} else if (bmi < 24) {System.out.println("恭喜您女士,您的体重适中,请保持!");} else if (bmi < 29) {System.out.println("女士,您的体重已经过重,请注意锻炼身体!");} else if (bmi < 34) {System.out.println("女士,您已经属于肥胖行列注意饮食习惯,加强体育锻炼!");} elseSystem.out.println("女士,您已经非常肥胖必须下定决心实施减肥了!");} else {if (bmi < 20) {System.out.println("男士,您的体重过轻,请加强膳食的补充!");} else if (bmi < 25) {System.out.println("恭喜您男士,您的体重适中,请保持!");} else if (bmi < 30) {System.out.println("男士,您的体重已经过重,请注意锻炼身体!");} else if (bmi < 35) {System.out.println("男士,您已经属于肥胖行列注意饮食习惯,加强体育锻炼!");} elseSystem.out.println("男士,您已经非常肥胖必须下定决心实施减肥了!");}System.out.println("专家建议,最理想的体重指数是22,为了健康的身体,大家一起加油吧!");}