zoukankan      html  css  js  c++  java
  • Java源码——Integer

      

      最近在研究java的源代码,但是由于自己英语水平有限,所以想使用中文注释的方式把源码里的方法全部重写

        一遍,下面是楼主整理出来的一小部分。我把整体的项目托管到GitHub上了,欢迎大家前去交流学习。

        GitHub : https://github.com/15128928804/yuanMa

      

    /**
    * @Author:zhuangfei
    * @Description:返回由第二个参数指定基数转换为字符串格式的第一个参数
    * 如果基数超过了Character的最小(-2)或最大(36)区间,会指定为10
    * 如第一个参数为负,则会把它相应转换后的ASCII参数前加上 ‘-’
    * i :需要转换的参数
    * radix :指定的基数
    * @Date:11:13 2017/11/29
    */
    public static String toString(int i, int radix) {
    if(radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
    radix = 10;
    }

    if(radix == 10)
    return toString(i);

    char buf[] = new char[33];
    boolean negative = (i < 0);
    int charPos = 32;

    if(!negative)
    i = -i;

    while (i <= -radix) {
    buf[charPos--] = digits[-(i % radix)];
    i = i / radix;
    }
    buf[charPos] = digits[-i];
    if(negative) {
    buf[--charPos] = '-';
    }

    return new String(buf, charPos, (33 - charPos));
    }

    /**
    * @Author:zhuangfei
    * @Description:返回指定参数的String格式,指定的整数参数转换为有符号的小数返回
    * i :指定参数
    * @Date:11:28 2017/11/29
    */
    public static String toString(int i) {
    if(i == Integer.MIN_VALUE) {
    return "-2147483648";
    }
    int size = (i < 0)? stringSize(-i) + 1: stringSize(i);
    char[] buf = new char[size];
    getChars(i, size, buf);
    return new String(buf, true);
    }

    /**
    * @Author:zhuangfei
    * @Description:将整数放入数组中,字符被放置到缓冲区里,然后从指定索引处最不重要
    * 的数开始向后遍历
    * i :整数
    * index :指定的索引
    * buf :字符数组
    * @Date:11:33 2017/11/29
    */
    static void getChars(int i, int index, char[] buf) {
    int q, r;
    int charPos = index;
    char sign = 0;

    if (i < 0) {
    sign = '-';
    i = -i;
    }

    while (i >= 65536) {
    q = i / 100;
    r = i - ((q << 6) + (q << 5) + (q << 2));
    i = q;
    buf[--charPos] = DigitOnes[r];
    buf[--charPos] = DigitTens[r];
    }

    for(;;) {
    q = (i * 52429) >>> (16 + 3);
    r = i - ((q << 3) + (q << 1));
    buf[--charPos] = digits[r];
    i = q;
    if (i == 0) break;
    }
    if(sign != 0) {
    buf[--charPos] = sign;
    }
    }
    /**
    * @Author:zhuangfei
    * @Description:需要正数的参数
    * x :正参
    * @Date:11:42 2017/11/29
    */
    static int stringSize(int x) {
    for(int i = 0; ; i++) {
    if(x <= sizeTable[i]) {
    return i + 1;
    }
    }
    }
  • 相关阅读:
    ElasticSearch工作原理
    prometheus监控es集群
    es索引调优
    ES中Refresh和Flush的区别
    网络服务器技术Apache与Nginx,IIS的不同
    shell里/dev/fd与/proc/self/fd的区别
    常用抓包工具
    Ubuntu Kubuntu Xubuntu Lubuntu Dubuntu Mythbuntu UbuntuBudgie区别
    Android的Looper.loop()消息循环机制
    申请读写sd卡权限shell
  • 原文地址:https://www.cnblogs.com/zhuangfei/p/7929586.html
Copyright © 2011-2022 走看看