百度空间 | 百度首页 
 
查看文章
 
每天学Java-22 容器
2009-02-02 20:48
Java语言: 容器练习
import java.util.*;//要加上他。。    
public class TestCollection {//容器练习
    public static void main(String[] args){
        Collection c= new ArrayList();//可以放入不同类型的对象 用父类的对象指向子类 面向对象的思想
        c.add("hello");
        c.add(new Name("f1","l1"));
        c.add(new Integer(100));//加入的是对象的引用 不能是基础类型,因为基础类型放在栈区,随时都有可能清空
        System.out.println(c.size());
        System.out.println(c);
    }
}    
class Name{
    Name(String s1,String s2){
    }
}
    /*结果:
3
[hello, Name@107077e, 100]                                                                                                                                               
     */

import java.util.*;//要加上他。。    
public class TestCollection {//练习遍历容器 Iterator
    public static void main(String[] args){
        Collection c= new ArrayList();//可以放入不同类型的对象 用父类的对象指向子类 面向对象的思想
        c.add(new Name("f1","l1"));
        c.add(new Name("f2","l2"));
        c.add(new Name("f3","l3"));
        Iterator i = c.iterator();
        while(i.hasNext()){//遍历
            Name n =(Name)i.next();   
            System.out.println(n.getFistName());
        }
        System.out.println(c.size());
        System.out.println(c);
    }
}    
class Name{
    private String FirstName;
    private String SecondName;
    Name(String s1,String s2){
        this.FirstName=s1;this.SecondName=s2;
    }
    public String toString(){
        return FirstName+"   "+SecondName;
    }
    public String getFistName(){
        return FirstName;
    }
}
    /*结果:
f1
f2
f3
3
[f1   l1, f2   l2, f3   l3]                                                                                                                                            
     */


Java语言: list练习
import java.util.*;
public class TestList {
    public static void main(String[] args){
        List l1=new LinkedList();
        for(int i=0;i<=5;i++){
            l1.add("a"+i);
        }
        System.out.println(l1);
        l1.add(3,"a6");
        System.out.println(l1);
        l1.set(6,"a7");
        System.out.println(l1);
        System.out.println((String)l1.get(2)+" ");
        System.out.println(l1.indexOf("a3"));
        l1.remove(1);
        System.out.println(l1);
        Collections.shuffle(l1);//随机排列
        System.out.println(l1);
        Collections.reverse(l1);//逆序
        System.out.println(l1);
        Collections.sort(l1);
        System.out.println(l1);//排序
        System.out.println(Collections.binarySearch(l1, "a3"));//折半法查找
    }
}
/*
结果:
[a0, a1, a2, a3, a4, a5]
[a0, a1, a2, a6, a3, a4, a5]
[a0, a1, a2, a6, a3, a4, a7]
a2
4
[a0, a2, a6, a3, a4, a7]
[a3, a2, a7, a6, a4, a0]
[a0, a4, a6, a7, a2, a3]
[a0, a2, a3, a4, a6, a7]
2
*/

Java语言: map练习
import java.util.*;
public class Testmap {
    public static void main(String[] args) {
        Map m1=new     HashMap(); Map m2= new HashMap();
        m1.put("one", new Integer(1));
        m1.put("two", new Integer(2));
        m1.put("three",new Integer(3));
        m2.put("A", new Integer(1));
        m2.put("B",new Integer(2));
        System.out.println(m1.size());
        System.out.println(m1.containsKey("one"));
        System.out.println(m2.containsValue(new Integer(1)));
        if(m1.containsKey("two")){
            int i=((Integer)m1.get("two")).intValue();
            System.out.println(i);
        }
        Map m3=new HashMap(m1);
        m3.putAll(m2);
        System.out.println(m3);
    }
}
/*
结果:
3
true
true
2
{one=1, A=1, two=2, three=3, B=2}

*/

Java语言: map练习2
import java.util.*;
public class TestArgsWords {
    private    static final     Integer ONE =new Integer(1);
    //private static final int ONE = 1;
    public static void main(String[] args) {
        Map m =new HashMap();
        for(int i=0;i<args.length;i++){
        Integer freq = (Integer)m.get(args[i]);
        //int freq=(Integer)m.get(args[i])==null?0:(Integer)m.get(args[i]);
            m.put(args[i],(freq==null?ONE:new Integer(freq.intValue()+1)));
        }
        System.out.println(m.size()+"distinct words detected");
        System.out.println(m);
    }
}
/*
4distinct words detected
{a=2, aa=2, c=3, b=2}
*/

类别:java编程 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu