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>

鎶€鏈垎浜浘鐗? src=

淇敼绋嬪簭閿欒鍚庡涓嬪浘

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? id=
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();
    }
}
CatsAndDogs

 

绀轰緥绋嬪簭2锛?/p>

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? id=
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());
    }
}
Stacks

 

 

绀轰緥绋嬪簭3

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? id=
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);
    }
}
Counter

 

 

娴嬭瘯绋嬪簭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");

   }

 

}

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? id=
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));

}

}

}
ArrayListDemo

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? id=
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");

   }


}
 
LinkedListDemo

 

l 鍦‥lipse鐜涓嬬紪杈戣繍琛岃皟璇曟暀鏉?60椤电▼搴?-1锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? id=
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);
   }
}
LinkedListTest

 

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());

        }

    }

}

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? id=
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());

        }

    }

}
SetDemo

 

l 鍦‥lipse鐜涓嬭皟璇曟暀鏉?65椤电▼搴?-2锛岀粨鍚堣繍琛岀粨鏋滅悊瑙g▼搴忥紱浜嗚ВHashSet绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>

鎶€鏈垎浜浘鐗? src=

 

鎶€鏈垎浜浘鐗? id=
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.");
   }
}
SetTest

 

 

l 鍦‥lipse鐜涓嬭皟璇曟暀鏉?67椤?368绋嬪簭9-3銆?-4锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱浜嗚ВTreeSet绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? id=
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);
   }
}
TreeSetTest
鎶€鏈垎浜浘鐗? id=
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);
   }
}
Item

 

娴嬭瘯绋嬪簭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);

  }

}

鎶€鏈垎浜浘鐗? src=

l

鎶€鏈垎浜浘鐗? id=
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);

  }

}
HashMapDemo

 

 鍦‥lipse鐜涓嬭皟璇曟暀鏉?73椤电▼搴?-6锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱

l 浜嗚ВHashMap銆乀reeMap涓や釜绫荤殑鐢ㄩ€斿強甯哥敤API銆?/p>

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? id=
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));
   }
}
MapTest
鎶€鏈垎浜浘鐗? id=
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 + "]";
   }
}
Employee

 

瀹為獙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>

鎶€鏈垎浜浘鐗? src=

l鎶€鏈垎浜浘鐗? src=

 

鎶€鏈垎浜浘鐗? id=
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;
        }
}
Main
鎶€鏈垎浜浘鐗? id=
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+"
";

}
}
Person

閫氳繃瀛︿範鍚堜綔浼欎即鐨勭▼搴忥紝鍙戠幇浠栫殑绋嬪簭鍜屾垜鐨勭▼搴忕浉宸笉澶э紝鏈変簺缁嗗井鐨勪笉鍚屽彲浠ヤ粠浠栫殑浠g爜涓湁鎵€鏀瑰杽銆?/p>

鍚勮嚜杩愯鍚堜綔浼欎即瀹為獙鍗佺紪绋嬬粌涔?锛岀粨鍚堜娇鐢ㄤ綋楠屽鎵€杩愯绋嬪簭鎻愬嚭瀹屽杽寤鸿锛?/p>

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? src=

 

鎶€鏈垎浜浘鐗? id=
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();
    }
}
Main
鎶€鏈垎浜浘鐗? id=
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;
    }
}
Suanfa

閫氳繃瑙傚療鍚堜綔浼欎即鐨勮繖涓▼搴忥紝鍙戠幇濂圭殑姣旀垜鐨勭▼搴忔洿涓虹簿绠€锛屽叾涓湁寰堝鍙互瀛︿範鐨勫湴鏂广€?/p>

l 閲囩敤缁撳缂栫▼鏂瑰紡锛屼笌瀛︿範浼欎即鍚堜綔瀹屾垚瀹為獙涔濈紪绋嬬粌涔?锛?/p>

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? src=

l

鎶€鏈垎浜浘鐗? id=
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;         
      

}


}
xinxi
鎶€鏈垎浜浘鐗? id=
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+"
";
    }    
}
Student

鎴戜滑鍦ㄧ粨瀵圭紪鍐欐绋嬪簭鏃讹紝涓€璧蜂氦娴佹帰绱㈡垜浠悇鑷殑绋嬪簭缂洪櫡鍜屼笉瀹岀編鐨勫湴鏂癸紝鍐嶇粺涓€缁撳悎缂栧啓鍑烘洿鍔犵簿绠€鐨勭▼搴忋€?/p>

 閲囩敤缁撳缂栫▼鏂瑰紡锛屼笌瀛︿範浼欎即鍚堜綔瀹屾垚瀹為獙鍗佺紪绋嬬粌涔?銆?/p>

 鎶€鏈垎浜浘鐗? src=鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? id=
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();
    }
}
Main
鎶€鏈垎浜浘鐗? id=
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;
    }
}
Suanfa

鎴戜滑浜ゆ祦鎬荤粨鍑轰簡鏇翠负绮剧畝鐨勭▼搴忋€?/p>

 瀹為獙鎬荤粨

閫氳繃鏈瀹為獙锛屾垜鎺屾彙浜哣etor銆丼tack銆丠ashtable涓変釜绫荤殑鐢ㄩ€斿強甯哥敤API锛屼互鍙?nbsp;浜嗚В浜?span style="font-family: "Times New Roman"">java闆嗗悎妗嗘灦浣撶郴缁勬垚锛涙澶栵紝杩欐槸绗竴娆℃帴瑙︾粨瀵圭紪绋嬶紝浣撲細鍒颁簡涓嶅悓浜庡崟鐙紪绋嬬殑浼樼偣鍜屼箰瓒o紝鍜屽悎浣滀紮浼村郊姝や氦娴侊紝鍊熼壌瀵规柟鐨勪紭鐐癸紝鏀规鑷繁鐨勪笉瓒筹紝浜掔浉浜ゆ祦瀛︿範锛屾湁鐫€寰堝ぇ鐨勬敹鑾枫€?/span>

 

以上是关于201771010142 寮犵嚂& 鏉ㄨ搲搴? 瀹為獙鍗佷竴 闆嗗悎的主要内容,如果未能解决你的问题,请参考以下文章

201771010142张燕/杨蓉庆实验十三 图形界面事件处理技术

201771010142 张燕 Java的基本程序设计结构第二周学习总结

搴旂敤鍦ㄥ嵃鍒锋満姊扮殑鍗曞弻寮犵焊寮犳娴嬩紶鎰熷櫒

9263锛佽嵎鍏板寳閮ㄥ彧鍓╂渶鍚?寮犵梾搴婏紝Ajax澶哄啝涓囦汉鐙傛锛佸姵宸ョЩ姘戞棤娉曟帴绉嶇柅鑻楋紵