古老的代码,java代替goto,代码没有实际的意思,测试这种功能而已。注释解释啦功能。不知道还有没人会这样用。只是测试下这个功能。
- aa: if (true) {
- int a = 101;
- if (a == 10) {
- System.out.println("==10");
- } else {
- break aa;
- }
- System.out.println("last");// 如果a不等于10的话就跳出aa代码块,不会打印last
- }
- bb: for (int num = 1; num <= 5; num++) {
- System.out.println("--------------- 第一层循环: " + num);
- for (int i = 0; i < 3; i++) {
- if (num == 4)
- continue bb;
- System.out.println("第二层循环: " + i);
- }
- }
1 三元运算符 对于只有赋值的形式的if else if else if 如果很多的话,不妨试试三元运算符,多三元运算符:
public class Test {
public static String test1(String args){
if(args.equals("a1")){
if (!args.equals("a1")) {
return "a other!";
}
return "a1";
}else{
if (!args.equals("b1")) {
if (args.equals("b2")) {
return "b2";
}else{
return "b other!";
}
}
return "b1";
}
}
public static String test2(String args){
return args.equals("a1")?
( args.equals("a1")?"a1":"a other!" ):
( args.equals("b1")?"b1":
(args.equals("b2")?"b2":"b other!") );
}
public static void main(String[] args) {
System.out.println(test1("a1"));
System.out.println(test2("a1"));
System.out.println("-------------");
System.out.println(test1("b1"));
System.out.println(test2("b1"));
System.out.println("===============");
System.out.println(test1("a1d"));
System.out.println(test2("a1d"));
System.out.println("-------------");
System.out.println(test1("b1d"));
System.out.println(test2("b1d"));
System.out.println("===============");
System.out.println(test1("b2"));
System.out.println(test2("b2"));
System.out.println("-------------");
}
}
对于if(if else) else( if else)更简单
三目运算符的效率和if else差不多,一亿次的运算也相差32毫秒左右,而加强的for比普通的for效率要高很多,不到一倍,本地的测试:
int[] arrays = new int[10000];
int[] results = new int[10000];
long startTime = System.currentTimeMillis();
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays.length; j++) {
results[j] = arrays[j];
}
}
long endTime = System.currentTimeMillis();
System.out.println("普通for循环:" + (endTime - startTime) + "毫秒");
startTime = System.currentTimeMillis();
for (int i : arrays) {
for (int j : arrays) {
results[j] = arrays[j];
}
}
endTime = System.currentTimeMillis();
System.out.println("加强for循环:" + (endTime - startTime) + "毫秒");
结果如下:
普通for循环:360毫秒
加强for循环:250毫秒
下面的代码集中方法是同一种效果
public static void main(String[] args) {
boolean boo1 = "415sdg".matches("\\^\\d \\$ ");// if it contains the
boolean boo2 = "1515151515".matches("\\d*"); // if it not contains
String a = "a";
System.out.println(threetype(a));
System.out.println(ifelse(a));
System.out.println(ifelse(a));
System.out.println(zhezhong(a));
System.out.println(zhezhong2(a));
a = "b";
System.out.println("===================");
System.out.println(threetype(a));
System.out.println(ifelse(a));
System.out.println(ifelse(a));
System.out.println(zhezhong(a));
System.out.println(zhezhong2(a));
a = "c";
System.out.println("===================");
System.out.println(threetype(a));
System.out.println(ifelse(a));
System.out.println(ifelse(a));
System.out.println(zhezhong(a));
System.out.println(zhezhong2(a));
a = "d";
System.out.println("===================");
System.out.println(threetype(a));
System.out.println(ifelse(a));
System.out.println(ifelse(a));
System.out.println(zhezhong(a));
System.out.println(zhezhong2(a));
a = "d6%^&*(((()_++++|";
System.out.println("===================");
System.out.println(threetype(a));
System.out.println(ifelse(a));
System.out.println(ifelse(a));
System.out.println(zhezhong(a));
System.out.println(zhezhong2(a));
}
public static String threetype(String args) {
return "a".equals(args) ? "a" : ("b".equals(args) ? "b" : ("c"
.equals(args) ? "c" : ("d".equals(args) ? "d" : "other!")));
}
public static String ifelse(String args) {
String a = "";
if ("a".equals(args)) {
a = "a";
} else {
if ("b".equals(args)) {
a = "b";
} else {
if ("c".equals(args)) {
a = "c";
} else {
if ("d".equals(args)) {
a = "d";
} else {
a = "other!";
}
}
}
}
return a;
}
public static String zhezhong(String args) {
String a = "";
boolean boo = "a".equals(args) || "b".equals(args) || "c".equals(args)
|| "d".equals(args);
if (boo) {
a = args;
} else {
a = "other!";
}
return a;
}
public static String zhezhong2(String args) {
boolean boo = "a".equals(args) || "b".equals(args) || "c".equals(args)
|| "d".equals(args);
return boo ? args : "other!";
}
一种是if else太多看的人头晕,一种是代码让人看着苦涩难懂。一种是代码臃肿,一种是代码简洁。 那么你更喜欢那种呢?
public String toman(String inrole){
return
"a".equals(inrole)?
"a":("b".equals(inrole)?
"b":("c".equals(inrole)?
"c":("d".equals(inrole)?
"d":"others!")
));
}