java基础记录·杂(while、split、位运算符)

一、

split截取是将所有子串放入数组里,若大量操作效率较低,可以用StringTokenizer替换。

测试如下:

package gcc.mix.test;
import java.util.StringTokenizer;
/**
 * Created by gcc on 2018/5/4.
 * 单纯截取字符串用substring,少量长字符串截取取值可以用split,大量的话用StringTokenizer
 */
public class SplitTest {

    public static final String getTerminalFromYardLocation(String yardLocation) {
        String result = null;
        if (yardLocation != null) {
            int start = yardLocation.indexOf(",") + 1;
            int end = yardLocation.indexOf(",", start);
            if (end > start) {
                result = yardLocation.substring(start, end);
            }
        }
        return result;
    }

    public static final String getTest(String test) {
        String s = null;
        if (test != null) {
            String[] gt = test.split(",");
            if (gt.length > 1) {
                s = gt[1];
            }
        }
        return s;
    }

    public static final String StringTokenizerTest(String test) {
        String s = null;
        if (test != null) {
            StringTokenizer st = new StringTokenizer(test, ",");
            //st.countTokens();
            st.nextToken();
            st.nextToken();
            s = st.nextToken();
        }
        return s;
    }

    public static void main(String[] args) {
        String s = "Y,T,1,B14,1,2,1,2";

        System.out.println("1:"+getTerminalFromYardLocation(s));
        System.out.println("2:"+getTest(s));
        System.out.println("3:"+StringTokenizerTest(s));

        int max = 100000;
        long start = System.currentTimeMillis();
        for (int i = 0; i < max; i++) {
            getTerminalFromYardLocation(s);
        }
        System.out.println("getTerminalFromYardLocation escape time: " + (System.currentTimeMillis() - start) + " ms");

        start = System.currentTimeMillis();
        for (int i = 0; i < max; i++) {
            getTest(s);
        }
        System.out.println("getTest escape time: " + (System.currentTimeMillis() - start) + " ms");

        start = System.currentTimeMillis();
        for (int i = 0; i < max; i++) {
            StringTokenizerTest(s);
        }
        System.out.println("getTest1 escape time: " + (System.currentTimeMillis() - start) + " ms");
    }

}

结果如下:


二、

这里记录下while (n-- != 0) { }

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

三、

<<:

左移就是把一个数的全部位数都向左移动若干位,例如以下:

12 的二进制为 1100

则 12 << 1 的二进制为 11000, 则完整的运算式为 12 << 1 = 24;

>>:

右移就是把一个数的全部位数都向右移动若干位,例如以下:

12 的二进制为 1100

则 12 >> 1 的二进制为 0110。 则完整的运算式为 12 >> 1 = 6;

~:

'取反'运算符~的作用是将各位数字取反:全部的0置为1,1置为0,例如以下:

12 的二进制为 1100

进行取反操作为 10000000 00000000 00000000 00001101

则完整的运算式为

~12 = -13

&:
作用是对运算符两側以二进制表达的操作符按位分别进行'与'运算。操作的规则是:仅当两个操作数都为1时。输出结果才为1。否则为0,例如:
       12 的二进制为 1100
        5  的二进制为 0101

则 12 & 5 的二进制为 0100,则完整的运算式为 12 & 5 = 4; 

可以用于判断一个数的奇偶(比其他方法效率高)。如5&1,只判断末尾,1则奇数,0则偶数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值