




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
設計題編寫代碼比較使用迭代器遍歷鏈表和使用get(intindex)方法遍歷鏈表所有的時間。mportjava.util.*;publicclassTestSpeed{ publicstaticvoidmain(String[]args){ LinkedList<String>list=newLinkedList<String>(); for(inti=0;i<=60096;i++){ list.add("speed"+i); } Iterator<String>iter=list.iterator(); longstarttime=System.currentTimeMillis(); while(iter.hasNext()){ Stringte=iter.next(); } longendTime=System.currentTimeMillis(); ; longresult=endTime-starttime; System.out.println("使用迭代遍歷集合所用時間"+result+"毫秒"); starttime=System.currentTimeMillis(); for(inti=0;i<list.size();i++){ Stringte=list.get(i); } endTime=System.currentTimeMillis(); result=endTime-starttime; System.out.println("使用get方法歷集合所用時間"+result+"毫秒"); }}編寫?zhàn)I漢式和懶漢式單件模式示意代碼,并說明這兩種方式的適應性。(1)餓漢式:publicclassSingleton{ privatestaticSingletonunigueInstance=newSingleton(); privateSingleton(){} publicstaticSingletongetInstance(){ returnunigueInstance; }}(2)懶漢式:publicclassSingleton{ privatestaticSingletonunigueInstance; privateSingleton(){} publicstaticsynchronizedSingletongetInstance(){ if(unigueInstance==null){ unigueInstance=newSingleton(); } returnunigueInstance; }}以下你看到的類和接口是取自一個動作冒險游戲,它們代表游戲角色(Character)的類和角色可以使用的武器行為的類。每個角色一次只能使用一種武器,但是可以在游戲的過程中換武器。你的工作是使用策略模式實現(xiàn)該游戲的架構。publicabstractclassCharacter{ WeaponBehaviorweapon; publicvoidperformWeapon(){ weapon.useWeapon(); } publicabstractvoidfight(); publicvoidsetWeaponBehavior(WeaponBehaviorw){ this.weapon=w; }}publicclassQueenextendsCharacter{ publicQueen(){ weapon=newKnifeBehavior(); } publicvoidfight(){ System.out.println("皇后在戰(zhàn)斗。"); }}publicclassKingextendsCharacter{publicKing(){publicinterfaceHandler{ publicabstractvoidcompuerMultiply(Stringnumber); publicabstractvoidsetNextHandler(Handlerhandler);}publicclassUseIntimplementsHandler{ privateHandlerhandler; privateintresult=1; publicvoidcompuerMultiply(Stringnumber){ try{ intn=Integer.parseInt(number); inti=1; while(i<=n){ result=result*i; if(result<=0){ System.out.println("超出我的能力范圍,我計算不了"); puerMultiply(number); return; } i++; } System.out.println(number+"的階乘:"+result); } catch(NumberFormatExceptionexp){ System.out.println(exp.toString()); } } publicvoidsetNextHandler(Handlerhandler){ this.handler=handler; }}publicclassUseLongimplementsHandler{ privateHandlerhandler; privatelongresult=1; publicvoidcompuerMultiply(Stringnumber){ try{ longn=Long.parseLong(number); longi=1; while(i<=n){ result=result*i; if(result<=0){ System.out.println("超出我的能力范圍,我計算不了"); puerMultiply(number); return; } i++; } System.out.println(number+"的階乘:"+result); } catch(NumberFormatExceptionexp){ System.out.println(exp.toString()); } } publicvoidsetNextHandler(Handlerhandler){ this.handler=handler; }}importjava.math.BigInteger;publicclassUseBigIntegerimplementsHandler{ privateHandlerhandler; privateBigIntegerresult=newBigInteger("1"); publicvoidcompuerMultiply(Stringnumber){ try{ BigIntegern=newBigInteger(number); BigIntegerONE=newBigInteger("1"); BigIntegeri=ONE; while(pareTo(n)<=0){ result=result.multiply(i); i=i.add(ONE); } System.out.println(number+"的階乘:"+result); } catch(NumberFormatExceptionexp){ System.out.println(exp.toString()); } } publicvoidsetNextHandler(Handlerhandler){ this.handler=handler; }}publicclassApplication{ privateHandleruseInt,useLong,useBig; publicvoidcreateChain(){ useInt=newUseInt(); useLong=newUseLong(); useBig=newUseBigInteger(); useInt.setNextHandler(useLong); useLong.setNextHandler(useBig); } publicvoidreponseCli
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國變電專用設備數(shù)據(jù)監(jiān)測研究報告
- 2025年雞翅木床項目可行性研究報告
- 2025年耐火線項目可行性研究報告
- 2025年捻股成繩機項目可行性研究報告
- 鄰氯甲苯行業(yè)深度研究報告
- 2025年黃銅單卡通活接閘閥項目投資可行性研究分析報告
- 2024-2025學年高中政治第四單元當代國際社會第九課維護世界和平促進共同發(fā)展課時三我國外交政策的基本目標和宗旨課時精練含解析新人教版必修2
- 2025年脲醛膠行業(yè)深度研究分析報告
- 2025年核桃殼項目投資可行性研究分析報告
- 打井合同范本乙方出合同
- 《大白菜種植栽培技》課件
- 北京工業(yè)大學《數(shù)據(jù)挖掘》2023-2024學年第一學期期末試卷
- 2024年物聯(lián)網(wǎng)安裝調試員(中級工)職業(yè)資格鑒定考試題庫(含答案)
- 標準化機房改造方案
- 珠海市第三人民醫(yī)院中醫(yī)智能臨床輔助診療系統(tǒng)建設方案
- 早產(chǎn)臨床診斷與治療指南
- 工程簽證單完整版
- 《義務教育數(shù)學課程標準(2022年版)》初中內容解讀
- 全院護理查房(食管裂孔疝)
- 川教版信息技術六年級下冊全冊教案【新教材】
- 2024-2025學年統(tǒng)編版語文九年級下冊第7課《溜索》任務驅動型教學設計
評論
0/150
提交評論