201771010142 寮犵嚂& 鏉ㄨ搲搴? 瀹為獙鍗佷竴 闆嗗悎
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了201771010142 寮犵嚂& 鏉ㄨ搲搴? 瀹為獙鍗佷竴 闆嗗悎相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%a2%9e%e5%8a%a0' title='澧炲姞'>澧炲姞
瀛楀吀鎺掑簭 杈撳嚭 api ever href rop inpu port瀹為獙鍗佷竴 闆嗗悎
瀹為獙鏃堕棿 2018-11-8
涓€ 鐞嗚鐭ヨ瘑
涓€鑸皢鏁版嵁缁撴瀯鍒嗕负涓ゅぇ绫伙細绾挎€ф暟鎹粨鏋勫拰闈炵嚎鎬ф暟鎹粨鏋勩€傜嚎鎬ф暟鎹粨鏋勶細绾挎€ц〃銆佹爤銆侀槦鍒椼€佷覆銆佹暟缁勫拰鏂囦欢銆傞潪绾挎€ф暟鎹粨鏋勶細鏍戝拰鍥俱€?/p>
绾挎€ц〃鎸夊叾瀛樺偍缁撴瀯鍙垎涓洪『搴忚〃鍜岄摼琛紱鐢ㄩ『搴忓瓨鍌ㄧ粨鏋勫瓨鍌ㄧ殑绾挎€ц〃绉颁负椤哄簭琛紱椤哄簭琛ㄥ皢绾挎€ц〃涓殑鏁版嵁鍏冪礌渚濇瀛樻斁鍦ㄦ煇涓瓨鍌ㄥ尯鍩熶腑銆備竴缁存暟缁勫氨鏄敤椤哄簭鏂瑰紡瀛樺偍鐨勭嚎鎬ц〃銆傜敤閾惧紡瀛樺偍缁撴瀯瀛樺偍鐨勭嚎鎬ц〃绉颁负閾捐〃銆?/p>
鏍?Stack)涔熸槸涓€绉嶇壒娈婄殑绾挎€ц〃锛屾槸涓€绉嶅悗杩涘厛鍑?(LIFO)鐨勭粨鏋勩€傛爤鏄檺瀹氫粎鍦ㄨ〃灏捐繘琛屾彃鍏ュ拰鍒犻櫎杩愮畻鐨勭嚎鎬ц〃锛岃〃灏剧О涓烘爤椤?top)锛岃〃澶寸О涓烘爤搴?bottom)銆傛爤鐨勭墿鐞嗗瓨鍌ㄥ彲浠ョ敤椤哄簭瀛樺偍缁撴瀯锛屼篃鍙互鐢ㄩ摼寮忓瓨鍌ㄧ粨鏋勩€?/p>
闃熷垪(Queue)鏄檺瀹氭墍鏈夌殑鎻掑叆鍙兘鍦ㄨ〃鐨勪竴绔繘琛?锛岃€屾墍鏈夌殑鍒犻櫎閮藉湪琛ㄧ殑鍙︿竴绔繘琛岀殑绾挎€ц〃銆傝〃涓厑璁告彃鍏ョ殑涓€绔О涓洪槦灏?Rear)锛屽厑璁稿垹闄ょ殑涓€绔О涓洪槦澶?Front)銆傞槦鍒楃殑鎿嶄綔鏄寜鍏堣繘鍏堝嚭(FIFO)鐨勫師鍒欒繘琛岀殑銆傞槦鍒楃殑鐗╃悊瀛樺偍鍙互鐢ㄩ『搴忓瓨鍌ㄧ粨鏋勶紝涔熷彲浠ョ敤閾惧紡瀛樺偍缁撴瀯銆?/p>
鏁e垪琛ㄥ張绉颁负鍝堝笇琛ㄣ€傛暎鍒楄〃绠楁硶鐨勫熀鏈€濇兂鏄細浠ョ粨鐐圭殑鍏抽敭瀛椾负鑷彉閲忥紝閫氳繃涓€瀹氱殑鍑芥暟鍏崇郴锛堟暎鍒楀嚱鏁帮級璁$畻鍑哄搴旂殑鍑芥暟鍊硷紝浠ヨ繖涓€间綔涓鸿缁撶偣瀛樺偍鍦ㄦ暎鍒楄〃涓殑鍦板潃銆傚綋鏁e垪琛ㄤ腑鐨勫厓绱犲瓨鏀惧お婊★紝灏卞繀椤昏繘琛屽啀鏁e垪锛屽皢浜х敓涓€涓柊鐨勬暎鍒楄〃锛屾墍鏈夊厓绱犲瓨鏀惧埌鏂扮殑鏁e垪琛ㄤ腑锛屽師鍏堢殑鏁e垪琛ㄥ皢琚垹闄ゃ€傚湪Java璇█涓紝閫氳繃璐熻浇鍥犲瓙(load factor)鏉ュ喅瀹氫綍鏃跺鏁e垪琛ㄨ繘琛屽啀鏁e垪銆傝礋杞藉洜瀛愯秺楂?(瓒婃帴杩?.0)锛屽唴瀛樼殑浣跨敤鏁堢巼瓒婇珮锛屽厓绱犵殑瀵绘壘鏃堕棿瓒婇暱銆傝礋杞藉洜瀛愯秺浣?(瓒婃帴杩?.0)锛屽厓绱犵殑瀵绘壘鏃堕棿瓒婄煭锛屽唴瀛樻氮璐硅秺澶氥€侶ashSet绫荤殑缂虹渷璐熻浇鍥犲瓙鏄?.75銆?/p>
JAVA鐨勯泦鍚堟鏋跺疄鐜板鍚勭鏁版嵁缁撴瀯鐨勫皝瑁咃紝浠ラ檷浣庡鏁版嵁绠$悊涓庡鐞嗙殑闅惧害銆傛墍璋撴鏋跺氨鏄竴涓被搴撶殑闆嗗悎锛屾鏋朵腑鍖呭惈寰堝瓒呯被锛岀紪绋嬭€呭垱寤鸿繖浜涜秴绫荤殑瀛愮被鍙緝鏂逛究鐨勮璁¤璁$▼搴忔墍闇€鐨勭被銆?/p>
闆嗗悎妗嗘灦锛欽AVA闆嗗悎绫诲簱鐨勭粺涓€鏋舵瀯銆?/p>
闆嗗悎绫荤殑浣滅敤锛欽ava鐨勯泦鍚堢被鎻愪緵浜嗕竴浜涘熀鏈暟鎹粨鏋勭殑鏀寔銆?/p>
闆嗗悎绫荤殑浣跨敤锛?Java鐨勯泦鍚堢被鍖呭惈鍦╦ava.util鍖呬腑銆?/p>
闆嗗悎绫荤殑鐗圭偣涓€锛氬彧瀹圭撼瀵硅薄銆傛敞鎰忥細鏁扮粍鍙互瀹圭撼鍩烘湰鏁版嵁绫诲瀷鏁版嵁鍜屽璞°€傚鏋滈泦鍚堢被涓兂浣跨敤鍩烘湰鏁版嵁绫诲瀷锛屽張鎯冲埄鐢ㄩ泦鍚堢被鐨勭伒娲绘€э紝鍙互鎶婂熀鏈暟鎹被鍨嬫暟鎹皝瑁呮垚璇ユ暟鎹被鍨嬬殑鍖呰鍣ㄥ璞★紝鐒跺悗鏀惧叆闆嗗悎涓鐞嗐€?/p>
鐗圭偣浜岋細闆嗗悎绫诲绾崇殑瀵硅薄閮芥槸Object绫荤殑瀹炰緥锛屼竴鏃︽妸涓€涓璞$疆鍏ラ泦鍚堢被涓紝瀹冪殑绫讳俊鎭皢涓㈠け锛岃繖鏍疯璁$殑鐩殑鏄负浜嗛泦鍚堢被鐨勯€氱敤鎬с€傚洜涓篛bject绫绘槸鎵€鏈夌被鐨勭鍏堬紝鎵€浠ュ彲浠ュ湪杩欎簺闆嗗悎涓瓨鏀句换浣曠被鐨勫璞¤€屼笉鍙楅檺鍒讹紝浣嗗湪浣跨敤闆嗗悎鎴愬憳涔嬪墠蹇呴』瀵瑰畠閲嶆柊閫犲瀷銆?/p>
Vector绫荤被浼奸暱搴﹀彲鍙樼殑鏁扮粍銆俈ector涓彧鑳藉瓨鏀惧璞°€俈ector鐨勫厓绱犻€氳繃涓嬫爣杩涜璁块棶銆?Vector绫诲叧閿睘鎬э細capacity琛ㄧず闆嗗悎鏈€澶氳兘瀹圭撼鐨勫厓绱犱釜鏁般€俢apacityIncrement琛ㄧず姣忔澧炲姞澶氬皯瀹归噺銆俿ize琛ㄧず闆嗗悎褰撳墠鍏冪礌涓暟銆?/p>
Stack绫绘槸Vector鐨勫瓙绫汇€係tack绫绘弿杩板爢鏍堟暟鎹粨鏋勶紝鍗矻IFO銆?/p>
Hashtable閫氳繃閿潵鏌ユ壘鍏冪礌銆侶ashtable鐢ㄦ暎鍒楃爜锛坔ashcode锛夋潵纭畾閿€傛墍鏈夊璞¢兘鏈変竴涓暎鍒楃爜锛屽彲浠ラ€氳繃Object绫荤殑hashCode()鏂规硶鑾峰緱銆?/p>
闆嗗悎妗嗘灦涓殑鍩烘湰鎺ュ彛锛欳ollection锛氶泦鍚堝眰娆′腑鐨勬牴鎺ュ彛锛孞DK鏈彁渚涜繖涓帴鍙g殑鐩存帴瀹炵幇绫汇€?/p>
Set锛氫笉鑳藉寘鍚噸澶嶇殑鍏冪礌銆傚璞″彲鑳戒笉鏄寜瀛樻斁鐨勬搴忓瓨鏀撅紝涔熷氨鏄涓嶈兘鍍忔暟缁勪竴鏍锋寜绱㈠紩鐨勬柟寮忚繘琛岃闂紝SortedSet鏄竴涓寜鐓у崌搴忔帓鍒楀厓绱犵殑Set銆?/p>
List锛氭槸涓€涓湁搴忕殑闆嗗悎锛屽彲浠ュ寘鍚噸澶嶇殑鍏冪礌銆傛彁渚涗簡鎸夌储寮曡闂殑鏂瑰紡銆?/p>
Map锛氬寘鍚簡key-value瀵广€侻ap涓嶈兘鍖呭惈閲嶅鐨刱ey銆?/p>
SortedMap鏄竴涓寜鐓у崌搴忔帓鍒梜ey鐨凪ap銆?/p>
Collection鎺ュ彛锛氭瀯閫犵被闆嗘鏋剁殑鍩虹銆?/p>
List鐨勬槑鏄剧壒寰佹槸瀹冪殑鍏冪礌閮芥湁涓€涓‘瀹氱殑椤哄簭銆傚疄鐜板畠鐨勭被鏈堿rrayList鍜孡inkedList銆侫rrayList涓殑鍏冪礌鍦ㄥ唴瀛樹腑鏄『搴忓瓨鍌ㄧ殑銆侺inkedList涓殑鍏冪礌鍦ㄥ唴瀛樹腑鏄互閾捐〃鏂瑰紡瀛樺偍鐨勩€?/p>
ArrayList锛氬彲浠ュ皢鍏剁湅浣滄槸鑳藉鑷姩澧為暱瀹归噺鐨勬暟缁勩€傚埄鐢ˋrrayList鐨則oArray()杩斿洖涓€涓暟缁勩€侫rrays.asList()杩斿洖涓€涓垪琛ㄣ€侺inkedList鏄噰鐢ㄥ弻鍚戝惊鐜摼琛ㄥ疄鐜扮殑銆傚埄鐢↙inkedList瀹炵幇鏍?stack)銆侀槦鍒?queue)銆佸弻鍚戦槦鍒?(double-ended queue )銆侫rrayList搴曞眰閲囩敤鏁扮粍瀹屾垚锛岃€孡inkedList鍒欐槸浠ヤ竴鑸殑鍙屽悜閾捐〃(double-linked list)瀹屾垚锛屽叾鍐呮瘡涓璞¢櫎浜嗘暟鎹湰韬锛岃繕鏈変袱涓紩鐢紝鍒嗗埆鎸囧悜鍓嶄竴涓厓绱犲拰鍚庝竴涓厓绱犮€傚鏋滅粡甯稿湪 List 涓繘琛屾彃鍏ュ拰鍒犻櫎鎿嶄綔锛屽簲璇ヤ娇鐢↙inkedList锛屽惁鍒欙紝浣跨敤ArrayList灏嗘洿鍔犲揩閫熴€?/p>
Set涓殑鍏冪礌蹇呴』鍞竴銆傛坊鍔犲埌Set涓殑瀵硅薄鍏冪礌蹇呴』瀹氫箟equals鏂规硶锛屼互鎻愪緵绠楁硶鏉ュ垽鏂娣诲姞杩涙潵鐨勫璞℃槸鍚︿笌宸茬粡瀛樺湪鐨勬煇瀵硅薄鐩哥瓑锛屼粠鑰屽缓绔嬪璞$殑鍞竴鎬с€傚疄鐜癝et鎺ュ彛鐨勭被鏈塇ashSet锛孴reeSet銆?/p>
TreeSet鏄竴涓湁搴忛泦鍚堬紝TreeSet涓厓绱犲皢鎸夌収鍗囧簭鎺掑垪锛岀己鐪佹槸鎸夌収鑷劧椤哄簭杩涜鎺掑垪锛屾剰鍛崇潃TreeSet涓厓绱犺瀹炵幇Comparable鎺ュ彛銆傚彲浠ュ湪鏋勯€燭reeSet瀵硅薄鏃讹紝浼犻€掑疄鐜颁簡 Comparator鎺ュ彛鐨勬瘮杈冨櫒瀵硅薄銆侶ashSet鏄熀浜嶩ash绠楁硶瀹炵幇鐨勶紝鍏舵€ц兘閫氬父閮戒紭浜嶵reeSet銆傞€氬父浣跨敤HashSet锛岄渶瑕佹帓搴忕殑鍔熻兘鏃讹紝浣跨敤TreeSet銆?/p>
Map鎺ュ彛鐢ㄦ潵缁存寔寰堝鈥滈敭锛嶅€尖€濆锛屼互渚块€氳繃閿潵鏌ユ壘鐩稿簲鐨勫€笺€侶ashMap鍩轰簬鏁e垪琛ㄥ疄鐜帮紙鏇夸唬Hashtable锛夈€俆reeMap鍦ㄤ竴涓簩鍙夋爲鐨勫熀纭€涓婂疄鐜般€?/p>
鏄犲皠锛坢ap锛夋槸涓€涓瓨鍌ㄥ叧閿瓧鍜屽€肩殑鍏宠仈鎴栧叧閿瓧/鍊煎鐨勫璞°€傜粰瀹氫竴涓叧閿瓧锛屽彲浠ュ緱鍒板畠鐨勫€笺€傚叧閿瓧鍜屽€奸兘鏄璞°€傚叧閿瓧蹇呴』鏄敮涓€鐨勩€備絾鍊兼槸鍙互琚鍒剁殑銆?/p>
Map鎺ュ彛鏄犲皠鍞竴鍏抽敭瀛楀埌鍊笺€傚叧閿瓧锛坘ey锛夋槸浠ュ悗鐢ㄤ簬妫€绱㈠€肩殑瀵硅薄銆傜粰瀹氫竴涓叧閿瓧鍜屼竴涓€硷紝鍙互瀛樺偍杩欎釜鍊煎埌涓€涓狹ap瀵硅薄涓€傚綋杩欎釜鍊艰瀛樺偍浠ュ悗锛屽氨鍙互浣跨敤瀹冪殑鍏抽敭瀛楁潵妫€绱㈠畠
Map寰幆浣跨敤涓や釜鍩烘湰鎿嶄綔锛歡et( )鍜宲ut( )銆備娇鐢?put( )鏂规硶鍙互灏嗕竴涓寚瀹氫簡鍏抽敭瀛楀拰鍊肩殑鍊煎姞鍏ユ槧灏勩€備负浜嗗緱鍒板€硷紝鍙互閫氳繃灏嗗叧閿瓧浣滀负鍙傛暟鏉ヨ皟鐢?get( )鏂规硶銆傝皟鐢ㄨ繑鍥炶鍊笺€?/p>
Map鎺ュ彛鐨勫疄鐜扮被涓昏鏈塇ashMap锛孴reeMap锛孒ashtable锛孭roperties銆侶ashMap瀵筴ey杩涜鏁e垪銆俆reeMap鎸夌収key杩涜鎺掑簭銆傚拰Set绫讳技锛孒ashMap鐨勯€熷害閫氬父閮芥瘮TreeMap蹇紝鍙湁鍦ㄩ渶瑕佹帓搴忕殑鍔熻兘鐨勬椂鍊欙紝鎵嶄娇鐢═reeMap銆?/p>
浜屻€佸疄楠岄儴鍒?/strong>
1銆佸疄楠岀洰鐨勪笌瑕佹眰
(1) 鎺屾彙Vetor銆丼tack銆丠ashtable涓変釜绫荤殑鐢ㄩ€斿強甯哥敤API锛?/p>
(2) 浜嗚Вjava闆嗗悎妗嗘灦浣撶郴缁勬垚锛?/span>
(3) 鎺屾彙ArrayList銆丩inkList涓や釜绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>
(4) 浜嗚ВHashSet绫汇€乀reeSet绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>
(5)浜嗚ВHashMap銆乀reeMap涓や釜绫荤殑鐢ㄩ€斿強甯哥敤API锛?/span>
(6) 缁撳缂栫▼锛?span style="font-family: "Times New Roman"">Pair programming锛夌粌涔狅紝浣撻獙绋嬪簭寮€鍙戜腑鐨勪袱浜哄悎浣溿€?/span>
1銆佸疄楠岀洰鐨勪笌瑕佹眰
(1) 鎺屾彙Vetor銆丼tack銆丠ashtable涓変釜绫荤殑鐢ㄩ€斿強甯哥敤API锛?/p>
(2) 浜嗚Вjava闆嗗悎妗嗘灦浣撶郴缁勬垚锛?/span>
(3) 鎺屾彙ArrayList銆丩inkList涓や釜绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>
(4) 浜嗚ВHashSet绫汇€乀reeSet绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>
(5)浜嗚ВHashMap銆乀reeMap涓や釜绫荤殑鐢ㄩ€斿強甯哥敤API锛?/p>
(6) 缁撳缂栫▼锛?span style="font-family: Times New Roman">Pair programming锛夌粌涔狅紝浣撻獙绋嬪簭寮€鍙戜腑鐨勪袱浜哄悎浣溿€?/p>
2銆佸疄楠屽唴瀹瑰拰姝ラ
瀹為獙1锛?nbsp;瀵煎叆绗?绔犵ず渚嬬▼搴忥紝娴嬭瘯绋嬪簭骞惰繘琛屼唬鐮佹敞閲娿€?/p>
娴嬭瘯绋嬪簭1锛?/p>
l 浣跨敤JDK鍛戒护杩愯缂栬緫銆佽繍琛屼互涓嬩笁涓ず渚嬬▼搴忥紝缁撳悎杩愯缁撴灉鐞嗚В绋嬪簭锛?/p>
l 鎺屾彙Vetor銆丼tack銆丠ashtable涓変釜绫荤殑鐢ㄩ€斿強甯哥敤API銆?nbsp;
//绀轰緥绋嬪簭1 import java.util.Vector;
class Cat { private int catNumber;
Cat(int i) { catNumber = i; }
void print() { System.out.println("Cat #" + catNumber); } }
class Dog { private int dogNumber;
Dog(int i) { dogNumber = i; }
void print() { System.out.println("Dog #" + dogNumber); } }
public class CatsAndDogs { public static void main(String[] args) { Vector cats = new Vector(); for (int i = 0; i < 7; i++) cats.addElement(new Cat(i)); cats.addElement(new Dog(7)); for (int i = 0; i < cats.size(); i++) ((Cat) cats.elementAt(i)).print(); } } |
//绀轰緥绋嬪簭2 import java.util.*;
public class Stacks { static String[] months = { "1", "2", "3", "4" };
public static void main(String[] args) { Stack stk = new Stack(); for (int i = 0; i < months.length; i++) stk.push(months[i]); System.out.println(stk); System.out.println("element 2=" + stk.elementAt(2)); while (!stk.empty()) System.out.println(stk.pop()); } } |
//绀轰緥绋嬪簭3 import java.util.*;
class Counter { int i = 1;
public String toString() { return Integer.toString(i); } }
public class Statistics { public static void main(String[] args) { Hashtable ht = new Hashtable(); for (int i = 0; i < 10000; i++) { Integer r = new Integer((int) (Math.random() * 20)); if (ht.containsKey(r)) ((Counter) ht.get(r)).i++; else ht.put(r, new Counter()); } System.out.println(ht); } } |
绀轰緥绋嬪簭涓€锛?/p>
淇敼绋嬪簭閿欒鍚庡涓嬪浘
package shiyan; //绀轰緥绋嬪簭1 import java.util.Vector; class Cat { private int catNumber; Cat(int i) { catNumber = i; } void print() { System.out.println("Cat #" + catNumber); } } class Dog { private int dogNumber; Dog(int i) { dogNumber = i; } void print() { System.out.println("Dog #" + dogNumber); } } public class CatsAndDogs { public static void main(String[] args) { Vector cats = new Vector();//鐢熶骇cats绫婚泦鍚?/span> for (int i = 0; i < 7; i++) cats.addElement(new Cat(i)); cats.addElement(new Dog(7)); for (int i = 0; i < cats.size(); i++) //鍒ゆ柇鏄惁涓篶ats绫诲瀷鐨勬暟鎹紝濡傛灉鏄寜Cat杈撳嚭 if(cats.elementAt(i) instanceof Cat) ((Cat) cats.elementAt(i)).print(); //鑻ヤ笉鏄痗ats绫诲瀷鐨勬暟鎹紝鎸塂og杈撳嚭 else ((Dog) cats.elementAt(i)).print(); } }
绀轰緥绋嬪簭2锛?/p>
package shiyan; //绀轰緥绋嬪簭2 import java.util.*; public class Stacks { static String[] months = { "1", "2", "3", "4" }; public static void main(String[] args) { Stack stk = new Stack(); for (int i = 0; i < months.length; i++) stk.push(months[i]);//鎶婇」鍘嬪叆鏍堥《 System.out.println(stk); System.out.println("element 2=" + stk.elementAt(2)); while (!stk.empty())//褰撴爤椤跺厓绱犱笉涓虹┖鏃讹紝绉婚櫎鏍堥《瀵硅薄骞朵綔涓烘鍑芥暟鐨勫€艰繑鍥炲璞?/span> System.out.println(stk.pop()); } }
绀轰緥绋嬪簭3
package shiyan; import java.util.*; class Counter { int i = 1; public String toString() { return Integer.toString(i); } } public class Statistics { public static void main(String[] args) { Hashtable ht = new Hashtable();//鐢熸垚Hashtable绫诲璞t // for (int i = 0; i < 10000; i++) { Integer r = new Integer((int) (Math.random() * 20));//鐢熸垚浜?0涓暣鍨嬪疄鏁拌浆鎹㈡垚鍖呰鍣ㄧ被瀵硅薄鍚庣敤r鍘诲紩鐢?/span> if (ht.containsKey(r))//containsKey鏂规硶鐢ㄦ潵鍒ゆ柇r鏄笉鏄疕ashtable琛ㄧ殑闂村€?/span> ((Counter) ht.get(r)).i++;//閫氳繃get鏂规硶寰楀埌闂村嚭鐜扮殑value鍊硷紝鏍规嵁r鍑虹幇鐨勬鏁拌鏁板櫒涓嶆柇绱姞 else ht.put(r, new Counter());//濡傛灉闂翠笉瀛樺湪锛岄€氳繃put鏂规硶鏂板缓鍊?/span> } System.out.println(ht); } }
娴嬭瘯绋嬪簭2锛?/p>
l 浣跨敤JDK鍛戒护缂栬緫杩愯ArrayListDemo鍜孡inkedListDemo涓や釜绋嬪簭锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱
import java.util.*;
public class ArrayListDemo { public static void main(String[] argv) { ArrayList al = new ArrayList(); // Add lots of elements to the ArrayList... al.add(new Integer(11)); al.add(new Integer(12)); al.add(new Integer(13)); al.add(new String("hello")); // First print them out using a for loop. System.out.println("Retrieving by index:"); for (int i = 0; i < al.size(); i++) { System.out.println("Element " + i + " = " + al.get(i)); } } } |
import java.util.*; public class LinkedListDemo { public static void main(String[] argv) { LinkedList l = new LinkedList(); l.add(new Object()); l.add("Hello"); l.add("zhangsan"); ListIterator li = l.listIterator(0); while (li.hasNext()) System.out.println(li.next()); if (l.indexOf("Hello") < 0) System.err.println("Lookup does not work"); else System.err.println("Lookup works"); }
} |
package shiyan; import java.util.*; public class ArrayListDemo { public static void main(String[] argv) { ArrayList al = new ArrayList(); // Add lots of elements to the ArrayList... al.add(new Integer(11)); al.add(new Integer(12)); al.add(new Integer(13)); al.add(new String("hello")); // First print them out using a for loop. System.out.println("Retrieving by index:"); for (int i = 0; i < al.size(); i++) { System.out.println("Element " + i + " = " + al.get(i)); } } }
package shiyan; import java.util.*; public class LinkedListDemo { public static void main(String[] argv) { LinkedList l = new LinkedList(); l.add(new Object()); l.add("Hello"); l.add("zhangsan"); ListIterator li = l.listIterator(0); while (li.hasNext()) System.out.println(li.next()); if (l.indexOf("Hello") < 0) System.err.println("Lookup does not work"); else System.err.println("Lookup works"); } }
l 鍦‥lipse鐜涓嬬紪杈戣繍琛岃皟璇曟暀鏉?60椤电▼搴?-1锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱
package linkedList; import java.util.*; /** * This program demonstrates operations on linked lists. * @version 1.11 2012-01-26 * @author Cay Horstmann */ public class LinkedListTest { public static void main(String[] args) { List<String> a = new LinkedList<>(); a.add("Amy"); a.add("Carl"); a.add("Erica"); List<String> b = new LinkedList<>(); b.add("Bob"); b.add("Doug"); b.add("Frances"); b.add("Gloria"); // merge the words from b into a ListIterator<String> aIter = a.listIterator(); Iterator<String> bIter = b.iterator(); while (bIter.hasNext()) { if (aIter.hasNext()) aIter.next(); aIter.add(bIter.next()); } System.out.println(a); // remove every second word from b bIter = b.iterator(); while (bIter.hasNext()) { bIter.next(); // skip one element if (bIter.hasNext()) { bIter.next(); // skip next element bIter.remove(); // remove that element } } System.out.println(b); // bulk operation: remove all words in b from a a.removeAll(b); System.out.println(a); } }
l 鎺屾彙ArrayList銆丩inkList涓や釜绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>
娴嬭瘯绋嬪簭3锛?/p>
l 杩愯SetDemo绋嬪簭锛岀粨鍚堣繍琛岀粨鏋滅悊瑙g▼搴忥紱
import java.util.*; public class SetDemo { public static void main(String[] argv) { HashSet h = new HashSet(); //涔熷彲浠?nbsp;Set h=new HashSet() h.add("One"); h.add("Two"); h.add("One"); // DUPLICATE h.add("Three"); Iterator it = h.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } } |
package linkedList; import java.util.*; public class SetDemo { public static void main(String[] argv) { HashSet h = new HashSet(); //涔熷彲浠?Set h=new HashSet() h.add("One"); h.add("Two"); h.add("One"); // DUPLICATE h.add("Three"); Iterator it = h.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
l 鍦‥lipse鐜涓嬭皟璇曟暀鏉?65椤电▼搴?-2锛岀粨鍚堣繍琛岀粨鏋滅悊瑙g▼搴忥紱浜嗚ВHashSet绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>
package set; import java.util.*; /** * This program uses a set to print all unique words in System.in. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class SetTest { public static void main(String[] args) { Set<String> words = new HashSet<>(); // HashSet implements Set long totalTime = 0; try (Scanner in = new Scanner(System.in)) { while (in.hasNext()) { String word = in.next(); long callTime = System.currentTimeMillis(); words.add(word); callTime = System.currentTimeMillis() - callTime; totalTime += callTime; } } Iterator<String> iter = words.iterator(); for (int i = 1; i <= 20 && iter.hasNext(); i++) System.out.println(iter.next()); System.out.println(". . ."); System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds."); } }
l 鍦‥lipse鐜涓嬭皟璇曟暀鏉?67椤?368绋嬪簭9-3銆?-4锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱浜嗚ВTreeSet绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>
package treeSet; import java.util.*; /** * This program sorts a set of item by comparing their descriptions. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class TreeSetTest { public static void main(String[] args) { SortedSet<Item> parts = new TreeSet<>(); parts.add(new Item("Toaster", 1234)); parts.add(new Item("Widget", 4562)); parts.add(new Item("Modem", 9912)); System.out.println(parts); NavigableSet<Item> sortByDescription = new TreeSet<>( Comparator.comparing(Item::getDescription)); sortByDescription.addAll(parts); System.out.println(sortByDescription); } }
package treeSet; import java.util.*; /** * An item with a description and a part number. */ public class Item implements Comparable<Item> { private String description; private int partNumber; /** * Constructs an item. * * @param aDescription * the item鈥榮 description * @param aPartNumber * the item鈥榮 part number */ public Item(String aDescription, int aPartNumber) { description = aDescription; partNumber = aPartNumber; } /** * Gets the description of this item. * * @return the description */ public String getDescription() { return description; } public String toString() { return "[description=" + description + ", partNumber=" + partNumber + "]"; } public boolean equals(Object otherObject) { if (this == otherObject) return true; if (otherObject == null) return false; if (getClass() != otherObject.getClass()) return false; Item other = (Item) otherObject; return Objects.equals(description, other.description) && partNumber == other.partNumber; } public int hashCode() { return Objects.hash(description, partNumber); } public int compareTo(Item other) { int diff = Integer.compare(partNumber, other.partNumber); return diff != 0 ? diff : description.compareTo(other.description); } }
娴嬭瘯绋嬪簭4锛?/p>
l 浣跨敤JDK鍛戒护杩愯HashMapDemo绋嬪簭锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱
import java.util.*; public class HashMapDemo { public static void main(String[] argv) { HashMap h = new HashMap(); // The hash maps from company name to address. h.put("Adobe", "Mountain View, CA"); h.put("IBM", "White Plains, NY"); h.put("Sun", "Mountain View, CA"); String queryString = "Adobe"; String resultString = (String)h.get(queryString); System.out.println("They are located in: " + resultString); } } |
l
package set; import java.util.*; public class HashMapDemo { public static void main(String[] argv) { HashMap h = new HashMap(); // The hash maps from company name to address. h.put("Adobe", "Mountain View, CA"); h.put("IBM", "White Plains, NY"); h.put("Sun", "Mountain View, CA"); String queryString = "Adobe"; String resultString = (String)h.get(queryString); System.out.println("They are located in: " + resultString); } }
鍦‥lipse鐜涓嬭皟璇曟暀鏉?73椤电▼搴?-6锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱
l 浜嗚ВHashMap銆乀reeMap涓や釜绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>
package map; import java.util.*; /** * This program demonstrates the use of a map with key type String and value type Employee. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class MapTest { public static void main(String[] args) { Map<String, Employee> staff = new HashMap<>(); staff.put("144-25-5464", new Employee("Amy Lee")); staff.put("567-24-2546", new Employee("Harry Hacker")); staff.put("157-62-7935", new Employee("Gary Cooper")); staff.put("456-62-5527", new Employee("Francesca Cruz")); // print all entries System.out.println(staff); // remove an entry staff.remove("567-24-2546"); // replace an entry staff.put("456-62-5527", new Employee("Francesca Miller")); // look up a value System.out.println(staff.get("157-62-7935")); // iterate through all entries staff.forEach((k, v) -> System.out.println("key=" + k + ", value=" + v)); } }
package map; /** * A minimalist employee class for testing purposes. */ public class Employee { private String name; private double salary; /** * Constructs an employee with $0 salary. * @param n the employee name */ public Employee(String name) { this.name = name; salary = 0; } public String toString() { return "[name=" + name + ", salary=" + salary + "]"; } }
瀹為獙2锛氱粨瀵圭紪绋嬬粌涔狅細
l 鍏充簬缁撳缂栫▼锛氫互涓嬪浘鐗囨槸涓€涓粨瀵圭紪绋嬪満鏅細涓や綅瀛︿範浼欎即鍧愬湪涓€璧凤紝闈㈠鐫€鍚屼竴鍙版樉绀哄櫒锛屼娇鐢ㄧ潃鍚屼竴閿洏锛屽悓涓€涓紶鏍囷紝浠栦滑涓€璧锋€濊€冮棶棰橈紝涓€璧峰垎鏋愰棶棰橈紝涓€璧风紪鍐欑▼搴忋€?/p>
l 鍏充簬缁撳缂栫▼鐨勯槓杩板彲鍙傝浠ヤ笅閾炬帴锛?/p>
http://www.cnblogs.com/xinz/archive/2011/08/07/2130332.html
http://en.wikipedia.org/wiki/Pair_programming
l 瀵逛簬缁撳缂栫▼涓唬鐮佽璁¤鑼冪殑瑕佹眰鍙傝€冿細
http://www.cnblogs.com/xinz/archive/2011/11/20/2255971.html
浠ヤ笅瀹為獙锛屽氨璁╂垜浠潵浣撻獙涓€涓嬬粨瀵圭紪绋嬬殑榄呭姏銆?/p>
l 纭畾鏈瀹為獙缁撳缂栫▼鍚堜綔浼欎即锛涙潹钃夊簡
l 鍚勮嚜杩愯鍚堜綔浼欎即瀹為獙涔濈紪绋嬬粌涔?锛岀粨鍚堜娇鐢ㄤ綋楠屽鎵€杩愯绋嬪簭鎻愬嚭瀹屽杽寤鸿锛?/p>
l
package ID; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; @SuppressWarnings("unused") public class Main{ private static ArrayList<Person> Personlist; @SuppressWarnings("resource") public static void main(String[] args) { Personlist = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("E:\鏂板缓鏂囦欢澶筡韬唤璇佸彿.txt"); //鎹曡幏浠g爜 try { FileInputStream fis = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String temp = null; while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String ID = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String place =linescanner.nextLine(); Person Person = new Person(); Person.setname(name); Person.setID(ID); Person.setsex(sex); int a = Integer.parseInt(age); Person.setage(a); Person.setbirthplace(place); Personlist.add(Person); } } catch (FileNotFoundException e) { System.out.println("鏌ユ壘涓嶅埌淇℃伅"); e.printStackTrace(); } catch (IOException e) { System.out.println("淇℃伅璇诲彇鏈夎"); e.printStackTrace(); } boolean isTrue = true; while (isTrue) { System.out.println("鈥斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€?); System.out.println("1:鎸夊鍚嶅瓧鍏稿簭杈撳嚭浜哄憳淇℃伅"); System.out.println("2:鏌ヨ鏈€澶у勾榫勪汉鍛樹俊鎭拰鏈€灏忓勾榫勪汉鍛樹俊鎭?); System.out.println("3锛氳緭鍏ヤ綘鐨勫勾榫?鏌ヨ骞撮緞涓庝綘鏈€杩戜汉鐨勬墍鏈変俊鎭?); System.out.println("4:鏌ヨ浜哄憳涓槸鍚︽湁浣犵殑鍚屼埂"); int nextInt = scanner.nextInt(); switch (nextInt) { case 1: Collections.sort(Personlist); System.out.println(Personlist.toString()); break; case 2: int max=0,min=100;int j,k1 = 0,k2=0; for(int i=1;i<Personlist.size();i++) { j=Personlist.get(i).getage(); if(j>max) { max=j; k1=i; } if(j<min) { min=j; k2=i; } } System.out.println("骞撮緞鏈€澶э細"+Personlist.get(k1)); System.out.println("骞撮緞鏈€灏忥細"+Personlist.get(k2)); break; case 3: System.out.println("place?"); String find = scanner.next(); String place=find.substring(0,3); String place2=find.substring(0,3); for (int i = 0; i <Personlist.size(); i++) { if(Personlist.get(i).getbirthplace().substring(1,4).equals(place)) System.out.println(""+Personlist.get(i)); } break; case 4: System.out.println("骞撮緞锛?); int yourage = scanner.nextInt(); int near=agenear(yourage); int d_value=yourage-Personlist.get(near).getage(); System.out.println(""+Personlist.get(near)); /* for (int i = 0; i < Personlist.size(); i++) { int p=Personlist.get(i).getage()-yourage; if(p<0) p=-p; if(p==d_value) System.out.println(Personlist.get(i)); } */ break; case 5: isTrue = false; System.out.println("閫€鍑虹▼搴?"); break; default: System.out.println("杈撳叆鏈夎"); } } } public static int agenear(int age) { int j=0,min=53,d_value=0,k=0; for (int i = 0; i < Personlist.size(); i++) { d_value=Personlist.get(i).getage()-age; if(d_value<0) d_value=-d_value; if (d_value<min) { min=d_value; k=i; } } return k; } }
package ID; public class Person implements Comparable<Person> { private String name; private String ID; private int age; private String sex; private String birthplace; public String getname() { return name; } public void setname(String name) { this.name = name; } public String getID() { return ID; } public void setID(String ID) { this.ID= ID; } public int getage() { return age; } public void setage(int age) { // int a = Integer.parseInt(age); this.age= age; } public String getsex() { return sex; } public void setsex(String sex) { this.sex= sex; } public String getbirthplace() { return birthplace; } public void setbirthplace(String birthplace) { this.birthplace= birthplace; } public int compareTo(Person o) { return this.name.compareTo(o.getname()); } public String toString() { return name+" "+sex+" "+age+" "+ID+" "+birthplace+" "; } }
閫氳繃瀛︿範鍚堜綔浼欎即鐨勭▼搴忥紝鍙戠幇浠栫殑绋嬪簭鍜屾垜鐨勭▼搴忕浉宸笉澶э紝鏈変簺缁嗗井鐨勪笉鍚屽彲浠ヤ粠浠栫殑浠g爜涓湁鎵€鏀瑰杽銆?/p>
鍚勮嚜杩愯鍚堜綔浼欎即瀹為獙鍗佺紪绋嬬粌涔?锛岀粨鍚堜娇鐢ㄤ綋楠屽鎵€杩愯绋嬪簭鎻愬嚭瀹屽杽寤鸿锛?/p>
package C; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Suanfa counter = new Suanfa(); PrintWriter out = null; try { out = new PrintWriter("test.txt"); } catch (FileNotFoundException e) { System.out.println("鏂囦欢澶硅緭鍑哄け璐?); e.printStackTrace(); } int sum = 0; for (int i = 1; i <= 10; i++) { int a = (int) Math.round(Math.random() * 100); int b = (int) Math.round(Math.random() * 100); int m; Random rand = new Random(); m = (int) rand.nextInt(4) + 1; System.out.println("闅忔満鐢熸垚鐨勫洓鍒欒繍绠楃被鍨嬶細" + m); switch (m) { case 1: a = b + (int) Math.round(Math.random() * 100); while(b == 0){ b = (int) Math.round(Math.random() * 100); } while(a % b != 0){ a = (int) Math.round(Math.random() * 100); } //鑻ョ敓鎴愮殑闄ゆ硶寮忓瓙蹇呴』鑳芥暣闄わ紝涓旀弧瓒冲垎姣嶄负0鐨勬潯浠讹紝鍒檃涓€瀹氳澶т簬b锛屼笖a妯鐨勭粨鏋滆涓?銆?/span> System.out.println(i + ": " + a + "/" + b + "="); int c0 = in.nextInt(); out.println(a + "/" + b + "=" + c0); if (c0 == counter.d(a, b)) { sum += 10; System.out.println("姝g‘!"); } else { System.out.println("閿欒!"); } break; case 2: System.out.println(i + ": " + a + "*" + b + "="); int c = in.nextInt(); out.println(a + "*" + b + "=" + c); if (c == counter.m(a, b)) { sum+= 10; System.out.println("姝g‘!"); } else { System.out.println("閿欒!"); } break; case 3: System.out.println(i + ": " + a + "+" + b + "="); int c1 = in.nextInt(); out.println(a + "+" + b + "=" + c1); if (c1 == counter.add(a, b)) { sum += 10; System.out.println("姝g‘!"); } else { System.out.println("閿欒!"); } break; case 4: while (a < b) { b = (int) Math.round(Math.random() * 100); } //鍥犱负涓嶈兘浜х敓杩愮畻缁撴灉涓鸿礋鏁扮殑鍑忔硶寮忓瓙锛屾墍浠涓€瀹氳澶т簬b銆傝嫢a<b,鍒欓噸鏂扮敓鎴恇銆?/span> System.out.println(i + ": " + a + "-" + b + "="); int c2 = in.nextInt(); out.println(a + "-" + b + "=" + c2); if (c2 == counter.r(a, b)) { sum += 10; System.out.println("姝g‘!"); } else { System.out.println("閿欒!"); } break; } } System.out.println("鎴愮哗" + sum); out.println("鎴愮哗:" + sum); out.close(); } }
package C; public class Suanfa<T> { private T a; private T b; public int add(int a,int b) { return a + b; } public int r(int a, int b) { return a - b; } public int m(int a, int b) { return a * b; } public int d(int a, int b) { if (b != 0 && a%b==0) return a / b; else return 0; } }
閫氳繃瑙傚療鍚堜綔浼欎即鐨勮繖涓▼搴忥紝鍙戠幇濂圭殑姣旀垜鐨勭▼搴忔洿涓虹簿绠€锛屽叾涓湁寰堝鍙互瀛︿範鐨勫湴鏂广€?/p>
l 閲囩敤缁撳缂栫▼鏂瑰紡锛屼笌瀛︿範浼欎即鍚堜綔瀹屾垚瀹為獙涔濈紪绋嬬粌涔?锛?/p>
l
package xinxi; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class xinxi{ private static ArrayList<Student> studentlist; public static void main(String[] args) { studentlist = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("F:\韬唤璇佸彿.txt"); try { FileInputStream fis = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String temp = null; while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String number = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String province =linescanner.nextLine(); Student student = new Student(); student.setName(name); student.setnumber(number); student.setsex(sex); int a = Integer.parseInt(age); student.setage(a); student.setprovince(province); studentlist.add(student); } } catch (FileNotFoundException e) { System.out.println("瀛︾敓淇℃伅鏂囦欢鎵句笉鍒?); e.printStackTrace(); } catch (IOException e) { System.out.println("瀛︾敓淇℃伅鏂囦欢璇诲彇閿欒"); e.printStackTrace(); } boolean isTrue = true; while (isTrue) { System.out.println("杈撳叆搴忓彿杩涜鐩稿簲鐨勬搷浣?); System.out.println("1.瀛楀吀鎺掑簭"); System.out.println("2.杈撳嚭骞撮緞鏈€澶у拰骞撮緞鏈€灏忕殑浜?); System.out.println("3.鏌ユ壘涓庝綘骞撮緞鐩歌繎鐨勪汉"); System.out.println("4.鏌ユ壘浣犵殑鍚屼埂"); String n = scanner.next(); switch (n) { case "1": Collections.sort(studentlist); System.out.println(studentlist.toString()); break; case "2": int max=0,min=100; int j,k1 = 0,k2=0; for(int i=1;i<studentlist.size();i++) { j=studentlist.get(i).getage(); if(j>max) { max=j; k1=i; } if(j<min) { min=j; k2=i; } } System.out.println("骞撮緞鏈€澶э細"+studentlist.get(k1)); System.out.println("骞撮緞鏈€灏忥細"+studentlist.get(k2)); break; case "3": System.out.println("骞撮緞锛?); int yourage = scanner.nextInt(); int near=agenear(yourage); int value=yourage-studentlist.get(near).getage(); System.out.println("鍜屼綘骞撮緞鐩歌繎鐨勬槸"+studentlist.get(near)); break; case "4": System.out.println("杈撳叆浣犵殑瀹朵埂"); String find = scanner.next(); String place=find.substring(0,3); for (int i = 0; i <studentlist.size(); i++) { if(studentlist.get(i).getprovince().substring(1,4).equals(place)) System.out.println("浣犵殑鍚屼埂鏄? "+studentlist.get(i)); } break; } } } public static int agenear(int age) { int j=0,min=53,value=0,k=0; for (int i = 0; i < studentlist.size(); i++) { value=studentlist.get(i).getage()-age; if(value<0) value=-value; if (value<min) { min=value; k=i; } } return k; } }
package xinxi; public class Student implements Comparable<Student> { private String name; private String number ; private String sex ; private int age; private String province; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getnumber() { return number; } public void setnumber(String number) { this.number = number; } public String getsex() { return sex ; } public void setsex(String sex ) { this.sex =sex ; } public int getage() { return age; } public void setage(int age) { this.age= age; } public String getprovince() { return province; } public void setprovince(String province) { this.province=province ; } public int compareTo(Student o) { return this.name.compareTo(o.getName()); } public String toString() { return name+" "+sex+" "+age+" "+number+" "+province+" "; } }
鎴戜滑鍦ㄧ粨瀵圭紪鍐欐绋嬪簭鏃讹紝涓€璧蜂氦娴佹帰绱㈡垜浠悇鑷殑绋嬪簭缂洪櫡鍜屼笉瀹岀編鐨勫湴鏂癸紝鍐嶇粺涓€缁撳悎缂栧啓鍑烘洿鍔犵簿绠€鐨勭▼搴忋€?/p>
閲囩敤缁撳缂栫▼鏂瑰紡锛屼笌瀛︿範浼欎即鍚堜綔瀹屾垚瀹為獙鍗佺紪绋嬬粌涔?銆?/p>
package C; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Suanfa counter = new Suanfa(); PrintWriter out = null; try { out = new PrintWriter("test.txt"); } catch (FileNotFoundException e) { System.out.println("鏂囦欢澶硅緭鍑哄け璐?); e.printStackTrace(); } int sum = 0; for (int i = 1; i <= 10; i++) { int a = (int) Math.round(Math.random() * 100); int b = (int) Math.round(Math.random() * 100); int m; Random rand = new Random(); m = (int) rand.nextInt(4) + 1; System.out.println("闅忔満鐢熸垚鐨勫洓鍒欒繍绠楃被鍨嬶細" + m); switch (m) { case 1: a = b + (int) Math.round(Math.random() * 100); while(b == 0){ b = (int) Math.round(Math.random() * 100); } while(a % b != 0){ a = (int) Math.round(Math.random() * 100); } //鑻ョ敓鎴愮殑闄ゆ硶寮忓瓙蹇呴』鑳芥暣闄わ紝涓旀弧瓒冲垎姣嶄负0鐨勬潯浠讹紝鍒檃涓€瀹氳澶т簬b锛屼笖a妯鐨勭粨鏋滆涓?銆?/span> System.out.println(i + ": " + a + "/" + b + "="); int c0 = in.nextInt(); out.println(a + "/" + b + "=" + c0); if (c0 == counter.d(a, b)) { sum += 10; System.out.println("姝g‘!"); } else { System.out.println("閿欒!"); } break; case 2: System.out.println(i + ": " + a + "*" + b + "="); int c = in.nextInt(); out.println(a + "*" + b + "=" + c); if (c == counter.m(a, b)) { sum+= 10; System.out.println("姝g‘!"); } else { System.out.println("閿欒!"); } break; case 3: System.out.println(i + ": " + a + "+" + b + "="); int c1 = in.nextInt(); out.println(a + "+" + b + "=" + c1); if (c1 == counter.add(a, b)) { sum += 10; System.out.println("姝g‘!"); } else { System.out.println("閿欒!"); } break; case 4: while (a < b) { b = (int) Math.round(Math.random() * 100); } //鍥犱负涓嶈兘浜х敓杩愮畻缁撴灉涓鸿礋鏁扮殑鍑忔硶寮忓瓙锛屾墍浠涓€瀹氳澶т簬b銆傝嫢a<b,鍒欓噸鏂扮敓鎴恇銆?/span> System.out.println(i + ": " + a + "-" + b + "="); int c2 = in.nextInt(); out.println(a + "-" + b + "=" + c2); if (c2 == counter.r(a, b)) { sum += 10; System.out.println("姝g‘!"); } else { System.out.println("閿欒!"); } break; } } System.out.println("鎴愮哗" + sum); out.println("鎴愮哗:" + sum); out.close(); } }
package C; public class Suanfa<T> { private T a; private T b; public int add(int a,int b) { return a + b; } public int r(int a, int b) { return a - b; } public int m(int a, int b) { return a * b; } public int d(int a, int b) { if (b != 0 && a%b==0) return a / b; else return 0; } }
鎴戜滑浜ゆ祦鎬荤粨鍑轰簡鏇翠负绮剧畝鐨勭▼搴忋€?/p>
瀹為獙鎬荤粨
閫氳繃鏈瀹為獙锛屾垜鎺屾彙浜哣etor銆丼tack銆丠ashtable涓変釜绫荤殑鐢ㄩ€斿強甯哥敤API锛屼互鍙?nbsp;浜嗚В浜?span style="font-family: "Times New Roman"">java闆嗗悎妗嗘灦浣撶郴缁勬垚锛涙澶栵紝杩欐槸绗竴娆℃帴瑙︾粨瀵圭紪绋嬶紝浣撲細鍒颁簡涓嶅悓浜庡崟鐙紪绋嬬殑浼樼偣鍜屼箰瓒o紝鍜屽悎浣滀紮浼村郊姝や氦娴侊紝鍊熼壌瀵规柟鐨勪紭鐐癸紝鏀规鑷繁鐨勪笉瓒筹紝浜掔浉浜ゆ祦瀛︿範锛屾湁鐫€寰堝ぇ鐨勬敹鑾枫€?/span>
以上是关于201771010142 寮犵嚂& 鏉ㄨ搲搴? 瀹為獙鍗佷竴 闆嗗悎的主要内容,如果未能解决你的问题,请参考以下文章
201771010142张燕/杨蓉庆实验十三 图形界面事件处理技术
201771010142 张燕 Java的基本程序设计结构第二周学习总结