一、
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则偶数