码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA语法题

时间:2016-05-27 16:20:43      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:



import java.util.*; public class Birthdays { public static void main(String[] args){ Map<Friends, String> hm = new HashMap<Friends,String>(); Friends f1 = new Friends("Charis"); Friends f2 = new Friends("Darling"); hm.put(f1, "summer 2009"); hm.put(f2, "spring 2002"); System.out.println(hm.get(f1)); Friends f3 = new Friends("Darling"); System.out.println(hm.get(f3)); } } class Friends{ String name; Friends(String n){ name = n; } }

结果:

summer 2009
null

  

2.  enum用法 

public class WeatherTest {
    static Weather w;
    public static void main(String[] args){
    	System.out.println(w.RAINY.count+" "+w.Sunny.count+" ");
    }    
}
enum Weather{
	RAINY, Sunny;
	int count = 0;
	Weather(){
		System.out.print("c ");
		count++;
	}
}

结果

c c 1 1

 

public class WeatherTest {
    static Weather w;
    public static void main(String[] args){
    	System.out.println(w.RAINY+" "+w.Sunny+" ");
    }    
}
enum Weather{
	RAINY(1), Sunny(2);
	private  int n;
	private Weather(int n){
		this.n=n;
	}
	@Override
        public String toString() {
             return String.valueOf(this.n);
        }	
}

结果

1 2

枚举的遍历:

public class WeatherTest {  
	enum Weather{
		RAINY(1), Sunny(2);
		private  int n;
		private Weather(int n){
			this.n=n;
		}
		 @Override
	    public String toString() {
	        return String.valueOf(this.n);
	    }	
	}   
    public static void main(String[] args){
    	Weather[] allW = Weather.values();
    	for(Weather w: allW)
    	System.out.print(w+" ");
    }    
}

结果同上, 1 2

3. 数值类型题

public class Dec26 {

    public static void main(String[] args){
    	short a1=6;
    	new Dec26().go(a1);
    	new Dec26().go(new Integer(7));
    }
    
    void go(short x){
    	System.out.print("S ");
    }
    void go(Long x){
    	System.out.print("L ");
    }
    void go(int x){
    	System.out.print("i ");
    }
    void go(Number n){
    	System.out.print("N ");
    }
    
}

结果: 

S N

 

4. switch, case  

public class Humping {
    public static void main(String[] args) {
    	String r="-";
    	char[] c={‘a‘,‘b‘,‘c‘,‘z‘};
    	for(char c1:c)
    		switch(c1){
    			case ‘a‘:
    				r+="a";
    			case ‘b‘:
    				r+="b";
    				break;
    			default:
    				r+="X";
    			case ‘z‘:
    				r+="z";
    		}
    		System.out.println(r);    	
    }   
}

结果:

-abbXzz

5. HashMap键值为空的问题

HashMap:适用于在Map中插入、删除和定位元素。 
Treemap:适用于按自然顺序或自定义顺序遍历键(key)。

两者都是非线程安全,前者无排序,后者会自动排序 

Ø  HashMap里面存入的键值对在取出的时候是随机的,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。在Map 中插入、删除和定位元素,HashMap是最好的选择。

Ø  TreeMap取出来的是排序后的键值对。插入、删除需要维护平衡会牺牲一些效率。但如果要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。

本测试增加和查找功能,HashMap比TreeMap的效率要高。

import java.util.*;
public class Garage {

    public static void main(String[] args) {
    	Map<String,String> hm=new HashMap<String,String>();
    	String[] k={null,"2","3",null,"5"};
    	String[] v={"a","b","c","d","e"};
    	for(int i=0;i<5;i++){
    		hm.put(k[i],v[i]);
    		System.out.print(hm.get(k[i])+" ");
    	}
    	System.out.print(hm.size()+" "+hm.values()+"\n");   	
    }   
}

结果

a b c d e 4 [c, d, b, e]

换成TreeMap报错, 所以 TreeMap键值不允许空 

null和" "的区别:

import java.util.*;
public class Garage {

    public static void main(String[] args) {
    	Map<String,String> hm=new HashMap<String,String>();
    	String[] k={null,"2","3"," ","5"};
    	String[] v={"a","b","c","d","e"};
    	for(int i=0;i<5;i++){
    		hm.put(k[i],v[i]);
    		System.out.print(hm.get(k[i])+" ");
    	}
    	System.out.print(hm.size()+" "+hm.keySet()+" "+hm.values()+"\n");   	
    }   
}

结果

a b c d e 5 [3, null, 2,  , 5] [c, a, b, d, e]

 

  

 

 

 

  

 

 

JAVA语法题

标签:

原文地址:http://www.cnblogs.com/wujixing/p/5534916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!