版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
设计题编写代码比较使用迭代器遍历链表和使用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+"毫秒"); }}编写饿汉式和懒汉式单件模式示意代码,并说明这两种方式的适应性。(1)饿汉式:publicclassSingleton{ privatestaticSingletonunigueInstance=newSingleton(); privateSingleton(){} publicstaticSingletongetInstance(){ returnunigueInstance; }}(2)懒汉式:publicclassSingleton{ privatestaticSingletonunigueInstance; privateSingleton(){} publicstaticsynchronizedSingletongetInstance(){ if(unigueInstance==null){ unigueInstance=newSingleton(); } returnunigueInstance; }}以下你看到的类和接口是取自一个动作冒险游戏,它们代表游戏角色(Character)的类和角色可以使用的武器行为的类。每个角色一次只能使用一种武器,但是可以在游戏的过程中换武器。你的工作是使用策略模式实现该游戏的架构。publicabstractclassCharacter{ WeaponBehaviorweapon; publicvoidperformWeapon(){ weapon.useWeapon(); } publicabstractvoidfight(); publicvoidsetWeaponBehavior(WeaponBehaviorw){ this.weapon=w; }}publicclassQueenextendsCharacter{ publicQueen(){ weapon=newKnifeBehavior(); } publicvoidfight(){ System.out.println("皇后在战斗。"); }}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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026上海银清企业服务有限公司招聘参考考试题库及答案解析
- 2026四川成都市双流区川大江安小学教师招聘11人参考笔试题库附答案解析
- 幼儿园校园风景摄影大赛方案摄影活动
- 2025甘肃兰州新区石化产业投资集团有限公司法务专干、造价工程师、会计岗位招聘6人备考笔试题库及答案解析
- 2026西藏民族大学急需紧缺人才引进2人备考考试试题及答案解析
- 深度解析(2026)《GBT 25936.4-2024橡胶塑料粉碎机械 第4部分团粒机安全要求》(2026年)深度解析
- 深度解析(2026)《GBT 25907.3-2010信息技术 维吾尔文、哈萨克文、柯尔克孜文编码字符集 16点阵字型 第3部分:库非白体》
- 2026年威海乳山市民兵训练基地公开招聘事业单位工作人员(1名)参考考试题库及答案解析
- 痛风抗炎症治疗指南(2025 版)解读
- 2025江苏盐城市交通运输局直属事业单位选调1人参考考试试题及答案解析
- UL347a标准中文版-2019中压电力转换设备UL标准中文版
- 北京市西城区2023-2024学年六年级上学期语文期末试卷(含答案)
- DB11T 850-2011 建筑墙体用腻子应用技术规程
- 城市轨道交通列车自动控制系统维护 课件 3.1 ZC系统认知
- 2024年天津市南开区翔宇学校四上数学期末检测模拟试题含解析
- LNG加气站管道工程施工方案
- 油漆作业风险和隐患辨识、评估分级与控制措施一览表
- NB/T 11440-2023生产煤矿储量估算规范
- 洁净工厂设计合同范本
- 无人机应用技术专业申报表
- PDCA提高卧床患者踝泵运动的执行率
评论
0/150
提交评论