fastwind 发表于 2013-2-5 01:49:40

java常用容器用法实例(Hashtable,ArrayList,vector)

Hashtable 的用法 

package usehashtable;    import java.util.Enumeration;import java.util.Hashtable;    /**** @author Administrator*/public class UseHashtable {      //创建Hashtable 对象    Hashtable ht=new Hashtable();    public UseHashtable() {    }    public void setHash(){    ht.put("1","a");    ht.put("2","b");    ht.put("3","c");    }    public void getHash(){      //用key来标记hashtable中的索引    Enumeration key=ht.keys();      //allvalue标记hashtable中索引对应的值    Enumeration allvalue=ht.elements();    System.out.println("hashtable all keys:");    while(key.hasMoreElements())    {    String str=(String)key.nextElement();    String strv=(String)allvalue.nextElement();    System.out.println(str+" : "+strv);    }    if(ht.containsKey("1"))    System.out.println("has the value 1");    else    System.out.println("do not contain 1");    ht.remove("1");    System.out.println("after remove the value 1:");    if(ht.containsKey("1"))    System.out.println("has the value 1");    else    System.out.println("do not contain 1");    if(ht.contains("c"))    System.out.println("has the value c");    }    public static void main(String[] args){    UseHashtable hs=new UseHashtable();    hs.setHash();    hs.getHash();       }} 
Vector的用法
package usehashtable;import java.util.Vector;import java.util.Enumeration;/**** @author Administrator*/public class UseVector {       Vector mainv=new Vector();    /** Creates a new instance of Vector */    public UseVector() {    }    public void setVector(){      //按照如下实例你可以存取数据库中一条记录里所有的字段值    Vector v1=new Vector();    v1.add("1");    v1.add("a");    v1.add("1a");    mainv.add(v1);    Vector v2=new Vector();    v2.add("2");    v2.add("b");    v2.add("2b");    mainv.add(v2);    Vector v3=new Vector();    v3.add("3");    v3.add("c");    v3.add("3c");    mainv.add(v3);    }    public void getVector(){    for(int i=0;i<3;i++)    {    for(int j=0;j<3;j++)    {    System.out.print(checkNull(i,j)+" ");    }    System.out.println("");    }       }      //只有值不为空时才返回他的值    public String checkNull(int row,int col)    {    try {    if(!((String)((Vector)mainv.elementAt(row)).elementAt(col)).trim().equals(null)){    return ((String)((Vector)mainv.elementAt(row)).elementAt(col));    }    else return "";    } catch(Exception e){    return "";    }    }    public static void main(String[] args){    UseVector uv=new UseVector();    uv.setVector();    uv.getVector();    }} 

ArrayList的用法(借助一个Book类)
public class Book {    private String bookName;    private int bookPrice;    /** Creates a new instance of Book */    public Book() {    }      public int getBookPrice() {    return bookPrice;    }      public void setBookPrice(int bookPrice) {    this.bookPrice = bookPrice;    }      public String getBookName() {    return bookName;    }      public void setBookName(String bookName) {    this.bookName = bookName;    }   }    //ArrayList    package usehashtable;import java.util.ArrayList;import java.util.Iterator;/**** @author Administrator*/public class UseArraylist {       ArrayList ay=new ArrayList();    /** Creates a new instance of UseArraylist */    public UseArraylist() {    }    public void setArray(){    Book bk1=new Book();    bk1.setBookName("java");    bk1.setBookPrice(13);    ay.add(0,bk1);    Book bk2=new Book();    bk2.setBookName("jsp");    bk2.setBookPrice(30);    ay.add(bk2);    Book bk3=new Book();    bk3.setBookName("j2ee");    bk3.setBookPrice(20);    ay.add(bk3);    }    public void getArray(){    Iterator i=ay.iterator();    while(i.hasNext()){    Book bk=(Book)i.next();    System.out.println(bk.getBookName()+" ,"+bk.getBookPrice());    }    }    public static void main(String[] args){    UseArraylist list=new UseArraylist();    list.setArray();    list.getArray();    }} 
页: [1]
查看完整版本: java常用容器用法实例(Hashtable,ArrayList,vector)