java例程练习(泛型和自动打包、解包)

//TestArsgWords.java
import java.util.*;
public class TestArgsWords {
	
	public static void main(String[] args) {
		Map<String, Integer> m = new HashMap<String, Integer>();
		
		for(int i = 0; i < args.length; i++) {
			
			if(!m.containsKey(args[i])) {
				m.put(args[i], 1);	
			} else {
				int freq = m.get(args[i]);
				m.put(args[i], freq + 1);	
			}

		}
		
		System.out.println
			(m.size() + " distinct words detected:");
		System.out.println(m);
	}	
}
//Test.java
import java.util.*;

public class Test {
	public static void main(String[] args) {
		List<String> c = new ArrayList<String>();
		
		c.add("aaa");
		c.add("bbb");
		c.add("ccc");
		
		for(int i = 0; i < c.size(); i++) {
			String s = c.get(i);
			System.out.println(s);
		}
		
		Collection<String> c2 = new HashSet<String>	();
		c2.add("aaa");
		c2.add("bbb");
		c2.add("ccc");
		
		for(Iterator<String> it = c2.iterator(); it.hasNext();) {
			String s = it.next();
			System.out.println(s);
		}
		
	}
}


class MyName implements Comparable<MyName> {
	int age;
	
	public int compareTo(MyName mn) {
		if(this.age > mn.age) {
			return 1;
		} else if(this.age < mn.age) {
			return -1;
		} else {
			return 0;
		}
	}
}





//Test.java
import java.util.*;

public class Test {
	public static void main(String[] args) {
		Map<String, Integer> m1 = new HashMap<String, Integer>();
		Map<String, Integer> m2 = new HashMap<String, Integer>();
		
		m1.put("one", 1);
		//m1.put("one",new Integer(1));
		
		m1.put("two", 2);
		//m1.put("two",new Integer(2));
		
		m1.put("three", 3);
		//m1.put("three",new Integer(3));
		
		m2.put("A", 1);
		//m2.put("A", new Integer(1));
		
		m2.put("B", 2);
		//m2.put("B", new Integer(2));
		
		System.out.println(m1.size());
		System.out.println(m1.containsKey("one"));
		System.out.println
			(m2.containsValue(1));
			//(m2.containsValue(new Integer(1)));
		if(m1.containsKey("two")) {
			int i = m1.get("two");
			//int i =(Integer)m1.get("two");
			//int i = ((Integer)m1.get("two")).intValue();
			System.out.println(i);
		}
		
		Map<String, Integer> m3 = new HashMap<String, Integer>(m1);
		m3.putAll(m2);
		System.out.println(m3);
	}
}






posted on 2012-05-01 15:06  Yours风之恋  阅读(210)  评论(0编辑  收藏  举报