Java基础特殊应用

古老的代码,java代替goto,代码没有实际的意思,测试这种功能而已。注释解释啦功能。不知道还有没人会这样用。只是测试下这个功能。

 

Java代码 
  1. aa: if (true) {  
  2.     int a = 101;  
  3.     if (a == 10) {  
  4.         System.out.println("==10");  
  5.     } else {  
  6.         break aa;  
  7.     }  
  8.     System.out.println("last");// 如果a不等于10的话就跳出aa代码块,不会打印last  
  9. }  
  10.   
  11. bb: for (int num = 1; num <= 5; num++) {  
  12.     System.out.println("--------------- 第一层循环: " + num);  
  13.     for (int i = 0; i < 3; i++) {  
  14.         if (num == 4)  
  15.             continue bb;    
  16.         System.out.println("第二层循环: " + i);  
  17.     }  
  18. }  

 

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!")
		));
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值