各容器与迭代器的用法_第1页
各容器与迭代器的用法_第2页
各容器与迭代器的用法_第3页
各容器与迭代器的用法_第4页
各容器与迭代器的用法_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、各容器与迭代器的用法(一)关于迭代器:迭代器有两个:Enumeration已经不是主流,Iterator是它的下一代替代品.(二)关于容器:Collection FrameWork 如下:Collection(最基本的集合接口)卜List(可以包含重复元素)j 卜LinkedListi卜ArrayList (线程不安全的,就是不同步的;数据增长:默认增长为原来一半) jWector(线程安全的,也就是说是同步的;数据增长:默认增长为原来一倍)j LstackLSet(不包含重复元素)Map(Map是键-值的映射关系)卜Hashtable(基于陈旧的Dictionary类;线程安全,即同步;nu

2、ll不可作为它的key或value)卜HashMap(Map接口的一个实现;线程不安全,即不同步;null可作为它的key或value)LWeakHashMaphashtable,hashmap,arraylist,vector,iterator 的实例及总结:(2.1)总结一:vector,arraylist,hashtable,hashmap:1.都可采用iterator(hasNext()-next()来作为迭代器,也可采用for-size()-get()的方法来进行历遍.都可用toString()方法来显示整个容器的内容.都可用:if( xx!=null&!xx.isEmpty()来进

3、行判断.2.vector,arraylist:插入东西都用add(),都可用addAll()加载其它容器的内容.hashtable,hashmap:插入东西都用 put(),get(),remove(),clear(),isEmpty(),containsKey().总结二:hashtable,hashMap:用iterator迭代器时,是用其key来作为历遍的,即:xx.keySet().iterator+xx.get(it.get(i).而不像 vector,arraylist,直接 xx.iterator().总结三:hashtable 自 己本身的迭代器是 Emuneration(用方

4、法 xx.keys(),xx.elements(),W不是 iterator.总结四:将 hashtable,hashmap 的 key 放于 vector,arraylist.如下:Vector ve=new Vector(hashtable1.keySet();ArrayList al=new ArrayList(hashtable1.keySet();总结五:hashtable,hashmap的数据,后进先出;vector,arraylist的数据,先进先出;总结六:HashSet的方法与arraylist的方法,相同.但hashset不允许内容有重复.若有重复不会报错,而会 返回一个f

5、asle值.且在进行历遍时,就可看到效果了.(2.2)实例:import java.util.Enumeration;import java.util.Hashtable;import java.util.HashMap;import java.util.Vector;import java.util.ArrayList;import java.util.Iterator;public class JavaContainerDemo (public static void main(String args)Hashtable ht=new Hashtable();ht.put(0,jason)

6、;ht.put(1,test);ht.put(2,access);if(ht.contains(jason)&ht.containsKey(1)&!ht.isEmpty()ht.remove(2);System.out.println(hashtable,是先入后出);System.out.println(hashtable.keySet():+ht.keySet();System.out.println(hashtable.toString():+ht.toString();System.out.println(* 历遍方式 一:Enumeration*);Enumeration hte=

7、ht.keys();从hashtable,hashmap获取出来的key,value的数据类型都是:ObjectObject key;while(hte.hasMoreElements()/System.out.println(hashtables key:+hte.nextElement();/System.out.println(hashtables value:+ht.get(hte.nextElement();不能用这样的方式进行历遍,在一个while中,使用两次nextElement(),指针会跳两次的.应该 是:key=hte.nextElement();System.out.pr

8、intln(hashtables key:+key);System.out.println(hashtables value:+ht.get(key);System.out.println(* 历遍方式二:iterator*);System.out.println(*hashtable 是键值对存在的,所以只能用 keySet().iterator*);Iterator hti=ht.keySet().iterator();Object key2;while(hti.hasNext()key2=hti.next();System.out.println(hashtables key:+key2

9、);System.out.println(hashtables value:+ht.get(key2);System.out.println(* 历遍方式三:for*);for(int i=0;iht.size();i+)System.out.println(hashtables key:+i);System.out.println(hashtables value:+ht.get(i);/ht.clear();HashMap hm=new HashMap();/hm.put(null, null); hashmap 允许 key-value 者K为 nullhm.put(0, hwj);hm

10、.put(1, null);hm.put(2, access);if(hm.containsKey(O)&hm.containsValue(access)&!hm.isEmpty()hm.remove(l);System.out.println(hashmap,也 是先进后出);System.out.println(hashmap.keySet():+hm.keySet();System.out.println(hashmap.toString():+hm.toString();System.out.println(*hashmap 历遍方式:iterator*);Iterator hmi=h

11、m.keySet().iterator();Object key3;while(hmi.hasNext()key3=hmi.next();System.out.println(hashmaps key:+key3);System.out.println(hashmaps value:+hm.get(key3);System.out.println(*hashmap 历遍方式二:for*);for(int j=O;jhm.size();j+)System.out.println(hashmaps key:+j);System.out.println(hashmaps value:+hm.get(

12、j);/hm.clear();Vector v=new Vector(ht.keySet();System.out.println(vector 原来的大小:+v.size();v.add(中 国人”);v.addElement(香 港人”);System.out.println(利用 vector.addAll(container)去加载其它容器中的内容”);v.addAll(hm.keySet();/v.setElementAt( 数据 2, 2);if(v!=null&!v.isEmpty()System.out.println(vector 现在的大小:+v.size();System

13、.out.println(*vecto r:是先入先出 *);System.out.println(vector.toString():+v.toString();System.out.println(*vector 历遍方式:iterator*);Iterator vi=v.iterator();while(vi.hasNext()System.out.println(vectors value:+vi.next();System.out.println(*vector 历遍方式二:for*);for(int m=0;mv.size();m+)/System.out.println(vect

14、ors value:+v.get(m);/v.get(m)=v.elementAt(m),arraylis t 没此方法System.out.println(vectors value:+v.elementAt(m);ArrayList al=new ArrayList(hm.keySet();System.out.println(arraylist 原来的大小:+al.size();al.add(杰森”);System.out.println(利用 arraylist.addAll(container)去加载其它容器中的内容); al.addAll(v);if(al!=null&!al.is

15、Empty()System.out.println(arraylist 现在的大小:+al.size();System.out.println(*arraylist:是先入先出 *);System.out.println(*arraylist.toString()+al.toString();System.out.println(*arraylist 历遍方式:iterator*);Iterator ali=al.iterator();while(ali.hasNext()System.out.println(als value:+ali.next();System.out.println(*arraylist 历遍方式二:for*);for(int n=0;nal.size();n+)System.out.println(als

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论