LeetCode刷题遇到的知识点和问题的解决方法和自己容易混淆的地方(不定期更新)

  1. Java 出现unreachable statement异常

    一个unreachable statement就是编译器决定永远不会执行它。
    下面的几种情况会出现 unreachable statement:
    (1)在reutrn语句后写语句。
    (2)在throw语句后写语句。
    (3)break、continue语句之后定义语句。
  2.  如何将整数 int 转换成字串 String ?

        1、String s = String.valueOf(i);
        2、String s = Integer.toString(i);
        3、String s = "" + i;
    
    注: Double, Float, Long 转成字串的方法大同小异.

     

  3. 如何将字串 String 转换成整数 int?

     int i = Integer.parseInt([String]);

     

  4. Java去掉字符串前面的0

    String str = "000000001234034120";
    String newStr = str.replaceAll("^(0+)", "");//使用正则表达式
    System.out.println(newStr);
     public static void main(String[] args) {
            String str = "00001102";// 测试用字符串
            int len = str.length();// 取得字符串的长度
            int index = 0;// 预定义第一个非零字符串的位置
    
            char strs[] = str.toCharArray();// 将字符串转化成字符数组
            for (int i = 0; i < len; i ++) {
                if ('0' != strs[i]) {
                    index = i;// 找到非零字符串并跳出
                    break;
                }
            }
            String strLast = str.substring(index, len);// 截取字符串
            System.out.println(strLast);// 得到结果 strLast
        }

     

  5. Java字符串大小比较——compareTo()方法

语法:str1.compareTo(str2);

其返回的是一个int类型值。若Str1等于参数字符串Str2字符串,则返回0;若该Str1按字典顺序小于参数字符串Str2,则返回值小于0;若Str1按字典顺序大于参数字符串Str2,则返回值大于0。Java中的compareTo()方法,返回参与比较的前后两个字符串的ASCII码的差值。

*****************************************2020/10/16************************************

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值