[精选]J2ee开发技术(二).pptx
《[精选]J2ee开发技术(二).pptx》由会员分享,可在线阅读,更多相关《[精选]J2ee开发技术(二).pptx(209页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、J2ee 开发技术 liyoujn-public.sd tel:6/1/2023 1Chapter 9 HoldingyourObjects 6/1/2023 2 山东大学计算机学院人机交互与虚拟现实实验室Chapter 9:HoldingyourObjects b ThischapterexploresindepththecontainerlibrarythatJava2suppliestoholdobjectswhileyoureworkingwiththem:thesimplearraysandmoresophisticatedcontainers datastructures such
2、asArrayListandHashMap.6/1/2023 3 山东大学计算机学院人机交互与虚拟现实实验室Chapter 9:HoldingyourObjects b Arrays arraysofobjectsholdreferencesWeeblea;/NullreferenceWeebleb=newWeeble5;/NullreferencesWeeblec=newWeeble4;for inti=0;ic.length;i+ci=newWeeble;Weebled=newWeeble,newWeeble,newWeeble;a=newWeeblenewWeeble,newWeeble
3、;6/1/2023 4 山东大学计算机学院人机交互与虚拟现实实验室Chapter 9:HoldingyourObjects b Arrays arraysofprimitivesholdtheprimitivevaluesinte;/Nullreferenceintg=newint4;for inti=0;ig.length;i+gi=i*i;inth=11,47,93;e=h;hide newWeeblenewWeeble,newWeeble Containerofprimitives Containerclassescanholdonlyreferencestoobjects Arrayc
4、anreferencesorprimitives Integer,Double Returninganarray StringfloatrSet intn.6/1/2023 5 山东大学计算机学院人机交互与虚拟现实实验室Chapter 9:HoldingyourObjects b Arraysclass InJava.util equals,fill arrayname,intfromIndex,inttoIndex,value,sort,binarySearch;asList-returnaListcontainerb Copyinganarray System.arraycopy name
5、1,start,name2,start,num;b paringarrays Arrays.equals a1,a2;vsa1=a2 6/1/2023 6 山东大学计算机学院人机交互与虚拟现实实验室Chapter 9:HoldingyourObjects b Arrayelementparisons Java.lang.parableinterface pareTo/c09:pType.javab Sortinganarray Arrays.sort a,Collections.reverseOrder;Arrays.sort a,newpTypeparator;b Searchingasor
6、tedarray intlocation=Arrays.binarySearcha,r;6/1/2023 7 山东大学计算机学院人机交互与虚拟现实实验室publicinterfaceGeneratorObjectnext;/:/Fillanarrayusingagenerator:publicstaticvoidfillObjecta,Generatorgenfilla,0,a.length,gen;publicstaticvoidfillObjecta,intfrom,intto,Generatorgenforinti=from;ito;i+ai=gen.next;6/1/2023 8 山东
7、大学计算机学院人机交互与虚拟现实实验室/:c09:pType.javaImplementingparableinaclass.import.bruceeckel.util.*;importjava.util.*;publicclasspTypeimplementsparableinti;intj;publicpType intn1,intn2 i=n1;j=n2;publicStringtoString returni=+i+,j=+j+;publicintpareTo Objectrv intrvi=pType rv.i;return irvi?-1:i=rvi?0:1;privatesta
8、ticRandomr=newRandom;privatestaticintrandInt returnMath.abs r.nextInt%100;6/1/2023 9 山东大学计算机学院人机交互与虚拟现实实验室publicstaticGeneratorgeneratorreturnnewGeneratorpublicObjectnextreturnnewpTyperandInt,randInt;publicstaticvoidmainStringargspTypea=newpType10;Arrays2.filla,generator;Arrays2.printbeforesorting,a
9、=,a;Arrays.sorta;Arrays2.printaftersorting,a=,a;6/1/2023 10 山东大学计算机学院人机交互与虚拟现实实验室Chapter 9:HoldingyourObjects b Introductiontocontainers Collection:agroupofindividualelements Listmustholdtheelementsinaparticularsequence SetcannothaveanyduplicateelementsJava.lang Map:agroupofkey-valueobjectpairs Prin
10、tingcontainers/c09:PrintingContains.java FillingContainers Collections.fill name,object;Containerdisadvantage:unknowntype Makeatype-conscious 有意义的Array/c09:MouseList.java Parameterizedtype6/1/2023 11 山东大学计算机学院人机交互与虚拟现实实验室/:c09:PrintingContainers.java/Containersprintthemselvesautomatically.importjava
11、.util.*;publicclassPrintingContainersstaticCollectionfill Collectionc c.add dog;c.add dog;c.add cat;returnc;staticMapfill Mapm m.put dog,Bosco;m.put dog,Spot;m.put cat,Rags;returnm;publicstaticvoidmain Stringargs System.out.println fill newArrayList;System.out.println fill newHashSet;System.out.prin
12、tln fill newHashMap;6/1/2023 12 山东大学计算机学院人机交互与虚拟现实实验室/:c09:MouseList.javaAtype-consciousArrayList.importjava.util.*;publicclassMouseListprivateArrayListlist=newArrayList;publicvoidadd Mousem list.add m;publicMouseget intindex return Mouse list.get index;publicintsize returnlist.size;/:c09:MouseListT
13、est.javapublicclassMouseListTestpublicstaticvoidmain Stringargs MouseListmice=newMouseList;for inti=0;i3;i+mice.add newMouse i;for inti=0;imice.size;i+MouseTrap.caughtYa mice.get i;6/1/2023 13 山东大学计算机学院人机交互与虚拟现实实验室Chapter 9:HoldingyourObjects b Iterators Aniteratorisanobject,movethroughasequenceofob
14、jects,selecteachobjectGetaIteratorusingiterator.Getthenextobjectwithnext.SeeifthereareanymoreobjectswithhasNext.Removethelastelementwithremove.6/1/2023 14 山东大学计算机学院人机交互与虚拟现实实验室Container taxonomy 分类 分类 6/1/2023 15 山东大学计算机学院人机交互与虚拟现实实验室Container functionality b Collection booleanadd Object booleanaddA
15、ll Collection voidclear booleancontains Object booleancontainsAll Collection booleanisEmpty Iteratoriterator booleanremove Object booleanremoveAll Collection booleanretainAll Collection 只保存参数集合的元素 intsize ObjecttoArray 6/1/2023 16 山东大学计算机学院人机交互与虚拟现实实验室Container functionality b List List interface Or
16、deristhemostimportantfeature insertionandremovalofelementsinthemiddleofaList.produceaListIterator,ArrayList AListimplementedwithanarray.Allowsrapidrandomaccesstoelements,ListIterator-back-and-forthtraversalofanArrayList,notinsertingandremovingelements6/1/2023 17 山东大学计算机学院人机交互与虚拟现实实验室Container functi
17、onality b List LinkedList Providesoptimalsequentialaccess addFirst,addLast,getFirst,getLast,removeFirst,andremoveLast toallowittobeusedasastack,aqueue,andadeque.6/1/2023 18 山东大学计算机学院人机交互与虚拟现实实验室Container functionality b MakingastackfromaLinkedListpublicclassStackLprivateLinkedListlist=newLinkedList;
18、publicvoidpushObjectvlist.addFirstv;publicObjecttopreturnlist.getFirst;publicObjectpopreturnlist.removeFirst;6/1/2023 19 山东大学计算机学院人机交互与虚拟现实实验室Container functionality b Making a queue from a LinkedListpublicclassQueueprivateLinkedListlist=newLinkedList;publicvoidputObjectvlist.addFirstv;publicObjectg
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 精选 J2ee 开发 技术
限制150内