Java:基础类的使用

String类

方法解释说明
charAt (int index)返回字符串中第index个字符
boolean equals(String other)如果字符串与other相等,返回true,否则返回false
boolean equalslgnore(String other)如果字符串与other相等,返回true,否则返回false(忽略大小写同上)
lastIndexOf()返回从末尾开始查找第一个字符串str 在字符串中的索引位置,未找到字符串返回-1
int indexOf (String str)返回从头开始查找第一个子字符串str在字符串中索引的位置,未找到返回-1
int length()返回字符串长度
replace(oldChar , newChar)替换字符
boolean startsWith( String abc )字符串以abc开始,返回true
boolean endsWith( String abc )字符串以abc结尾,返回true
String substring(int index)字符串的截取,从第index位开始,返回截取之后新字符串
String substring(6,11)截取从第6位开始到10位的所有字符,同上
String toLowerCase()返回小写字母
String toUpperCase()返回大写字母
String trim()去首位空格
Str.replace(" " ,“”)去除所有空格

包装类使用:

//基本数据类型转换对象
Integer i = new Integer(10);
Integer ii = Integer.valueOf(10);
//包装类对象转成基本数据类型
double d = ii.doubleValue();
//字符串数字转换成包装类对象
Integer i1 = Integer.valueOf("123");
Integer i2 = Integer.parse("123");
//包装类对象转换成字符串
String str = i.toString();

包装类Integer缓存问题

Integer  a = 4000;
Integer  b = 4000;
//当数字在[-128,128]之间时,返回缓存数组中的某个元素
Integer  c = 123;
Integer  d = 123;
a==b //false  两个不同对象
c==d //true
a.equals(b) // true

日历类:

//new一个对象
 GregorianCalendar calendar = new GregorianCalendar(2022,12,5,76,45,32);
 //get和set也可设置
 //周日是1,周六是7
 //日历对象和时间对象转换
 Date d = calendar.getTime();
 GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
 

Math类

  • abs : 绝对值
  • acos,asin,atan,cos,sin,tab : 三角函数
  • sqrt : 平方根
  • pow(double a , double b) : a的b次幂
  • max(double a.double b)/min: 取最大/最小
  • ceil(double a) : 大于a的最小整数
  • floor(double a) : 小于a 的最大整数
  • random() : 返回 0.0到10的随机数
  • long round(double a) : double数据a转换为long四舍五入
  • toDegrees(double angrad) : 弧度>角度
  • toRadians(double angdeg) : 角度>弧度

File类

java.io.File 用来代表文件和目录
常见构造方法:public File(String pathname)

File f1 = new File("d:/");
System.out.println("File是否存在"+f1.exists());
System.out.println("File是否存在"+f1.isDirectory());
System.out.println("File是否是文件"+f1.isFile());
System.out.println("File的文件名"+ f1.getName());
System.out.println("File的目录路径"+f1.getPath());
System.out.println("File大小"+f1.length());
  //打印文件信息
    //file文件名称
    //level曾次数:第几次递归调用
    static void printFile(File file , int level){
        for(int i=0;i< level;i++){
            System.out.print(".");
        }
        //输出文件名
        System.out.println(file.getName());
        
        if(file.isDirectory()){
            File[]  files = file.listFiles();
            for(File temp :files){
                //递归调用该方法
                printFile(temp,level+1);
            }
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值