Chapter 10 - ArrayLists

Chapter 10 - ArrayLists - &KDSWHU $UUD\/LVWV...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: &KDSWHU $UUD\/LVWV &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS $UUD\/LVWV %DVLF $UUD\/LVW 2SHUDWLRQV $UUD\/LVW 6HDUFKLQJ 0HWKRGV 6DPSOH $UUD\/LVW 3UREOHPV 7KH IRU HDFK /RRS :UDSSHU &ODVVHV 7KH &RPSDUDEOH ,QWHUIDFH 1DWXUDO 2UGHULQJ DQG FRPSDUH7R ,PSOHPHQWLQJ &RPSDUDEOH &DVH 6WXG\ 9RFDEXODU\ &RPSDULVRQ 9HUVLRQ &RPSXWH 9RFDEXODU\ 9HUVLRQ &RPSXWH 2YHUODS 9HUVLRQ &RPSOHWH 3URJUDP ,QWURGXFWLRQ ,Q &KDSWHU ZH VDZ KRZ WR GHILQH DUUD\V WKDW FDQ EH XVHG WR VWRUH D VHTXHQFH RI YDOXHV DOO RI WKH VDPH W\SH ,Q WKLV FKDSWHU ZH H[SORUH D QHZ VWUXFWXUH NQRZQ DV DQ $UUD\/LVW WKDW SURYLGHV HYHQ PRUH IXQFWLRQDOLW\ WKDQ DQ DUUD\ 5HPHPEHU WKDW DUUD\V DUH IL[HG VL]H VWUXFWXUHV $UUD\/LVWV KDYH D YDULDEOH OHQJWK ZKLFK DOORZV WKHP WR JURZ DQG VKULQN DV WKH SURJUDP H[HFXWHV 7KH $UUD\/LVW VWUXFWXUH LV WKH ILUVW H[DPSOH RI D JHQHULF VWUXFWXUH VR ZH ZLOO KDYH WR GLVFXVV KRZ JHQHULFV ZRUN LQ -DYD :H ZLOO DOVR VHH KRZ WR XVH SULPLWLYH GDWD ZLWK VXFK D VWUXFWXUH XVLQJ ZKDW DUH NQRZQ DV WKH ZUDSSHU FODVVHV )LQDOO\ ZH ZLOO VHH KRZ WKH &RPSDUDEOH LQWHUIDFH LV XVHG WR GHVFULEH KRZ WR SXW YDOXHV RI D SDUWLFXODU W\SH LQWR VRUWHG RUGHU DQG ZH ZLOO VHH KRZ WR ZULWH FODVVHV WKDW LPSOHPHQW WKH &RPSDUDEOH LQWHUIDFH 533 $UUD\/LVWV $UUD\V DUH D SRZHUIXO DQG LPSRUWDQW SDUW RI WKH -DYD ODQJXDJH EXW WKH\ DUHQ W DOZD\V HDV\ WR PDQLSXODWH )RU H[DPSOH ZKDW LI ZH ZDQW WR VWRUH YDULDEOH OHQJWK GDWD ZLWK D OLVW RI YDOXHV WKDW JURZV DQG VKULQNV DV WKH SURJUDP H[HFXWHV" 7KLV EHKDYLRU LVQ W HDV\ WR LPSOHPHQW ZLWK DQ DUUD\ EHFDXVH DUUD\V KDYH D IL[HG VL]H -DYD RIIHUV DQ DOWHUQDWLYH 7KH -DYD FODVV OLEUDULHV LQFOXGH D VWUXFWXUH NQRZQ DV DQ $UUD\/LVW $V LWV QDPH VXJJHVWV LW LV EDVHG RQ DUUD\ WHFKQRORJ\ (DFK $UUD\/LVW XVHV DQ DUUD\ WR VWRUH LWV YDOXHV $V D UHVXOW WKH $UUD\/LVW KDV WKH VDPH IDVW UDQGRP DFFHVV WKDW DUUD\V SURYLGH ,Q DGGLWLRQ WKH $UUD\/LVW SURYLGHV PDQ\ PHWKRGV WKDW PDNH LW PRUH FRQYHQLHQW WKDQ DQ DUUD\ IRU FHUWDLQ SURJUDPPLQJ WDVNV ,I \RX UHDG WKH $3, GRFXPHQWDWLRQ IRU $UUD\/LVW \RX OO VHH WKDW LW LV DFWXDOO\ OLVWHG DV $UUD\/LVW(! 7KLV LV DQ H[DPSOH RI D JHQHULF FODVV LQ -DYD *HQHULF &ODVV *HQHULF $ FODVV VXFK DV $UUD\/LVW(! WKDW WDNHV D W\SH SDUDPHWHU WR LQGLFDWH ZKDW NLQG RI YDOXHV ZLOO EH XVHG 7KH ( LQ $UUD\/LVW(! LV VKRUW IRU (OHPHQW DQG LW LQGLFDWHV WKH W\SH RI HOHPHQWV ZH ZLOO EH LQFOXGLQJ LQ WKH $UUD\/LVW )RU H[DPSOH ZH PLJKW FRQVWUXFW DQ $UUD\/LVW6WULQJ! LI ZH ZDQW WR PDNH D OLVW RI 6WULQJ REMHFWV RU ZH FDQ PDNH DQ $UUD\/LVW3RLQW! LI ZH ZDQW WR PDNH D OLVW RI 3RLQW REMHFWV RU DQ $UUD\/LVW&RORU! LI ZH ZDQW D OLVW RI &RORU REMHFWV %DVLF $UUD\/LVW 2SHUDWLRQV 7KH V\QWD[ IRU FRQVWUXFWLQJ DQ $UUD\/LVW LV PRUH FRPSOLFDWHG WKDQ ZH KDYH VHHQ ZLWK RWKHU W\SHV EHFDXVH RI WKH W\SH SDUDPHWHU )RU H[DPSOH ZH ZRXOG FRQVWUXFW DQ $UUD\/LVW6WULQJ! DV IROORZV ArrayList<String> list = new ArrayList<String>(); 7KLV FRGH FRQVWUXFWV DQ HPSW\ $UUD\/LVW6WULQJ! 7KLV V\QWD[ LV FRPSOLFDWHG EXW LW ZLOO EH HDVLHU WR UHPHPEHU LI \RX NHHS LQ PLQG WKDW WKH 6WULQJ! QRWDWLRQ LV DFWXDOO\ SDUW RI WKH W\SH 7KLV LVQ W VLPSO\ DQ $UUD\/LVW LW LV DQ $UUD\/LVW6WULQJ! RIWHQ UHDG DV DQ $UUD\/LVW RI 6WULQJ 1RWLFH KRZ WKH W\SH DSSHDUV LQ GHFODULQJ WKH YDULDEOH DQG LQ FDOOLQJ WKH FRQVWUXFWRU ArrayList<String> list = new ArrayList<String>(); ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~ type type ,I \RX WKLQN LQ WHUPV RI WKH W\SH EHLQJ $UUD\/LVW6WULQJ! WKHQ UHDOO\ WKLV OLQH RI FRGH LVQ W DOO WKDW GLIIHUHQW IURP WKH FRGH ZH KDYH EHHQ ZULWLQJ IRU DQ REMHFW OLNH D 3RLQW Point p = new Point(); ~~~~~ ~~~~~ type type 2QFH DQ $UUD\/LVW KDV EHHQ FRQVWUXFWHG ZH FDQ DGG YDOXHV WR LW E\ FDOOLQJ WKH DGG PHWKRG 534 ArrayList<String> list = new ArrayList<String>(); list.add("hello"); list.add("world"); list.add("this is fun"); :KHQ \RX DVN DQ $UUD\/LVW WR DGG D QHZ YDOXH WR WKH OLVW LW DGGV WKH QHZ YDOXH WR WKH HQG RI WKH OLVW ,Q RWKHU ZRUGV WKH VLPSOH YHUVLRQ RI DGG DSSHQGV WKH YDOXH WR WKH HQG RI WKH OLVW 8QOLNH VLPSOH DUUD\V WKH $UUD\/LVW FODVV RYHUULGHV -DYD V WR6WULQJ PHWKRG ZLWK VRPHWKLQJ XVHIXO 7KH $UUD\/LVW YHUVLRQ RI WR6WULQJ FRQVWUXFWV D 6WULQJ WKDW LQFOXGHV WKH FRQWHQWV RI WKH OLVW LQVLGH RI VTXDUH EUDFNHWV DQG VHSDUDWHG E\ FRPPDV 5HPHPEHU WKDW WKH WR6WULQJ PHWKRG LV FDOOHG ZKHQ ZH SULQW DQ REMHFW RU FRQFDWHQDWH WKH REMHFW WR D 6WULQJ $V D UHVXOW $UUD\/LVWV FDQ EH SULQWHG ZLWK D VLPSOH SULQWOQ DV LQ System.out.println("list = " + list); )RU H[DPSOH ZH FDQ DGG SULQWOQ VWDWHPHQWV DV ZH DGG YDOXHV WR WKH OLVW ArrayList<String> list = System.out.println("list list.add("hello"); System.out.println("list list.add("world"); System.out.println("list list.add("this is fun"); System.out.println("list new ArrayList<String>(); = " + list); = " + list); = " + list); = " + list); ,I ZH H[HFXWH WKLV FRGH ZH JHW WKH IROORZLQJ RXWSXW list list list list = = = = [hello] [hello, world] [hello, world, this is fun] 1RWLFH WKDW FDQ SULQW WKH $UUD\/LVW HYHQ ZKHQ LW LV HPSW\ DQG WKDW QHZ YDOXHV DUH DGGHG DW WKH HQG 7KH $UUD\/LVW FODVV DOVR SURYLGHV D PHWKRG IRU DGGLQJ YDOXHV LQ WKH PLGGOH RI WKH OLVW ,W SUHVHUYHV WKH RUGHU RI RWKHU OLVW HOHPHQWV VKLIWLQJ YDOXHV ULJKW WR PDNH URRP IRU WKH QHZ YDOXH 7R LQGLFDWH SRVLWLRQV ZLWKLQ WKH OLVW ZH XVH WKH -DYD FRQYHQWLRQ RI LQGH[LQJ VWDUWLQJ DW IRU WKH ILUVW YDOXH IRU WKH VHFRQG YDOXH DQG VR RQ )RU H[DPSOH JLYHQ WKH OLVW DERYH FRQVLGHU WKH HIIHFW RI LQVHUWLQJ D YDOXH DW LQGH[ list.add(1, "middle value"); System.out.println("now list = " + list); 7KH FDOO RQ DGG LQVWUXFWV WKH FRPSXWHU WR LQVHUW WKH QHZ 6WULQJ DW LQGH[ 7KDW PHDQV WKDW WKH ROG YDOXH DW LQGH[ DQG HYHU\WKLQJ WKDW FRPHV DIWHU LW JHWV VKLIWHG WR WKH ULJKW 6R WKH IROORZLQJ RXWSXW LV SURGXFHG now list = [hello, middle value, world, this is fun] 6R QRZ WKH OLVW KDV D WRWDO RI IRXU YDOXHV 1RWLFH WKDW WKLV QHZ PHWKRG LV DOVR FDOOHG DGG EXW LW WDNHV WZR SDUDPHWHUV DQ LQGH[ DQG D YDOXH WR LQVHUW 7KLV LV DQRWKHU H[DPSOH RI RYHUORDGLQJ ZKLFK ZDV GLVFXVVHG LQ &KDSWHU 535 7KH $UUD\/LVW DOVR KDV D PHWKRG IRU UHPRYLQJ D YDOXH DW D SDUWLFXODU LQGH[ 7KH UHPRYH PHWKRG DOVR SUHVHUYHV WKH RUGHU RI WKH OLVW E\ VKLIWLQJ YDOXHV OHIW WR ILOO LQ DQ\ JDS )RU H[DPSOH FRQVLGHU ZKDW KDSSHQV JLYHQ WKH OLVW DERYH LI ZH UHPRYH WKH YDOXH DW SRVLWLRQ DQG WKHQ UHPRYH WKH YDOXH DW SRVLWLRQ System.out.println("before remove list = " + list); list.remove(0); list.remove(1); System.out.println("after remove list = " + list); 7KLV FRGH SURGXFHV WKH IROORZLQJ RXWSXW before remove list = [hello, middle value, world, this is fun] after remove list = [middle value, this is fun] 7KLV UHVXOW LV D OLWWOH VXUSULVLQJ :H DVNHG WKH OLVW WR UHPRYH WKH YDOXH DW SRVLWLRQ DQG WKHQ WR UHPRYH WKH YDOXH DW SRVLWLRQ <RX PLJKW LPDJLQH WKDW WKLV ZRXOG JHW ULG RI WKH 6WULQJV KHOOR DQG PLGGOH YDOXH VLQFH WKH\ ZHUH DW SRVLWLRQV DQG UHVSHFWLYHO\ EHIRUH WKLV FRGH ZDV H[HFXWHG %XW \RX KDYH WR UHPHPEHU WKDW DQ $UUD\/LVW LV D G\QDPLF VWUXFWXUH ZKHUH YDOXHV DUH PRYLQJ DURXQG DQG VKLIWLQJ LQWR QHZ SRVLWLRQV :H FDQ VHH WKLV PRUH FOHDUO\ LI ZH LQFOXGH D VHFRQG SULQWOQ VWDWHPHQW System.out.println("before remove list = " + list); list.remove(0); System.out.println("after 1st remove list = " + list); list.remove(1); System.out.println("after 2nd remove list = " + list); 7KLV FRGH SURGXFHV WKH IROORZLQJ RXWSXW before remove list = [hello, middle value, world, this is fun] after 1st remove list = [middle value, world, this is fun] after 2nd remove list = [middle value, this is fun] 7KH ILUVW FDOO RQ UHPRYH UHPRYHV WKH 6WULQJ KHOOR EHFDXVH LW V WKH YDOXH FXUUHQWO\ LQ SRVLWLRQ %XW RQFH WKDW YDOXH KDV EHHQ UHPRYHG HYHU\WKLQJ HOVH VKLIWV RYHU 6R WKH 6WULQJ PLGGOH YDOXH PRYHV WR WKH IURQW WR SRVLWLRQ DQG WKH 6WULQJ ZRUOG VKLIWV LQWR SRVLWLRQ 6R ZKHQ WKH VHFRQG FDOO RQ UHPRYH LV SHUIRUPHG -DYD UHPRYHV ZRUOG IURP WKH OLVW EHFDXVH LW LV WKH YDOXH WKDW LV LQ SRVLWLRQ DW WKDW SRLQW LQ WLPH ,I \RX ZDQW WR ILQG RXW KRZ PDQ\ HOHPHQWV DUH LQ DQ $UUD\/LVW \RX FDQ FDOO LWV VL]H PHWKRG DQG LI \RX ZDQW WR REWDLQ DQ LQGLYLGXDO LWHP IURP WKH OLVW \RX FDQ FDOO LWV JHW PHWKRG SDVVLQJ LW D VSHFLILF LQGH[ )RU H[DPSOH WKH ORRS EHORZ ZRXOG DGG XS WKH OHQJWKV RI WKH 6WULQJV LQ DQ $UUD\/LVW6WULQJ! int sum = 0; for (int i = 0; i < list.size(); i++) { String s = list.get(i); sum += s.length(); } System.out.println("Total of lengths = " + sum); 536 7KLV ORRS ORRNV VLPLODU WR WKH NLQG RI ORRS ZH ZRXOG XVH WR DFFHVV WKH YDULRXV HOHPHQWV RI DQ DUUD\ %XW LQVWHDG RI DVNLQJ IRU OLVW OHQJWK DV ZH ZRXOG IRU DQ DUUD\ ZH DVN IRU OLVW VL]H DQG LQVWHDG RI DVNLQJ IRU OLVW>L@ DV ZH ZRXOG ZLWK DQ DUUD\ ZH DVN IRU OLVW JHW L &DOOLQJ DGG DQG UHPRYH FDQ EH H[SHQVLYH EHFDXVH RI WKH VKLIWLQJ RI YDOXHV $V D UHVXOW WKHUH LV D PHWKRG FDOOHG VHW WKDW WDNHV DQ LQGH[ DQG D YDOXH DQG WKDW UHSODFHV WKH YDOXH DW WKH JLYHQ LQGH[ ZLWK WKH JLYHQ YDOXH ZLWKRXW GRLQJ DQ\ VKLIWLQJ )RU H[DPSOH ZH FDQ UHSODFH WKH YDOXH DW WKH IURQW RI WKH OLVW E\ VD\LQJ list.set(0, "new front"); $V QRWHG DERYH ZKHQ \RX FRQVWUXFW DQ $UUD\/LVW LW ZLOO LQLWLDOO\ EH HPSW\ $IWHU \RX KDYH DGGHG YDOXHV WR D OLVW \RX FDQ UHPRYH WKHP RQH DW D WLPH %XW VRPHWLPHV \RX ZDQW WR UHPRYH DOO RI WKHP ,Q WKDW FDVH \RX FDQ FDOO WKH FOHDU PHWKRG RI WKH $UUD\/LVW WR KDYH LW UHPRYH DOO RI WKH LWHPV IURP WKH OLVW 7KH $UUD\/LVW FODVV LV SDUW RI WKH MDYD XWLO SDFNDJH VR WR LQFOXGH LW LQ D SURJUDP \RX ZRXOG KDYH WR LQFOXGH DQ LPSRUW GHFODUDWLRQ %HORZ LV D WDEOH VXPPDUL]LQJ WKH $UUD\/LVW RSHUDWLRQV ZH KDYH VHHQ LQ WKLV VHFWLRQ 7KH\ DUH GHILQHG LQ WHUPV RI WKH JHQHULF W\SH ( $ PRUH FRPSOHWH OLVW FDQ EH IRXQG LQ WKH RQOLQH -DYD GRFXPHQWDWLRQ %DVLF $UUD\/LVW 0HWKRGV 0HWKRG add(E value) 'HVFULSWLRQ $UUD\/LVW6WULQJ! ([DPSOH list.add("end"); list.add(1, "middle"); list.clear(); list.get(1) list.remove(1); list.set(2, "hello"); list.size() DGGV YDOXH DW WKH HQG RI WKH OLVW add(int index, E value) DGGV YDOXH DW WKH JLYHQ LQGH[ VKLIWLQJ VXEVHTXHQW YDOXHV ULJKW clear() PDNHV WKH OLVW HPSW\ get(int index) JHWV WKH YDOXH DW WKH JLYHQ LQGH[ UHWXUQ W\SH LV ( remove(int index) UHPRYHV WKH YDOXH DW WKH JLYHQ LQGH[ VKLIWLQJ VXEVHTXHQW YDOXHV OHIW set(int index, E value) UHSODFHV WKH YDOXH DW JLYHQ LQGH[ ZLWK JLYHQ YDOXH size() UHWXUQV WKH FXUUHQW QXPEHU RI HOHPHQWV LQ WKH OLVW $UUD\/LVW 6HDUFKLQJ 0HWKRGV 2QFH \RX KDYH EXLOW XS DQ $UUD\/LVW \RX PLJKW EH LQWHUHVWHG LQ VHDUFKLQJ IRU D VSHFLILF YDOXH LQ WKH OLVW 7KH $UUD\/LVW FODVV SURYLGHV VHYHUDO PHFKDQLVPV IRU GRLQJ VR ,I \RX MXVW ZDQW WR NQRZ ZKHWKHU RU QRW VRPHWKLQJ LV LQ WKH OLVW \RX FDQ FDOO WKH PHWKRG FDOOHG FRQWDLQV WKDW UHWXUQV D ERROHDQ YDOXH )RU H[DPSOH VXSSRVH WKDW \RX KDYH DQ LQSXW ILOH RI QDPHV WKDW KDV VRPH GXSOLFDWHV DQG \RX ZDQW WR JHW ULG RI WKH GXSOLFDWHV )RU H[DPSOH WKH ILOH PLJKW VWRUH 537 Maria Derek Erica Livia Jack Anita Kendall Maria Livia Derek Jamie Jack Erica <RX FDQ FRQVWUXFW DQ $UUD\/LVW6WULQJ! WR VROYH WKLV SUREOHP E\ XVLQJ WKH FRQWDLQV PHWKRG WR DYRLG DGGLQJ DQ\ GXSOLFDWHV Scanner input = new Scanner(new File("names.txt")); ArrayList<String> list = new ArrayList<String>(); while (input.hasNext()) { String name = input.hasNext(); if (!list.contains(name)) { list.add(name); } } System.out.println("list = " + list); *LYHQ WKH LQSXW ILOH DERYH WKLV FRGH SURGXFHV WKH IROORZLQJ RXWSXW list = [Maria, Derek, Erica, Livia, Jack, Anita, Kendall, Jamie] 1RWLFH WKDW RQO\ HLJKW RI WKH RULJLQDO WKLUWHHQ QDPHV DSSHDU LQ WKLV OLVW EHFDXVH WKH YDULRXV GXSOLFDWHV KDYH EHHQ HOLPLQDWHG 6RPHWLPHV LW LV QRW HQRXJK WR NQRZ WKDW D YDOXH DSSHDUV LQ WKH OLVW 6RPHWLPHV \RX ZDQW WR NQRZ H[DFWO\ ZKHUH LW RFFXUV )RU H[DPSOH VXSSRVH \RX ZDQW WR ZULWH D PHWKRG WR UHSODFH DOO RFFXUUHQFHV RI RQH ZRUG ZLWK DQRWKHU ZRUG LQ DQ $UUD\/LVW6WULQJ! <RX FDQ FDOO WKH VHW PHWKRG WR UHSODFH HDFK LQGLYLGXDO RFFXUUHQFH EXW \RX KDYH WR NQRZ ZKHUH WKH\ DUH LQ WKH OLVW <RX FDQ ILQG RXW WKH ORFDWLRQ RI D YDOXH LQ WKH OLVW E\ FDOOLQJ WKH LQGH[2I PHWKRG 7KH LQGH[2I PHWKRG WDNHV D YDOXH WR VHDUFK IRU ,W UHWXUQV WKH LQGH[ RI WKH ILUVW RFFXUUHQFH RI WKH YDOXH LQ WKH OLVW ,I LW GRHVQ W ILQG WKH YDOXH LW UHWXUQV 6R ZH FRXOG ZULWH WKH UHSODFH PHWKRG DV IROORZV public static void replace(String target, String replacement, ArrayList<String> list) { int index = list.indexOf(target); while (index >= 0) { list.set(index, replacement); index = list.indexOf(target); } } 1RWLFH WKDW WKH UHWXUQ W\SH RI WKLV PHWKRG LV YRLG HYHQ WKRXJK LW FKDQJHV WKH FRQWHQWV RI DQ $UUD\/LVW REMHFW 6RPH QRYLFHV WKLQN WKDW \RX KDYH WR UHWXUQ WKH FKDQJHG $UUD\/LVW EXW WKH PHWKRG GRHVQ W FUHDWH D QHZ $UUD\/LVW LW PHUHO\ FKDQJHV WKH FRQWHQWV RI WKH OLVW $V ZH KDYH VHHQ ZLWK DUUD\V DQG RWKHU REMHFWV D SDUDPHWHU LV DOO ZH QHHG WR EH DEOH WR FKDQJH WKH FXUUHQW VWDWH RI DQ REMHFW :H FDQ WHVW WKH PHWKRG ZLWK WKH IROORZLQJ FRGH 538 ArrayList<String> list = new ArrayList<String>(); list.add("to"); list.add("be"); list.add("or"); list.add("not"); list.add("to"); list.add("be"); System.out.println("initial list = " + list); replace("be", "beep", list); System.out.println("final list = " + list); 7KLV FRGH SURGXFHV WKH IROORZLQJ RXWSXW initial list = [to, be, or, not, to, be] final list = [to, beep, or, not, to, beep] 7KHUH LV DOVR D YDULDWLRQ RI LQGH[2I NQRZQ DV ODVW,QGH[2I $V LWV QDPH LPSOLHV LW UHWXUQV WKH LQGH[ RI WKH ODVW RFFXUUHQFH RI WKH YDOXH 7KLV PLJKW EH XVHIXO IRU DOJRULWKPV WKDW \RX ZDQW WR UXQ EDFNZDUGV 7KH IROORZLQJ WDEOH VXPPDUL]HV WKH $UUD\/LVW VHDUFKLQJ PHWKRGV $UUD\/LVW 6HDUFKLQJ 0HWKRGV 0HWKRG contains(Object value) 'HVFULSWLRQ $UUD\/LVW6WULQJ! ([DPSOH UHWXUQV WUXH LI WKH JLYHQ YDOXH DSSHDUV LQ WKH list.contains("hello") OLVW UHWXUQV IDOVH RWKHUZLVH indexOf(E value) list.indexOf("world") UHWXUQV WKH LQGH[ RI WKH ILUVW RFFXUUHQFH RI WKH JLYHQ YDOXH LQ WKH OLVW LI QRW IRXQG lastIndexOf(E value) UHWXUQV WKH LQGH[ RI WKH ODVW RFFXUUHQFH RI WKH list.lastIndexOf("hello") JLYHQ YDOXH LQ WKH OLVW LI QRW IRXQG 6DPSOH $UUD\/LVW 3UREOHPV /HW V VROYH WZR $UUD\/LVW SUREOHPV WR H[SORUH VRPH RI WKH LVVXHV WKDW FRPH XS ZKHQ XVLQJ $UUD\/LVW REMHFWV &RQVLGHU WKH IROORZLQJ FRGH WKDW FUHDWHV DQ $UUD\/LVW DQG VWRUHV VHYHUDO ZRUGV LQ LW ArrayList<String> words = new ArrayList<String>(); words.add("four"); words.add("score"); words.add("and"); words.add("seven"); words.add("years"); words.add("ago"); System.out.println("words = " + words); 7KLV FRGH SURGXFHV WKH IROORZLQJ RXWSXW words = [four, score, and, seven, years, ago] 539 6XSSRVH WKDW ZH ZDQW WR LQVHUW D 6WULQJ FRQWDLQLQJ D VLQJOH DVWHULVN LQ IURQW RI HDFK ZRUG :H H[SHFW WR GRXEOH WKH OHQJWK RI WKH $UUD\/LVW EHFDXVH HDFK RI WKHVH ZRUGV ZLOO KDYH DQ DVWHULVN LQ IURQW RI LW LI RXU FRGH ZRUNV SURSHUO\ +HUH LV D ILUVW DWWHPSW WKDW PDNHV VHQVH LQWXLWLYHO\ for (int i = 0; i < words.size(); i++) { words.add(i, "*"); } System.out.println("after loop words = " + words); 6R OHW V JHW EDFN WR WKH IRU ORRS ,W KDV WKH XVXDO DUUD\ VW\OH IRU ORRS ZLWK DQ LQGH[ YDULDEOH L WKDW VWDUWV DW DQG JRHV XS E\ RQH HDFK WLPH ,Q WKLV FDVH LW LV LQVHUWLQJ DQ DVWHULVN DW SRVLWLRQ L HDFK WLPH WKURXJK WKH ORRS 7KH SUREOHP LV WKDW WKH ORRS QHYHU WHUPLQDWHV ,I \RX UH SDWLHQW HQRXJK \RX ZLOO ILQG WKDW WKH SURJUDP GRHV HYHQWXDOO\ WHUPLQDWH ZLWK DQ RXW RI PHPRU\ HUURU 7KH SUREOHP LV WKH IDFW WKDW WKH $UUD\/LVW LV D G\QDPLF VWUXFWXUH ZKHUH WKLQJV PRYH DURXQG WR QHZ SRVLWLRQV /HW V WKLQN DERXW WKLV FDUHIXOO\ WR VHH ZKDW LV JRLQJ RQ ,QLWLDOO\ ZH KDYH WKLV OLVW ZLWK WKH 6WULQJ IRXU LQ SRVLWLRQ [four, score, and, seven, years, ago] 7KH ILUVW WLPH WKURXJK WKH ORRS ZH LQVHUW DQ DVWHULVN DW SRVLWLRQ WR WKH ULJKW VR WKDW LW LV QRZ LQ SRVLWLRQ [*, four, score, and, seven, years, ago] 7KLV VKLIWV WKH 6WULQJ IRXU RQH 7KHQ ZH FRPH DURXQG WKH IRU ORRS DQG LQFUHPHQW L WR EH $QG ZH LQVHUW DQ DVWHULVN DW SRVLWLRQ %XW QRWLFH WKDW WKH ZRUG IRXU LV FXUUHQWO\ DW SRVLWLRQ ZKLFK PHDQV WKDW WKLV VHFRQG DVWHULVN DOVR JRHV LQ IURQW RI WKH ZRUG IRXU VKLIWLQJ LW LQWR SRVLWLRQ [*, *, four, score, and, seven, years, ago] :H JR DURXQG WKH ORRS DJDLQ LQFUHPHQWLQJ L WR EH DQG RQFH DJDLQ LQVHUWLQJ DQ DVWHULVN DW WKDW SRVLWLRQ ZKLFK LV RQFH DJDLQ LQ IURQW RI WKH ZRUG IRXU [*, *, *, four, score, and, seven, years, ago] 7KLV FRQWLQXHV LQGHILQLWHO\ EHFDXVH ZH NHHS LQVHUWLQJ DVWHULVNV LQ IURQW RI WKH ILUVW ZRUG LQ WKH OLVW 7KH IRU ORRS WHVW FRPSDUHV L WR WKH VL]H RI WKH OLVW EXW EHFDXVH WKH OLVW LV JURZLQJ WKH VL]H NHHSV JRLQJ XS 6R WKLV SURFHVV FRQWLQXHV XQWLO ZH H[KDXVW DOO DYDLODEOH PHPRU\ 7R IL[ WKLV ORRS ZH KDYH WR UHDOL]H WKDW LQVHUWLQJ DQ DVWHULVN DW SRVLWLRQ L LV JRLQJ WR VKLIW HYHU\WKLQJ RQH WR WKH ULJKW 6R RQ WKH QH[W LWHUDWLRQ RI WKH ORRS ZH ZLOO ZDQW WR GHDO ZLWK WKH SRVLWLRQ WZR WR WKH ULJKW QRW WKH SRVLWLRQ RQH WR WKH ULJKW 6R ZH FDQ IL[ WKH ORRS VLPSO\ E\ FKDQJLQJ WKH XSGDWH SDUW RI WKH IRU ORRS WR DGG WR L LQVWHDG RI DGGLQJ WR L for (int i = 0; i < words.size(); i += 2) { words.add(i, "*"); } System.out.println("after loop words = " + words); :KHQ ZH H[HFXWH WKLV YHUVLRQ RI WKH FRGH ZH JHW WKH IROORZLQJ RXWSXW 540 after loop words = [*, four, *, score, *, and, *, seven, *, years, *, ago] $V DQRWKHU H[DPSOH OHW V FRQVLGHU ZKDW FRGH ZH ZRXOG QHHG WR ZULWH WR XQGR WKLV RSHUDWLRQ ,Q SDUWLFXODU ZH ZDQW WR ZULWH FRGH WKDW ZLOO UHPRYH HYHU\ RWKHU YDOXH IURP WKH OLVW VWDUWLQJ ZLWK WKH ILUVW YDOXH 6R ZH ZDQW WR UHPRYH WKH YDOXHV WKDW DUH FXUUHQWO\ DW LQGH[HV DQG 7KDW PLJKW OHDG XV WR ZULWH FRGH OLNH WKH IROORZLQJ for (int i = 0; i < words.size(); i += 2) { words.remove(i); } System.out.println("after second loop words = " + words); /RRNLQJ DW WKH ORRS \RX FDQ VHH WKDW L VWDUWV DW DQG JRHV XS E\ HDFK WLPH ZKLFK PHDQV LW SURGXFHV D VHTXHQFH RI HYHQ YDOXHV DQG VR RQ 7KDW ZRXOG VHHP WR EH ULJKW JLYHQ WKDW WKH YDOXHV WR EH UHPRYHG DUH DW WKRVH LQGH[HV %XW WKLV FRGH GRHVQ W ZRUN ,W SURGXFHV WKH IROORZLQJ RXWSXW after second loop words = [four, *, *, and, seven, *, *, ago] $JDLQ RXU SUREOHPV FRPHV IURP WKH IDFW WKDW WKH $UUD\/LVW LV D G\QDPLF VWUXFWXUH ZKHUH YDOXHV DUH VKLIWHG IURP RQH ORFDWLRQ WR DQRWKHU 7KH ILUVW DVWHULVN ZH ZDQW WR UHPRYH LV DW LQGH[ [*, four, *, score, *, and, *, seven, *, years, *, ago] %XW RQFH ZH UHPRYH WKH DVWHULVN DW SRVLWLRQ WKH VHFRQG DVWHULVN VKLIWV LQWR LQGH[ [four, *, score, *, and, *, seven, *, years, *, ago] 6R WKH VHFRQG UHPRYH VKRXOG EH DW LQGH[ WKH WKLUG DVWHULVN ZLOO EH LQ LQGH[ QRW LQGH[ $QG RQFH ZH SHUIRUP WKDW VHFRQG UHPRYH [four, score, *, and, *, seven, *, years, *, ago] 6R LQ WKLV FDVH ZH GRQ W ZDQW WR LQFUHPHQW L E\ WKDW LQFUHPHQWV E\ LV WKH ULJKW FKRLFH HDFK WLPH WKURXJK WKH ORRS +HUH WKH VLPSOH ORRS for (int i = 0; i < words.size(); i++) { words.remove(i); } System.out.println("after second loop words = " + words); $IWHU H[HFXWLQJ WKLV FRGH ZH REWDLQ WKH IROORZLQJ RXWSXW after second loop words = [four, score, and, seven, years, ago] 3XWWLQJ DOO RI WKHVH SLHFHV WRJHWKHU ZH REWDLQ WKH IROORZLQJ FRPSOHWH SURJUDP 541 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Builds up a list of words, adds stars to it and removes them. import java.util.*; public class StarFun { public static void main(String args) { // construct and fill up ArrayList ArrayList<String> words = new ArrayList<String>(); words.add("four"); words.add("score"); words.add("and"); words.add("seven"); words.add("years"); words.add("ago"); System.out.println("words = " + words); // insert one star in front of each word for (int i = 0; i < words.size(); i += 2) { words.add(i, "*"); } System.out.println("after loop words = " + words); // remove stars for (int i = 0; i < words.size(); i++) { words.remove(i); } System.out.println("after second loop words = " + words); } } ,I ZH ZDQW WR ZULWH WKH ORRSV LQ D PRUH LQWXLWLYH PDQQHU ZH FDQ UXQ WKHP EDFNZDUGV ,Q RWKHU ZRUGV WKH ORRSV ZH KDYH ZULWWHQ JR IURP OHIW WR ULJKW IURP WKH EHJLQQLQJ RI WKH OLVW WR WKH HQG RI WKH OLVW :H FRXOG LQVWHDG JR ULJKW WR OHIW IURP WKH HQG RI WKH OLVW WR WKH EHJLQQLQJ RI WKH OLVW %\ JRLQJ EDFNZDUGV ZH HQVXUH WKDW DQ\ FKDQJHV ZH DUH PDNLQJ RFFXU LQ SDUWV RI WKH OLVW WKDW ZH KDYH DOUHDG\ YLVLWHG )RU H[DPSOH ZH IRXQG WKDW WKLV ORRS GLG QRW ZRUN SURSHUO\ HYHQ WKRXJK LW VHHPHG OLNH WKH LQWXLWLYH DSSURDFK // doesn't work properly for (int i = 0; i < words.size(); i++) { words.add(i, "*"); } %XW LI ZH WXUQ WKLV ORRS DURXQG DQG KDYH LW JR EDFNZDUGV UDWKHU WKDQ JRLQJ IRUZDUGV WKHQ LW GRHV ZRUN SURSHUO\ // works properly because loop goes backwards for (int i = words.size() - 1; i >= 0; i--) { words.add(i, "*"); } 7KH SUREOHP ZH KDG ZLWK WKH RULJLQDO FRGH ZDV WKDW ZH ZHUH LQVHUWLQJ D YDOXH LQWR WKH OLVW DQG WKHQ PRYLQJ RXU LQGH[ YDULDEOH RQWR WKDW VSRW LQ WKH OLVW %\ JRLQJ EDFNZDUGV WKH FKDQJHV WKDW ZH PDNH KDSSHQ LQ SDUWV RI WKH OLVW WKDW ZH KDYH DOUHDG\ SURFHVVHG 542 6LPLODUO\ ZH ZDQWHG WR ZULWH WKH VHFRQG ORRS WKLV ZD\ // doesn't work properly for (int i = 0; i < words.size(); i += 2) { words.remove(i); } $JDLQ WKH SUREOHP LV WKDW ZH DUH FKDQJLQJ D SDUW RI WKH OLVW WKDW ZH DUH DERXW WR SURFHVV :H FDQ NHHS WKH RYHUDOO VWUXFWXUH E\ UXQQLQJ WKH ORRS EDFNZDUGV // works properly because loop goes backwards for (int i = words.size() - 1; i >= 0; i -= 2) { words.remove(i); } 7KH IRU HDFK /RRS :H VDZ LQ &KDSWHU WKDW ZH FDQ XVH WKH IRU HDFK ORRS WR LWHUDWH RYHU WKH HOHPHQWV RI DQ DUUD\ :H FDQ GR WKH VDPH ZLWK WKH $UUD\/LVW )RU H[DPSOH ZH VDZ WKDW WKH IROORZLQJ FRGH FRXOG EH XVHG WR DGG XS WKH OHQJWKV RI WKH 6WULQJV VWRUHG LQ DQ $UUD\/LVW6WULQJ! FDOOHG OLVW int sum = 0; for (int i = 0; i < list.size(); i++) { String s = list.get(i); sum += s.length(); } System.out.println("Total of lengths = " + sum); :H FDQ VLPSOLI\ WKLV FRGH ZLWK WKH IRU HDFK ORRS 5HPHPEHU WKDW LWV V\QWD[ LV DV IROORZV for (<type> <name> : <structure>) { <statement>; <statement>; ... <statement>; } 7KH ORRS DERYH FDQ EH UHZULWWHQ DV IROORZV int sum = 0; for (String s : list) { sum += s.length(); } System.out.println("Total of lengths = " + sum); 5HPHPEHU WKDW \RX FDQ WKLQN RI WKLV ORRS DV VD\LQJ )RU HDFK 6WULQJ V FRQWDLQHG LQ OLVW %HFDXVH WKH IRU HDFK ORRS KDV VXFK D VLPSOH V\QWD[ \RX VKRXOG XVH LW ZKHQHYHU \RX ILQG \RXUVHOI ZDQWLQJ WR VHTXHQWLDOO\ SURFHVV HDFK YDOXH VWRUHG LQ D OLVW 543 <RX ZLOO ILQG KRZHYHU WKDW WKH IRU HDFK ORRS LV QRW DSSURSULDWH IRU PRUH FRPSOH[ OLVW SUREOHPV )RU H[DPSOH WKHUH LV QR VLPSOH ZD\ WR VNLS DURXQG LQ WKH OLVW XVLQJ WKH IRU HDFK ORRS <RX PXVW SURFHVV WKH YDOXHV LQ VHTXHQFH IURP ILUVW WR ODVW <RX DOVR DUH QRW DOORZHG WR PRGLI\ WKH OLVW ZKLOH \RX DUH LWHUDWLQJ RYHU LW &RQVLGHU IRU H[DPSOH WKH IROORZLQJ VDPSOH FRGH for (String s : words) { System.out.println(s); words.remove(0); } 7KLV FRGH SULQWV HDFK 6WULQJ IURP WKH OLVW DQG WKHQ DWWHPSWV WR UHPRYH WKH YDOXH DW WKH IURQW RI WKH OLVW :KHQ \RX H[HFXWH WKLV FRGH WKH SURJUDP KDOWV ZLWK D &RQFXUUHQW0RGLILFDWLRQ([FHSWLRQ -DYD LV OHWWLQJ \RX NQRZ WKDW \RX DUH QRW DOORZHG WR LWHUDWH RYHU WKH OLVW DQG WR PRGLI\ WKH OLVW DW WKH VDPH WLPH FRQFXUUHQWO\ %HFDXVH RI WKLV OLPLWDWLRQ QHLWKHU RI WKH SUREOHPV IURP WKH SUHYLRXV VHFWLRQ FRXOG EH VROYHG XVLQJ WKH IRU HDFK ORRS :UDSSHU &ODVVHV 6R IDU DOO RI WKH $UUD\/LVW H[DPSOHV ZH KDYH ORRNHG DW KDYH LQYROYHG DQ $UUD\/LVW RI 6WULQJ REMHFWV :KDW LI ZH ZDQWHG WR IRUP D OLVW RI LQWHJHUV" *LYHQ WKDW WKH $UUD\/LVW(! LV D JHQHULF FODVV \RX G WKLQN WKDW -DYD ZRXOG DOORZ \RX WR GHILQH DQ $UUD\/LVWLQW! EXW WKDW LV QRW WKH FDVH 7KH ( LQ $UUD\/LVW(! FDQ EH ILOOHG LQ ZLWK DQ\ REMHFW RU UHIHUHQFH W\SH L H WKH QDPH RI D FODVV 7KH SULPLWLYH W\SHV OLNH LQW GRXEOH FKDU DQG ERROHDQ FDQQRW EH XVHG DV WKH W\SH SDUDPHWHU IRU DQ $UUD\/LVW ,QVWHDG -DYD GHILQHV D VHULHV RI ZUDSSHU FODVVHV WKDW DOORZ \RX WR VWRUH SULPLWLYH GDWD DV REMHFWV :UDSSHU &ODVV $ FODVV WKDW ZUDSV SULPLWLYH GDWD DV DQ REMHFW 7R XQGHUVWDQG WKH UROH RI D ZUDSSHU FODVV WKLQN DERXW ZK\ ZH VR RIWHQ SXW FDQG\ LQ D ZUDSSHU 3LHFHV RI FDQG\ FDQ EH VWLFN\ DQG LQFRQYHQLHQW WR KDQGOH GLUHFWO\ VR ZH SXW WKHP LQVLGH D ZUDSSHU WKDW PDNHV WKHP PRUH FRQYHQLHQW WR KDQGOH :KHQ ZH ZDQW WKH DFWXDO FDQG\ ZH RSHQ XS WKH ZUDSSHU WR JHW WKH FDQG\ RXW 7KH -DYD ZUDSSHU FODVVHV ILOO D VLPLODU UROH &RQVLGHU IRU H[DPSOH VLPSOH LQWHJHUV :H KDYH VHHQ WKDW WKH\ DUH RI W\SH LQW ZKLFK LV D SULPLWLYH W\SH 3ULPLWLYH W\SHV DUH QRW REMHFWV ZKLFK PHDQV WKDW ZH FDQ W XVH YDOXHV RI W\SH LQW LQ DQ REMHFW FRQWH[W 6R ZH ZUDS XS HDFK LQW LQWR DQ REMHFW RI W\SH ,QWHJHU ,QWHJHU REMHFWV DUH YHU\ VLPSOH 7KH\ KDYH MXVW RQH GDWD ILHOG DQ LQW YDOXH :KHQ ZH FRQVWUXFW DQ ,QWHJHU ZH SDVV DQ LQW YDOXH WR EH ZUDSSHG $QG ZKHQ ZH ZDQW WR JHW WKH LQW EDFN ZH FDOO D PHWKRG LQW9DOXH WKDW UHWXUQV WKH LQW 7R XQGHUVWDQG WKH GLVWLQFWLRQ FRQVLGHU WKH IROORZLQJ YDULDEOH GHFODUDWLRQV int x = 38; Integer y = new Integer(38); 7KLV FRGH OHDGV WR WKH IROORZLQJ VLWXDWLRQ LQ PHPRU\ 544 +----+ x | 38 | +----+ +--------------+ +---+ | +----+ | y | +-+---> | value | 38 | | +---+ | +----+ | +--------------+ :H KDYH VHHQ WKDW SULPLWLYH GDWD LV VWRUHG GLUHFWO\ WKH YDULDEOH [ VWRUHV WKH DFWXDO YDOXH REMHFWV DUH VWRUHG DV UHIHUHQFHV WKH YDULDEOH \ VWRUHV D UHIHUHQFH WR DQ REMHFW WKDW FRQWDLQV ,I ZH ODWHU ZDQW WR JHW WKH WKH PHWKRG LQW9DOXH int number = y.intValue(); ZKLOH RXW RI WKH REMHFW WR XQZUDS LW DQG UHPRYH WKH FDQG\ LQVLGH ZH FDOO 7KH ZUDSSHU FODVVHV DUH RI SDUWLFXODU LQWHUHVW IRU WKLV FKDSWHU EHFDXVH LI \RX ZDQW WR KDYH DQ $UUD\/LVW(! WKHQ WKH ( QHHGV WR EH D UHIHUHQFH W\SH 6R ZH FDQ W IRUP DQ $UUD\/LVWLQW! EXW ZH FDQ IRUP DQ $UUD\/LVW,QWHJHU! )RU H[DPSOH ZH FDQ ZULWH WKH IROORZLQJ FRGH WKDW DGGV VHYHUDO LQWHJHU YDOXHV WR D OLVW DQG DGGV WKHP XS ArrayList<Integer> list = new ArrayList<Integer>(); list.add(new Integer(13)); list.add(new Integer(47)); list.add(new Integer(15)); list.add(new Integer(9)); int sum = 0; for (Integer n : list) { sum += n.intValue(); } System.out.println("list = " + list); System.out.println("sum = " + sum); 7KLV FRGH SURGXFHV WKH IROORZLQJ RXWSXW list = [13, 47, 15, 9] sum = 84 -DYD SURYLGHV D PHFKDQLVP IRU VLPSOLI\LQJ FRGH WKDW LQYROYHV WKH XVH RI ZUDSSHU FODVVHV ,Q SDUWLFXODU -DYD ZLOO FRQYHUW EHWZHHQ ,QWHJHU YDOXHV DQG LQW YDOXHV ZKHQ \RXU LQWHQW VHHPV FOHDU )RU H[DPSOH JLYHQ WKH GHFODUDWLRQ RI WKH YDULDEOH OLVW DV DQ $UUD\/LVW,QWHJHU! ZH FDQ DGG D VHULHV RI ,QWHJHU REMHFWV WR WKH OLVW EXW ZH FDQ DOVR VLPSO\ DGG LQW YDOXHV DV LQ list.add(13); ,Q WKLV OLQH RI FRGH -DYD VHHV WKDW \RX DUH DGGLQJ DQ LQW WR D VWUXFWXUH WKDW LV H[SHFWLQJ DQ ,QWHJHU %XW -DYD DOVR NQRZV WKH UHODWLRQVKLS EHWZHHQ LQW DQG ,QWHJHU WKDW HDFK ,QWHJHU LV VLPSO\ DQ LQW ZUDSSHG XS DV DQ REMHFW 6R LQ WKLV FDVH -DYD ZLOO DXWRPDWLFDOO\ FRQYHUW WKH LQW YDOXH LQWR D FRUUHVSRQGLQJ ,QWHJHU REMHFW 7KLV SURFHVV LV NQRZQ DV ER[LQJ %R[LQJ $Q DXWRPDWLF FRQYHUVLRQ IURP SULPLWLYH GDWD WR D ZUDSSHG REMHFW RI DSSURSULDWH W\SH H J DQ LQW ER[HG WR IRUP DQ ,QWHJHU 545 6LPLODUO\ ZH GRQ W KDYH WR FDOO WKH LQW9DOXH PHWKRG WR XQZUDS WKH REMHFW 7KH ERG\ RI WKH IRU ORRS FDQ EH UHZULWWHQ DV IROORZV sum += n; ,Q WKLV FDVH ZH KDYH D YDULDEOH RI W\SH LQW RQ WKH OHIW KDQG VLGH DQG D YDULDEOH RI W\SH ,QWHJHU RQ WKH ULJKW KDQG VLGH 1RUPDOO\ WKHVH YDULDEOHV ZRXOG EH LQFRPSDWLEOH H[FHSW IRU WKH IDFW WKDW -DYD NQRZV DERXW WKH UHODWLRQVKLS EHWZHHQ LQW DQG ,QWHJHU 6R LQ WKLV FDVH -DYD ZLOO FDOO WKH LQW9DOXH PHWKRG IRU \RX WR XQZUDS WKH REMHFW 7KLV SURFHVV LV NQRZQ DV XQER[LQJ 8QER[LQJ $Q DXWRPDWLF FRQYHUVLRQ IURP D ZUDSSHG REMHFW WR LWV FRUUHVSRQGLQJ SULPLWLYH GDWD H J DQ ,QWHJHU XQER[HG WR \LHOG DQ LQW :H FDQ HYHQ UHZULWH RXU IRU HDFK ORRS WR XVH D YDULDEOH RI W\SH LQW HYHQ WKRXJK WKH $UUD\/LVW VWRUHV YDOXHV RI W\SH ,QWHJHU %HFDXVH RI DXWRPDWLF XQER[LQJ -DYD ZLOO SHUIRUP WKH DSSURSULDWH FRQYHUVLRQV IRU XV 6R WKH FRGH IUDJPHQW DERYH FDQ EH UHZULWWHQ DV IROORZV ArrayList<Integer> list = new ArrayList<Integer>(); list.add(13); list.add(47); list.add(15); list.add(9); int sum = 0; for (int n : list) { sum += n; } System.out.println("list = " + list); System.out.println("sum = " + sum); %HFDXVH RI ER[LQJ DQG XQER[LQJ \RX ZLOO ILQG WKDW WKH RQO\ SODFH \RX JHQHUDOO\ QHHG WR XVH WKH ZUDSSHU FODVV LV LQ GHILQLQJ D W\SH OLNH $UUD\/LVW,QWHJHU! <RX FDQ W GHILQH LW WR EH RI W\SH $UUD\/LVWLQW! EXW HYHQ WKRXJK LW LV RI W\SH $UUD\/LVW,QWHJHU! \RX FDQ PDQLSXODWH LW DV LI LW LV RI W\SH $UUD\/LVWLQW! %HORZ LV D OLVW RI WKH PDMRU SULPLWLYH W\SHV DQG WKHLU FRUUHVSRQGLQJ ZUDSSHU FODVVHV &RPPRQ :UDSSHU &ODVVHV 3ULPLWLYH 7\SH :UDSSHU &ODVV LQW GRXEOH FKDU ERROHDQ ,QWHJHU 'RXEOH &KDUDFWHU %RROHDQ 'LG \RX .QRZ &RQWURYHUV\ 2YHU %R[LQJ DQG 8QER[LQJ 1RW DOO VRIWZDUH GHYHORSHUV DUH KDSS\ ZLWK 6XQ V GHFLVLRQ WR DGG ER[LQJ DQG XQER[LQJ WR WKH ODQJXDJH 7KH IDFW WKDW DQ $UUD\/LVW,QWHJHU! FDQ EH PDQLSXODWHG DOPRVW DV LI LW ZHUH DQ $UUD\/LVWLQW! FDQ VLPSOLI\ FRGH DQG HYHU\RQH DJUHHV WKDW VLPSOLILFDWLRQ LV JRRG %XW WKH 546 GLVDJUHHPHQW FRPHV IURP WKH IDFW WKDW LW LV DOPRVW OLNH DQ $UUD\/LVWLQW! 7KH DUJXPHQW LV WKDW DOPRVW LVQ W JRRG HQRXJK 7KH IDFW WKDW LW FRPHV FORVH PHDQV WKDW \RX DUH OLNHO\ WR XVH LW DQG HYHQWXDOO\ FRPH WR FRXQW RQ LW 7KDW FDQ SURYH GLVDVWURXV ZKHQ DOPRVW LVQ W DOZD\V )RU H[DPSOH VXSSRVH WKDW VRPHRQH WROG \RX WR XVH D GHYLFH WKDW LV DOPRVW OLNH D SRWKROGHU ,Q PRVW FDVHV LW ZLOO SURWHFW \RXU KDQG IURP KHDW 6R \RX VWDUW XVLQJ LW DQG \RX PLJKW EH QHUYRXV DW ILUVW EXW WKHQ \RX ILQG WKDW LW VHHPV WR ZRUN MXVW ILQH $QG WKHQ RQH GD\ \RX UH VXUSULVHG WR ILQG WKDW D VPDOO DUHD LVQ W EHKDYLQJ OLNH D SRWKROGHU DQG \RX JHW EXUQHG <RX FDQ WKLQN RI VLPLODU VFHQDULRV ZLWK DLUFUDIW ODQGLQJ JHDU WKDW DOPRVW ZRUNV RU YHVWV WKDW DUH DOPRVW EXOOHW SURRI )RU H[DPSOH FRQVLGHU WKH IROORZLQJ FRGH int n = 42; ArrayList<Integer> list = new ArrayList<Integer>(); list.add(n); list.add(n); if (list.get(0) == list.get(1)) { System.out.println("equal"); } else { System.out.println("unequal"); } ,W V GLIILFXOW WR NQRZ H[DFWO\ ZKDW WKLV FRGH ZLOO GR ,I ZH WKLQN RI WKH $UUD\/LVW,QWHJHU! DV EHLQJ DOPRVW OLNH DQ $UUD\/LVWLQW! ZH ZRXOG EH LQFOLQHG WR WKLQN WKDW WKH FRGH ZRXOG SULQW WKH PHVVDJH WKDW WKH WZR YDOXHV DUH HTXDO 7KH FRGH GRHV LQ IDFW SULQW WKDW PHVVDJH %XW UHPHPEHU WKDW WHVWLQJ IRU REMHFW HTXDOLW\ LV QRW DV VLPSOH DV WHVWLQJ IRU HTXDOLW\ RI SULPLWLYH GDWD 7ZR 6WULQJV PLJKW VWRUH WKH VDPH WH[W EXW QRW EH WKH VDPH REMHFW ZKLFK LV ZK\ ZH FDOO WKH HTXDOV PHWKRG WR FRPSDUH 6WULQJV 7KH VDPH DSSOLHV KHUH 7KH WZR OLVW HOHPHQWV PLJKW VWRUH WKH VDPH LQW EXW QRW EH WKH VDPH REMHFW 7KH FRGH DERYH KDSSHQV WR ZRUN EXW GHSHQGLQJ XSRQ WKH YDOXH RI Q LW LVQ W JXDUDQWHHG WR ZRUN ,I ZH FKDQJH WKH DVVLJQPHQW RI Q WR LQVWHDG RI WKH FRGH SULQWV WKH PHVVDJH WKDW WKH YDOXHV DUH XQHTXDO 7KH -DYD /DQJXDJH 6SHFLILFDWLRQ JXDUDQWHHV WKDW WKLV FRGH ZLOO ZRUN IRU DQ\ YDOXH RI Q EHWZHHQ DQG EXW SURYLGHV QR JXDUDQWHH DV WR KRZ WKH FRGH ZLOO EHKDYH IRU RWKHU YDOXHV RI Q ,W FRXOG SULQW HLWKHU PHVVDJH $QG ZH KDYH QR JXDUDQWHH RI KRZ WKLV PLJKW FKDQJH IURP RQH LPSOHPHQWDWLRQ RI -DYD WR DQRWKHU ,Q PLJKW EH WKDW LQ WKH QH[W UHOHDVH RI -DYD WKH FRGH ZLOO SULQW HTXDOV IRU EXW QRW IRU D YDOXH OLNH 6RPH KDYH DUJXHG WKDW EHFDXVH ER[LQJ DQG XQER[LQJ FRYHU XS ZKDW LV KDSSHQLQJ XQGHUQHDWK WKDW LW LV EHWWHU QRW WR XVH WKHP DW DOO %R[LQJ DQG XQER[LQJ GRQ W QHFHVVDULO\ VLPSOLI\ DQ\WKLQJ LI WKH\ ZRUN RQO\ VRPHWLPHV EHFDXVH \RX KDYH WR EH DEOH WR XQGHUVWDQG WKH FDVHV ZKHUH WKH\ GRQ W ZRUN 7KH &RPSDUDEOH ,QWHUIDFH :H VDZ LQ &KDSWHU WKDW DUUD\V FDQ EH VRUWHG E\ FDOOLQJ WKH PHWKRG $UUD\V VRUW WKDW LV SDUW RI WKH MDYD XWLO SDFNDJH 7KHUH LV D VLPLODU PHWKRG WKDW FDQ EH XVHG WR VRUW DQ $UUD\/LVW WKDW LV FDOOHG &ROOHFWLRQV VRUW ,W LV DOVR SDUW RI WKH MDYD XWLO SDFNDJH 7KH IROORZLQJ VKRUW SURJUDP GHPRQVWUDWHV KRZ WR XVH &ROOHFWLRQV VRUW 547 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // Short program that constructs an ArrayList of Strings and sorts it. import java.util.*; public class SortExample { public static void main(String args) { ArrayList<String> words = new ArrayList<String>(); words.add("four"); words.add("score"); words.add("and"); words.add("seven"); words.add("years"); words.add("ago"); // show list before and after sorting System.out.println("before sort, words = " + words); Collections.sort(words); System.out.println("after sort, words = " + words); } } 7KLV SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW before sort, words = [four, score, and, seven, years, ago] after sort, words = [ago, and, four, score, seven, years] ,I ZH WU\ WR GR VRPHWKLQJ VLPLODU ZLWK DQ $UUD\/LVW3RLQW! ZH ZLOO ILQG WKDW WKH SURJUDP GRHV QRW FRPSLOH :K\ FDQ ZH VRUW D OLVW RI 6WULQJ REMHFWV EXW QRW D OLVW RI 3RLQW REMHFWV" 7KH DQVZHU LV WKDW WKH 6WULQJ FODVV LPSOHPHQWV DQ LQWHUIDFH NQRZQ DV WKH &RPSDUDEOH LQWHUIDFH ZKLOH WKH 3RLQW FODVV GRHV QRW ,Q WKLV VHFWLRQ ZH ZLOO H[SORUH WKH GHWDLOV RI WKH &RPSDUDEOH LQWHUIDFH DQG OHDUQ KRZ WR ZULWH FODVVHV WKDW LPSOHPHQW LW 1DWXUDO 2UGHULQJ DQG FRPSDUH7R :H DUH IDPLOLDU ZLWK PDQ\ NLQGV RI GDWD WKDW FDQ EH VRUWHG )RU H[DPSOH ZH DUH XVHG WR SXWWLQJ QXPEHUV LQWR LQFUHDVLQJ RUGHU RU DOSKDEHWL]LQJ D OLVW RI QDPHV :H GHVFULEH W\SHV WKDW FDQ EH VRUWHG DV KDYLQJ D QDWXUDO RUGHULQJ RI YDOXHV 7R KDYH VXFK DQ RUGHULQJ RI YDOXHV D W\SH QHHGV D ZHOO GHILQHG FRPSDULVRQ IXQFWLRQ WKDW LQGLFDWHV WKH UHODWLRQVKLS EHWZHHQ DQ\ SDLU RI YDOXHV &RPSDULVRQ )XQFWLRQ $ ZHOO GHILQHG SURFHGXUH IRU GHFLGLQJ JLYHQ D SDLU RI YDOXHV WKH UHODWLYH RUGHU RI WKH WZR YDOXHV OHVV WKDQ HTXDO RU JUHDWHU WKDQ 1DWXUDO 2UGHULQJ 7KH RUGHU LPSRVHG RQ D W\SH E\ LWV FRPSDULVRQ IXQFWLRQ 1RW DOO W\SHV KDYH D QDWXUDO RUGHULQJ EHFDXVH QRW DOO W\SHV KDYH D FRPSDULVRQ IXQFWLRQ )RU H[DPSOH ZH KDYH EHHQ H[SORULQJ LQ WKLV FKDSWHU KRZ WR FRQVWUXFW D YDULHW\ RI $UUD\/LVW REMHFWV +RZ ZRXOG \RX FRPSDUH WZR $UUD\/LVW REMHFWV WR GHWHUPLQH WKDW RQH LV OHVV WKDQ DQRWKHU" :KDW ZRXOG LW PHDQ IRU RQH $UUD\/LVW WR EH OHVV WKDQ DQRWKHU" <RX PLJKW GHFLGH WR XVH WKH OHQJWK RI WKH OLVW WR GHWHUPLQH ZKLFK RQH LV OHVV EXW WKHQ ZKDW GR \RX GR ZLWK WZR $UUD\/LVW REMHFWV RI HTXDO 548 OHQJWK WKDW VWRUH GLIIHUHQW YDOXHV" <RX ZRXOGQ W ZDQW WR GHVFULEH WKHP DV HTXDO 7KHUH LV QR QDWXUDO RUGHULQJ IRU $UUD\/LVW REMHFWV EHFDXVH ZH KDYH QR FRPSDULVRQ IXQFWLRQ WR ILJXUH RXW ZKDW WR GR ZLWK HDFK VXFK SDLU RI YDOXHV -DYD KDV D FRQYHQWLRQ IRU LQGLFDWLQJ WKH QDWXUDO RUGHULQJ RI D W\SH $Q\ W\SH WKDW KDV VXFK DQ RUGHULQJ VKRXOG LPSOHPHQW WKH &RPSDUDEOH LQWHUIDFH public interface Comparable<T> { public int compareTo(T other); } 7KLV LQWHUIDFH SURYLGHV D VHFRQG H[DPSOH RI D JHQHULF W\SH LQ -DYD ,Q WKH FDVH RI $UUD\/LVW 6XQ GHFLGHG WR XVH WKH OHWWHU ( ZKLFK LV VKRUW IRU (OHPHQW +HUH 6XQ GHFLGHG WR XVH WKH OHWWHU 7 ZKLFK LV VKRUW IRU 7\SH 7KH FRPSDUH7R PHWKRG LV WKH FRPSDULVRQ IXQFWLRQ IRU WKH W\SH :H FDQ W XVH D ERROHDQ UHWXUQ W\SH EHFDXVH WKHUH DUH WKUHH SRVVLEOH DQVZHUV OHVV WKDQ HTXDO RU JUHDWHU 7KH FRQYHQWLRQ IRU FRPSDUH7R LV WKDW DQ REMHFW VKRXOG UHWXUQ D QHJDWLYH QXPEHU WR LQGLFDWH D OHVV WKDQ UHODWLRQVKLS WR LQGLFDWH HTXDOLW\ D SRVLWLYH QXPEHU WR LQGLFDWH D JUHDWHU WKDQ UHODWLRQVKLS /HW V ORRN DW D IHZ H[DPSOHV WR XQGHUVWDQG WKLV EHWWHU :H KDYH VHHQ WKDW -DYD KDV ,QWHJHU REMHFWV WKDW VHUYH DV ZUDSSHUV IRU LQGLYLGXDO LQW YDOXHV :H NQRZ KRZ WR FRPSDUH LQW YDOXHV WR GHWHUPLQH WKHLU UHODWLYH RUGHU VR LW LV QRW VXUSULVLQJ WKDW WKH ,QWHJHU FODVV LPSOHPHQWV WKH &RPSDUDEOH LQWHUIDFH &RQVLGHU WKH IROORZLQJ VKRUW SURJUDP 1 2 3 4 5 6 7 8 9 10 public class CompareExample1 { public static void main(String args) { Integer x = 7; Integer y = 42; Integer z = 7; System.out.println(x.compareTo(y)); System.out.println(x.compareTo(z)); System.out.println(y.compareTo(x)); } } 7KLV SURJUDP EHJLQV E\ FRQVWUXFWLQJ WKUHH ,QWHJHU REMHFWV +--------------+ +---+ | +----+ | x | +-+---> | value | 7 | | +---+ | +----+ | +--------------+ +--------------+ +---+ | +----+ | z | +-+---> | value | 7 | | +---+ | +----+ | +--------------+ +--------------+ +---+ | +----+ | y | +-+---> | value | 42 | | +---+ | +----+ | +--------------+ 549 7KHQ LW LQFOXGHV D VHULHV RI SULQWOQ VWDWHPHQWV WKDW UHSRUW WKH UHVXOWV RI YDULRXV SDLUZLVH FRPSDULVRQV ,Q WKH ILUVW SULQWOQ ZH DVN [ WR FRPSDUH LWVHOI WR \ ZKLFK LQYROYHV FRPSDULQJ WKH LQW YDOXH WR WKH LQW YDOXH 7KLV SDLU KDV D OHVV WKDQ UHODWLRQVKLS [ LV OHVV WKDQ \ VR WKH PHWKRG FDOO UHWXUQV D QHJDWLYH LQWHJHU ,Q WKH VHFRQG SULQWOQ ZH DVN [ WR FRPSDUH LWVHOI WR ] ZKLFK LQYROYHV FRPSDULQJ RQH RFFXUUHQFH RI WKH LQW YDOXH ZLWK DQRWKHU RFFXUUHQFH RI WKH LQW YDOXH 7KLV VHFRQG SDLU KDV DQ HTXDOLW\ UHODWLRQVKLS [ HTXDOV ] VR WKH PHWKRG FDOO UHWXUQV ,Q WKH ILQDO SULQWOQ ZH DVN ] WR FRPSDUH LWVHOI WR [ ZKLFK LQYROYHV FRPSDULQJ WKH LQW YDOXH WR WKH LQW YDOXH 7KLV ILQDO SDLU KDV D JUHDWHU WKDQ UHODWLRQVKLS ] LV JUHDWHU WKDQ [ VR WKH PHWKRG FDOO UHWXUQV D SRVLWLYH LQWHJHU %HORZ LV WKH DFWXDO RXWSXW RI WKH SURJUDP -1 0 1 7KH YDOXHV DQG DUH WKH VWDQGDUG YDOXHV WR UHWXUQ EXW WKH FRPSDUH7R PHWKRG LV QRW UHTXLUHG WR UHWXUQ WKHVH VSHFLILF YDOXHV )RU H[DPSOH FRQVLGHU D VLPLODU SURJUDP WKDW FRPSDUHV 6WULQJ YDOXHV 1 2 3 4 5 6 7 8 9 10 public class CompareExample2 { public static void main(String args) { String x = "hello"; String y = "world"; String z = "hello"; System.out.println(x.compareTo(y)); System.out.println(x.compareTo(z)); System.out.println(y.compareTo(x)); } } :H KDYH VLPLODU UHODWLRQVKLSV LQ WKLV SURJUDP EHFDXVH [ LV OHVV WKDQ \ KHOOR LV OHVV WKDQ ZRUOG [ LV HTXDO WR ] WKH WZR RFFXUUHQFHV RI KHOOR DUH HTXDO DQG ] LV JUHDWHU WKDQ [ ZRUOG LV JUHDWHU WKDQ KHOOR %XW WKH RXWSXW SURGXFHG LV VOLJKWO\ GLIIHUHQW WKDQ LQ WKH ,QWHJHU H[DPSOH -15 0 15 ,QVWHDG RI DQG ZH JHW DQG 6R ZKLOH WKH YDOXHV DQG DUH RIWHQ UHWXUQHG E\ D FRPSDULVRQ IXQFWLRQ WKDW ZRQ W DOZD\V EH WKH FDVH 7KH LPSRUWDQW WKLQJ WR UHPHPEHU LV WKDW OHVV WKDQ UHODWLRQVKLSV DUH LQGLFDWHG E\ D QHJDWLYH QXPEHU DQG JUHDWHU WKDQ UHODWLRQVKLSV DUH LQGLFDWHG E\ D SRVLWLYH QXPEHU .HHS LQ PLQG WKDW WKH UHODWLRQVKLS RSHUDWRUV WKDW ZH YH EHHQ XVLQJ VLQFH &KDSWHU KDYH D GLIIHUHQW V\QWD[ )RU H[DPSOH ZH KDYH VHHQ WKDW LI \RX KDYH WZR YDULDEOHV [ DQG \ WKDW DUH RI W\SH LQW RU GRXEOH \RX FDQ FRPSDUH WKHP XVLQJ RSHUDWRUV OLNH DQG ! DV LQ int x = 7; int y = 42; if (x < y) { System.out.println("x less than y"); } 550 (YHQ WKRXJK WKH ,QWHJHU FODVV DQG 6WULQJ FODVV LPSOHPHQW WKH &RPSDUDEOH LQWHUIDFH \RX FDQ W XVH WKH UHODWLRQDO RSHUDWRU WR FRPSDUH WKHP Integer x = 7; Integer y = 42; if (x < y) { // illegal--can't compare objects this way System.out.println("x less than y"); } ,QVWHDG ZH FDOO WKH FRPSDUH7R PHWKRG DV LQ Integer x = 7; Integer y = 42; if (x.compareTo(y) < 0) { System.out.println("x less than y"); } :H FDQ XVH D UHODWLRQDO RSHUDWRU LQ WKLV FRQWH[W EHFDXVH WKH FRPSDUH7R PHWKRG UHWXUQV DQ LQW 1RWLFH WKDW ZH GRQ W XVH WKH VSHFLILF YDOXH RI IRU FRPSDUH7R EHFDXVH ZH DUH RQO\ JXDUDQWHHG WR JHW D QHJDWLYH YDOXH IRU D OHVV WKDQ UHODWLRQVKLS 7KH LI VWDWHPHQW DERYH ZRUNV HTXDOO\ ZHOO IRU WKH 6WULQJ FODVV String x = "hello"; String y = "world"; if (x.compareTo(y) < 0) { System.out.println("x less than y"); } %HORZ LV D WDEOH WKDW VXPPDUL]HV WKH VWDQGDUG &RPSDULQJ 9DOXHV 6XPPDU\ 5HODWLRQVKLS OHVV WKDQ OHVV WKDQ RU HTXDO HTXDO QRW HTXDO JUHDWHU WKDQ JUHDWHU WKDQ RU HTXDO 3ULPLWLYH 'DWD LQW GRXEOH HWF 2EMHFWV ,QWHJHU 6WULQJ HWF if (x < y) { ... } if (x <= y) { ... } if (x == y) { ... } if (x != y) { ... } if (x > y) { ... } if (x > y) { ... } if (x.compareTo(y) ... } if (x.compareTo(y) ... } if (x.compareTo(y) ... } if (x.compareTo(y) ... } if (x.compareTo(y) ... } if (x.compareTo(y) ... } < 0) { <= 0) { == 0) { != 0) { > 0) { >= 0) { 551 ,PSOHPHQWLQJ &RPSDUDEOH /HW V H[SORUH KRZ WR LPSOHPHQW WKH &RPSDUDEOH LQWHUIDFH 7R NHHS WKLQJV UHODWLYHO\ VLPSOH OHW V H[SORUH D FODVV WKDW ZRXOG NHHS WUDFN RI D FDOHQGDU GDWH 7KH LGHD LV WR NHHS WUDFN RI D SDUWLFXODU PRQWK DQG GD\ ZLWKRXW NHHSLQJ WUDFN RI D \HDU )RU H[DPSOH WKH 8QLWHG 6WDWHV FHOHEUDWHV LWV LQGHSHQGHQFH RQ -XO\ WK HDFK \HDU 6LPLODUO\ DQ RUJDQL]DWLRQ PLJKW ZDQW D ELUWKGD\ OLVW WKDW GRHVQ W LQFOXGH LQIRUPDWLRQ DERXW KRZ ROG SHRSOH DUH :H FDQ LPSOHPHQW WKLV DV D FODVV ZLWK WZR GDWD ILHOGV WR VWRUH WKH PRQWK DQG GD\ public class CalendarDate { private int month; private int day; public CalendarDate(int month, int day) { this.month = month; this.day = day; } // other methods } 5HPHPEHU WKDW WR LPSOHPHQW DQ LQWHUIDFH \RX LQFOXGH DQ H[WUD QRWDWLRQ LQ WKH FODVV KHDGHU ,PSOHPHQWLQJ WKH &RPSDUDEOH LQWHUIDFH LV D OLWWOH PRUH FKDOOHQJLQJ EHFDXVH LW LV D JHQHULF LQWHUIDFH &RPSDUDEOH7! 6R ZH FDQ W VLPSO\ VD\ public class CalendarDate implements Comparable { ... } // not correct :H KDYH WR UHSODFH WKH 7! LQ &RPSDUDEOH7! :KHQHYHU \RX LPSOHPHQW &RPSDUDEOH \RX ZLOO EH FRPSDULQJ SDLUV RI YDOXHV IURP WKH VDPH FODVV 6R D FODVV FDOOHG &DOHQGDU'DWH VKRXOG LPSOHPHQW &RPSDUDEOH&DOHQGDU'DWH! ,I \RX ORRN DW WKH KHDGHU IRU WKH ,QWHJHU FODVV \RX ZLOO ILQG WKDW LW LPSOHPHQWV &RPSDUDEOH,QWHJHU! $QG WKH 6WULQJ FODVV LPSOHPHQWV &RPSDUDEOH6WULQJ! 6R ZH QHHG WR FKDQJH WKH KHDGHU WR WKH IROORZLQJ public class CalendarDate implements Comparable<CalendarDate> { ... } &ODLPLQJ WR LPSOHPHQW WKH LQWHUIDFH LV QRW HQRXJK :H DOVR KDYH WR LQFOXGH DSSURSULDWH PHWKRGV ,Q WKLV FDVH WKH &RPSDUDEOH LQWHUIDFH KDV MXVW D VLQJOH PHWKRG IRU XV WR LQFOXGH public interface Comparable<T> { public int compareTo(T other); } %HFDXVH ZH DUH XVLQJ &DOHQGDU'DWH LQ SODFH RI 7 ZH QHHG WR ZULWH D FRPSDUH7R PHWKRG WKDW WDNHV D SDUDPHWHU RI W\SH &DOHQGDU'DWH public int compareTo(CalendarDate other) { ... } 552 1RZ ZH KDYH WR ILJXUH RXW KRZ WR FRPSDUH WZR GDWHV (DFK &DOHQGDU'DWH REMHFW ZLOO KDYH GDWD ILHOGV VWRULQJ PRQWK DQG GD\ :LWK FDOHQGDUV WKH PRQWK WDNHV SUHFHGHQFH RYHU WKH GD\ ,I ZH ZDQW WR FRPSDUH -DQXDU\ ZLWK $SULO ZH GRQ W FDUH WKDW FRPHV EHIRUH ZH FDUH PRUH DERXW WKH IDFW WKDW -DQXDU\ FRPHV EHIRUH $SULO 6R DV D ILUVW DWWHPSW ZH FRXOG ZULWH WKH PHWKRG DV IROORZV public int compareTo(CalendarDate other) { if (this.month < other.month) { return -1; } else if (this.month == other.month) { return 0; } else { // this.month > other.month return 1; } } 7KHUH DUH WZR SUREOHPV ZLWK WKLV DSSURDFK )LUVW WKLV FRGH XVHV D QHVWHG LI HOVH WR UHWXUQ WKH VWDQGDUG YDOXHV RI DQG :H KDYH D VLPSOHU RSWLRQ DYDLODEOH WR XV :H FDQ VLPSO\ UHWXUQ WKH GLIIHUHQFH EHWZHHQ WKLV PRQWK DQG RWKHU PRQWK EHFDXVH LW ZLOO EH QHJDWLYH ZKHQ WKLV PRQWK LV OHVV WKDQ RWKHU PRQWK LW ZLOO EH ZKHQ WKH\ DUH HTXDO DQG LW ZLOO EH SRVLWLYH ZKHQ WKLV PRQWK LV JUHDWHU WKDQ RWKHU PRQWK 6R ZH FDQ VLPSOLI\ WKH FRGH DV IROORZV public int compareTo(CalendarDate other) { return this.month - other.month; } 7KLV YHUVLRQ UHWXUQV VOLJKWO\ GLIIHUHQW YDOXHV WKDQ WKH HDUOLHU YHUVLRQ EXW LW VDWLVILHV WKH FRQWUDFW RI WKH &RPSDUDEOH LQWHUIDFH MXVW DV ZHOO DV WKH RWKHU YHUVLRQ ,W LV D JRRG LGHD WR NHHS WKLQJV VLPSOH ZKHQ \RX FDQ VR ZH ZRXOG WHQG WR XVH WKLV YHUVLRQ %XW WKH FRGH VWLOO KDV D SUREOHP :KLOH LW V WUXH WKDW PRQWKV DUH PRUH LPSRUWDQW WKDQ GD\V LQ D FDOHQGDU WKH GD\V FDQ EH LPSRUWDQW &RQVLGHU IRU H[DPSOH $SULO YHUVXV $SULO 7KH FXUUHQW YHUVLRQ RI FRPSDUH7R ZRXOG VXEWUDFW WKH PRQWKV DQG UHWXUQ D YDOXH RI LQGLFDWLQJ WKDW WKHVH WZR GDWHV DUH HTXDO 7KH GDWHV DUHQ W HTXDO ,Q SDUWLFXODU $SULO FRPHV EHIRUH $SULO ,I \RX WKLQN DERXW KRZ ZH SXW GDWHV LQWR RUGHU \RX OO UHDOL]H WKDW WKH GD\ RI WKH PRQWK EHFRPHV LPSRUWDQW RQO\ ZKHQ WKH PRQWKV DUH HTXDO 6R LI WKH PRQWKV GLIIHU WKHQ ZH XVH WKH PRQWKV WR GHWHUPLQH RUGHU 2WKHUZLVH ZKHQ WKH PRQWKV DUH HTXDO ZH XVH WKH GD\ RI WKH PRQWK WR GHWHUPLQH RUGHU 7KLV LV D FRPPRQ RUGHULQJ SULQFLSOH WKDW \RX ZLOO ILQG LQ PDQ\ WDVNV :H FDQ LPSOHPHQW WKLV VWUDWHJ\ DV IROORZV public int compareTo(CalendarDate other) { if (this.month != other.month) { return this.month - other.month; } else { return this.day - other.day; } } 553 ,W PLJKW VHHP WKDW WKLV FRGH QHYHU UHWXUQV EXW LW GRHV 6XSSRVH WKDW ZH KDYH WZR &DOHQGDU'DWH REMHFWV WKDW ERWK VWRUH $SULO :H ILQG WKDW WKH PRQWKV DUH HTXDO VR ZH UHWXUQ WKH GLIIHUHQFH EHWZHHQ WKH GDWHV 7KDW GLIIHUHQFH LV VR ZH UHWXUQ ZKLFK FRUUHFWO\ LQGLFDWHV WKDW WKH WZR GDWHV DUH HTXDO %HORZ LV D FRPSOHWH FODVV ZLWK WKH FRPSDUH7R PHWKRG WZR DFFHVVRU PHWKRGV DQG D WR6WULQJ PHWKRG 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // The CalendarDate class stores information about a single calendar date // (month and day but no year). public class CalendarDate implements Comparable<CalendarDate> { private int month; private int day; public CalendarDate(int month, int day) { this.month = month; this.day = day; } // Compares this calendar date to another date. // Dates are compared by month and then by day. public int compareTo(CalendarDate other) { if (this.month != other.month) { return this.month - other.month; } else { return this.day - other.day; } } public int getMonth() { return this.month; } public int getDay() { return this.day; } public String toString() { return this.month + "/" + this.day; } } 2QH RI WKH PDMRU EHQHILWV RI LPSOHPHQWLQJ WKH &RPSDUDEOH LQWHUIDFH LV WKDW \RX FDQ XVH EXLOW LQ XWLOLWLHV OLNH &ROOHFWLRQV VRUW :H VDZ WKDW ZH FRXOG XVH &ROOHFWLRQV VRUW WR VRUW DQ $UUD\/LVW6WULQJ! EXW QRW WR VRUW DQ $UUD\/LVW3RLQW! %HORZ LV D VKRUW SURJUDP GHPRQVWUDWLQJ WKH IDFW WKDW ZH FDQ XVH &ROOHFWLRQV VRUW IRU DQ $UUD\/LVW&DOHQGDU'DWH! 7KDW ZRUNV EHFDXVH WKH &DOHQGDU'DWH FODVV LPSOHPHQWV WKH &RPSDUDEOH LQWHUIDFH 554 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // Short program that creates a list of the birthdays of the first 5 // US Presidents and that puts them into sorted order. import java.util.*; public class CalendarDateTest { public static void main(String args) { ArrayList<CalendarDate> dates = new ArrayList<CalendarDate>(); dates.add(new CalendarDate(2, 22)); // Washington dates.add(new CalendarDate(10, 30)); // Adams dates.add(new CalendarDate(4, 13)); // Jefferson dates.add(new CalendarDate(3, 16)); // Madison dates.add(new CalendarDate(4, 28)); // Monroe System.out.println("birthdays = " + dates); Collections.sort(dates); System.out.println("birthdays = " + dates); } } 7KLV SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW birthdays = [2/22, 10/30, 4/13, 3/16, 4/28] birthdays = [2/22, 3/16, 4/13, 4/28, 10/30] 1RWLFH WKDW WKH GDWHV DSSHDU LQ LQFUHDVLQJ FDOHQGDU RUGHU DIWHU WKH FDOO RQ &ROOHFWLRQV VRUW 'LG \RX .QRZ :K\ QRW DQG IRU FRPSDUH7R" $V ZH KDYH VHHQ VRPH W\SHV OLNH ,QWHJHU UHWXUQ WKH YDOXHV RI DQG ZKHQ \RX FDOO FRPSDUH7R 7KHVH DUH WKH FDQRQLFDO YDOXHV IRU FRPSDUH7R WR UHWXUQ EHFDXVH WKH\ FRUUHVSRQG WR D IXQFWLRQ LQ 0DWKHPDWLFV NQRZQ DV WKH VLJQXP IXQFWLRQ VRPHWLPHV DEEUHYLDWHG VJQ %XW RWKHU W\SHV OLNH 6WULQJ GR QRW UHWXUQ WKH VWDQGDUG YDOXHV RI DQG <RX PLJKW ZRQGHU ZK\ 6XQ GLGQ W UHTXLUH WKDW DOO FODVVHV UHWXUQ DQG ZKHQ \RX FDOO FRPSDUH7R 2QH DQVZHU WR WKLV TXHVWLRQ LV WKDW -DYD GRHVQ W KDYH D FRQYHQLHQW WHUQDU\ W\SH )RU DQ\ ELQDU\ GHFLVLRQ ZH FDQ XVH ERROHDQ DV WKH UHWXUQ W\SH %XW ZKDW W\SH GR ZH WXUQ WR LI ZH ZDQW WR UHWXUQ H[DFWO\ RQH RI WKUHH GLIIHUHQW YDOXHV" :H GRQ W KDYH D FRQYHQLHQW SUHGHILQHG W\SH ZLWK MXVW WKUHH YDOXHV 6R LW V PRUH KRQHVW LQ VRPH VHQVH WR XVH 6XQ V UXOH WKDW DQ\ QHJDWLYH QXPEHU ZLOO GR DQG DQ\ SRVLWLYH QXPEHU ZLOO GR 6XSSRVH WKDW 6XQ VDLG WKDW FRPSDUH7R VKRXOG UHWXUQ MXVW DQG 7KHQ ZKDW VKRXOG KDSSHQ ZKHQ VRPHRQH ZULWHV D FRPSDUH7R WKDW UHWXUQV VRPHWKLQJ HOVH" ,GHDOO\ DQ\ FRGH FDOOLQJ WKDW FRPSDUH7R ZRXOG WKURZ DQ H[FHSWLRQ ZKHQ LW JHWV DQ LOOHJDO UHWXUQ YDOXH EXW WKDW ZRXOG UHTXLUH D ORW RI HUURU FKHFNLQJ FRGH %\ VD\LQJ WKDW DOO QHJDWLYHV ZLOO EH LQWHUSUHWHG RQH ZD\ DOO SRVLWLYHV ZLOO EH LQWHUSUHWHG D VHFRQG ZD\ DQG ZLOO EH LQWHUSUHWHG D WKLUG ZD\ 6XQ LV SURYLGLQJ D FRPSOHWH GHILQLWLRQ IRU DOO YDOXHV RI W\SH LQW ZKLFK PDNHV WKH FRPSDUH7R PHWKRG HDVLHU IRU SURJUDPPHUV WR ZRUN ZLWK $ VHFRQG UHDVRQ IRU KDYLQJ FRPSDUH7R EHKDYH WKLV ZD\ LV WKDW PDQ\ FRPSDULVRQ WDVNV FDQ EH HDVLO\ H[SUHVVHG GLUHFWO\ LQ WKLV ZD\ :H VDZ WKDW LW VLPSOLILHG RXU &DOHQGDU'DWH FRGH WR UHWXUQ HLWKHU WKH GLIIHUHQFH LQ WKH PRQWKV ZKHQ WKH PRQWKV ZHUH XQHTXDO RU WKH GLIIHUHQFH LQ WKH GD\V ZKHQ WKH PRQWKV ZHUH HTXDO 7KLV SDWWHUQ RFFXUV LQ PDQ\ SODFHV )RU H[DPSOH WKH 6WULQJ FODVV XVHV OH[LFRJUDSKLF RUGHU DOVR FDOOHG GLFWLRQDU\ RU DOSKDEHWLF RUGHU 7R GHWHUPLQH WKH UHODWLRQVKLS EHWZHHQ WZR 6WULQJV \RX VFDQ WKURXJK WKHP WU\LQJ WR ILQG WKH ILUVW SDLU RI OHWWHUV WKDW GLIIHU )RU H[DPSOH LI \RX ZHUH FRPSDULQJ QDWWHULQJ DQG QDEREV \RX G ILQG WKDW WKH ILUVW 555 SDLU RI FKDUDFWHUV WKDW GLIIHU LV WKH WKLUG SDLU QDW YHUVXV QDE ,I \RX ILQG VXFK D SDLU \RX FDQ UHWXUQ WKH GLIIHUHQFH EHWZHHQ WKH FKDUDFWHU YDOXHV W E ,I \RX GRQ W ILQG VXFK D SDLU WKHQ \RX UHWXUQ WKH GLIIHUHQFH EHWZHHQ WKH OHQJWKV VR WKDW QDWWHULQJ ZLOO EH FRQVLGHUHG JUHDWHU WKDQ QDW EDVHG RQ OHQJWK ,Q RWKHU ZRUGV WKH FRPSDUH7R EHKDYLRU IRU WKH 6WULQJ FODVV FDQ EH GHVFULEHG ZLWK WKH IROORZLQJ SVHXGRFRGH search for if (such a return } else { return } a pair of characters in corresponding positions that differ. pair exists) { this.char - other.char; this.length - other.length; 1RWLFH WKDW WKLV DSSURDFK UHWXUQV LQ MXVW WKH ULJKW FDVH ZKHQ WKHUH DUH QR FKDUDFWHU SDLUV WKDW GLIIHU DQG ZKHQ WKH VWULQJV KDYH WKH VDPH OHQJWK +DYLQJ WKH IOH[LELOLW\ WR UHWXUQ DQ\ QHJDWLYH LQWHJHU IRU OHVV WKDQ DQG DQ\ SRVLWLYH LQWHJHU IRU JUHDWHU WKDQ PDNHV LW HDVLHU WR LPSOHPHQW WKLV DSSURDFK 7KH ILQDO UHDVRQ WR KDYH FRPSDUH7R EHKDYH WKLV ZD\ LV HIILFLHQF\ %\ KDYLQJ D OHVV VWULFW UXOH 6XQ DOORZV SURJUDPPHUV WR ZULWH IDVWHU FRPSDUH7R PHWKRGV )RU H[DPSOH RQH RI WKH PRVW IUHTXHQWO\ FDOOHG PHWKRGV LV WKH FRPSDUH7R PHWKRG LQ WKH 6WULQJ FODVV $OO VRUWV RI GDWD FRPSDULVRQV DUH EXLOW RQ 6WULQJ FRPSDULVRQV DQG SHUIRUPLQJ D WDVN OLNH VRUWLQJ WKRXVDQGV RI UHFRUGV LV JRLQJ WR OHDG WR WKRXVDQGV RI FDOOV RQ WKH 6WULQJ FODVV FRPSDUH7R PHWKRG $V D UHVXOW ZH ZDQW WR PDNH VXUH WKDW WKH PHWKRG UXQV TXLFNO\ VR ZH ZRXOGQ W ZDQW WR XQQHFHVVDULO\ FRPSOLFDWH WKH FRGH E\ UHTXLULQJ WKDW LW DOZD\V UHWXUQ RU &DVH 6WXG\ 9RFDEXODU\ &RPSDULVRQ ,Q WKLV VHFWLRQ ZH ZLOO XVH $UUD\/LVWV WR VROYH D FRPSOH[ SUREOHP :H DUH JRLQJ WR UHDG WZR GLIIHUHQW WH[W ILOHV DQG FRPSDUH WKHLU YRFDEXODU\ 5HVHDUFKHUV LQ WKH +XPDQLWLHV RIWHQ SHUIRUP VXFK FRPSDULVRQV WR DQVZHU TXHVWLRQV OLNH 'LG &KULVWRSKHU 0DUORZH DFWXDOO\ ZULWH 6KDNHVSHDUH V SOD\V" ,Q SDUWLFXODU ZH ZLOO GHWHUPLQH WKH VHW RI ZRUGV XVHG LQ HDFK ILOH DQG FRPSXWH KRZ PXFK RYHUODS WKH\ KDYH $V ZH KDYH GRQH ZLWK PRVW RI WKH FDVH VWXGLHV ZH ZLOO GHYHORS WKH SURJUDP LQ VWDJHV 7KH ILUVW YHUVLRQ ZLOO UHDG WKH WZR ILOHV DQG UHSRUW WKH XQLTXH ZRUGV LQ HDFK :H ZLOO XVH VKRUW WHVWLQJ ILOHV IRU WKLV VWDJH 7KH VHFRQG YHUVLRQ ZLOO DOVR FRPSXWH WKH RYHUODS EHWZHHQ WKH WZR :H ZLOO FRQWLQXH WR XVH VKRUW WHVWLQJ ILOHV IRU WKLV VWDJH :H ZLOO UHDG IURP ODUJH WH[W ILOHV DQG ZLOO LQFOXGH VRPH DQDO\VLV RI WKH UHVXOWV 9HUVLRQ &RPSXWH 9RFDEXODU\ 7KH SURJUDP ZH DUH ZULWLQJ ZLOO RQO\ EH LQWHUHVWLQJ ZKHQ ZH XVH ODUJH LQSXW ILOHV WR FRPSDUH EXW ZKLOH ZH DUH GHYHORSLQJ WKH SURJUDP LW LV EHVW WR XVH VKRUW LQSXW ILOHV ZKHUH ZH FDQ HDVLO\ FKHFN ZKHWKHU ZH DUH JHWWLQJ WKH ULJKW DQVZHU %\ XVLQJ VKRUW LQSXW ILOHV ZH DOVR GRQ W KDYH WR ZRUU\ DERXW H[HFXWLRQ WLPH :KHQ \RX XVH D ODUJH LQSXW ILOH DQG WKH SURJUDP WDNHV D ORQJ WLPH WR H[HFXWH 556 LW LV GLIILFXOW WR NQRZ ZKHWKHU WKH SURJUDP ZLOO HYHU ILQLVK H[HFXWLQJ %\ GHYHORSLQJ WKH SURJUDP ZLWK VKRUW LQSXW ILOHV ZH NQRZ WKDW LW VKRXOG QHYHU WDNH D ORQJ WLPH WR H[HFXWH 6R LI ZH DFFLGHQWDOO\ LQWURGXFH DQ LQILQLWH ORRS LQWR RXU SURJUDP ZH OO NQRZ ULJKW DZD\ WKDW WKH SUREOHP LV ZLWK RXU FRGH QRW ZLWK WKH IDFW WKDW ZH KDYH D ORW RI GDWD WR SURFHVV )RU RXU SXUSRVHV OHW V XVH WKH ILUVW WZR VWDQ]DV RI D SRSXODU FKLOGUHQ V VRQJ DV RXU LQSXW ILOHV 6R ZH FDQ FUHDWH D ILOH FDOOHG WHVW W[W WKDW FRQWDLQV WKH IROORZLQJ 7KH ZKHHOV RQ WKH EXV JR URXQG DQG URXQG URXQG DQG URXQG URXQG DQG URXQG 7KH ZKHHOV RQ WKH EXV JR URXQG DQG URXQG DOO WKURXJK WKH WRZQ $QG ZH FDQ FUHDWH D ILOH FDOOHG WHVW W[W WKDW FRQWDLQV WKH IROORZLQJ The wipers on the bus go Swish, swish, swish, Swish, swish, swish, Swish, swish, swish. The wipers on the bus go Swish, swish, swish, all through the town. :H QHHG WR RSHQ HDFK RI WKHVH ILOHV ZLWK D 6FDQQHU VR RXU PDLQ PHWKRG ZLOO EHJLQ ZLWK Scanner input1 = new Scanner(new File("test1.txt")); Scanner input2 = new Scanner(new File("test2.txt")); 7KHQ ZH ZDQW WR FRPSXWH WKH XQLTXH YRFDEXODU\ FRQWDLQHG LQ HDFK ILOH :H FDQ VWRUH WKLV LQ DQ $UUD\/LVW6WULQJ! 7KH RSHUDWLRQ ZLOO EH WKH VDPH IRU HDFK VR LW PDNHV VHQVH WR ZULWH D VLQJOH PHWKRG WKDW ZH FDOO WZLFH 7KH PHWKRG VKRXOG WDNH WKH 6FDQQHU DV D SDUDPHWHU DQG LW VKRXOG FRQYHUW WKDW LQWR DQ $UUD\/LVW6WULQJ! WKDW FRQWDLQV WKH YRFDEXODU\ 6R DIWHU RSHQLQJ WKH ILOHV ZH FDQ H[HFXWH WKH IROORZLQJ FRGH ArrayList<String> list1 = getWords(input1); ArrayList<String> list2 = getWords(input2); 7KLV YHUVLRQ LV PHDQW WR EH IDLUO\ VLPSOH VR DIWHU ZH KDYH FRPSXWHG WKH YRFDEXODU\ IRU HDFK ILOH ZH FDQ VLPSO\ UHSRUW LW System.out.println("list1 = " + list1); System.out.println("list2 = " + list2); 6R WKH GLIILFXOW ZRUN IRU WKLV YHUVLRQ RI WKH SURJUDP UHGXFHV WR ZULWLQJ WKH JHW:RUGV PHWKRG ,W VKRXOG UHDG DOO RI WKH ZRUGV IURP WKH 6FDQQHU EXLOGLQJ XS DQ $UUD\/LVW6WULQJ! WKDW FRQWDLQV WKRVH ZRUGV :H FDQ GR WKLV IDLUO\ HDVLO\ ZLWK WKH $UUD\/LVW FRQWDLQV PHWKRG )RU RXU SXUSRVHV ZH GRQ W FDUH DERXW FDSLWDOL]DWLRQ VR ZH FDQ FRQYHUW HDFK ZRUG WR ORZHUFDVH EHIRUH ZH DGG LW WR WKH OLVW 6R ZH FDQ EXLOG XS WKH OLVW ZLWK WKH IROORZLQJ FRGH ArrayList<String> words = new ArrayList<String>(); while (input.hasNext()) { String next = input.next().toLowerCase(); if (!words.contains(next)) { words.add(next); } } 3XWWLQJ WKHVH SLHFHV WRJHWKHU ZH JHW WKH IROORZLQJ FRPSOHWH SURJUDP 557 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import java.util.*; import java.io.*; public class Vocabulary1A { public static void main(String args) throws FileNotFoundException { Scanner input1 = new Scanner(new File("test1.txt")); Scanner input2 = new Scanner(new File("test2.txt")); ArrayList<String> list1 = getWords(input1); ArrayList<String> list2 = getWords(input2); System.out.println("list1 = " + list1); System.out.println("list2 = " + list2); } public static ArrayList<String> getWords(Scanner input) { ArrayList<String> words = new ArrayList<String>(); while (input.hasNext()) { String next = input.next().toLowerCase(); if (!words.contains(next)) { words.add(next); } } return words; } } 7KLV SURJUDP EHKDYHV IDLUO\ ZHOO :KHQ ZH H[HFXWH LW ZH JHW WKH IROORZLQJ RXWSXW list1 = [the, wheels, on, bus, go, round, and, round., all, through, town.] list2 = [the, wipers, on, bus, go, swish,, swish., all, through, town.] 7KH RULJLQDO LQSXW ILOHV HDFK KDYH ZRUGV LQ WKHP :H KDYH UHGXFHG WKH ILUVW ILOH WR XQLTXH ZRUGV DQG WKH VHFRQG WR XQLTXH ZRUGV 7KH SURJUDP LV FRUUHFWO\ LJQRULQJ GLIIHUHQFHV LQ FDVH EXW LW LVQ W LJQRULQJ GLIIHUHQFHV LQ SXQFWXDWLRQ )RU H[DPSOH LW FRQVLGHUV URXQG DQG URXQG WR EH GLIIHUHQW ZRUGV RQH ZLWK D SHULRG RQH ZLWKRXW ,I ZH KDG PRUH WLPH WR LQYHVW ZH FRXOG H[SORUH ZD\V WR FRQILJXUH WKH 6FDQQHU VR WKDW LW ZRXOG LJQRUH VXFK GLIIHUHQFHV EXW IRU RXU SXUSRVHV WKLV ZLOO EH JRRG HQRXJK 6R WKLV DSSURDFK ZRUNV EXW LW LV QRW OLNHO\ WR ZRUN IDVW HQRXJK IRU ODUJH WH[W ILOHV $V WKH OLVW JURZV WKH FRQWDLQV PHWKRG LV JRLQJ WR KDYH WR ORRN DW PRUH DQG PRUH YDOXHV :H ZLOO DOVR ILQG LW FKDOOHQJLQJ WR FRPSDUH WKH WZR OLVWV LQ WKLV IRUP 0DQ\ SUREOHPV OLNH WKLV RQH DUH HDVLHU WR VROYH LI ZH FDQ SXW WKH OLVW LQWR VRUWHG RUGHU %HFDXVH ZH DUH XVLQJ DQ $UUD\/LVW6WULQJ! ZH FDQ FDOO &ROOHFWLRQV VRUW WR SXW WKH OLVW LQWR VRUWHG RUGHU 6R ZH FDQ UHZULWH WKH EHJLQQLQJ RI WKH PHWKRG WR VWRUH DOO ZRUGV LQWR WKH $UUD\/LVW DQG VRUW WKH OLVW DIWHU ILOOLQJ LW XS while (input.hasNext()) { String next = input.next().toLowerCase(); words.add(next); } Collections.sort(words); 558 2QFH WKH OLVW KDV EHHQ VRUWHG DOO RI WKH GXSOLFDWHV RI D ZRUG ZLOO EH JURXSHG WRJHWKHU %HFDXVH WKH GXSOLFDWHV ZLOO EH QH[W WR HDFK RWKHU ZH FDQ FRPSDUH DGMDFHQW HOHPHQWV DQG UHPRYH DQ\ GXSOLFDWHV WKDW ZH HQFRXQWHU %HORZ LV D SVHXGRFRGH GHVFULSWLRQ RI WKLV DSSURDFK for (each i) { if (value at i equals value at i+1) { remove value at i+1. } } :KLOH WKLV DSSURDFK FDQ ZRUN LW WXUQV RXW WR EH WRR VORZ DV ZHOO 5HPHPEHU WKDW D FDOO RQ UHPRYH UHTXLUHV VKLIWLQJ YDOXHV ,I WKH OLVW KDV WKRXVDQGV RI ZRUGV LQ LW WKHQ HDFK RI WKHVH FDOOV RQ UHPRYH LV OLNHO\ WR EH WLPH FRQVXPLQJ ,QVWHDG RI UHPRYLQJ GXSOLFDWHV IURP WKLV OLVW ZH FDQ LQVWHDG EXLOG XS D QHZ OLVW WKDW KDV WKH XQLTXH ZRUGV ,I ZH DGG WKH ZRUGV LQ DOSKDEHWLFDO RUGHU ZH FDQ DOZD\V DGG DW WKH HQG RI WKH QHZ OLVW DQG DGGLQJ DW WKH HQG RI D OLVW LV YHU\ IDVW ,Q WKH ILUVW DSSURDFK ZH ZHUH ORRNLQJ IRU GXSOLFDWHV WR UHPRYH ,Q WKLV VHFRQG DSSURDFK ZH QHHG WR ORRN IRU XQLTXH ZRUGV 7KH VLPSOHVW ZD\ WR GR WKLV LV WR ORRN IRU WUDQVLWLRQV EHWZHHQ ZRUGV )RU H[DPSOH LI ZH KDYH ILYH RFFXUUHQFHV RI RQH ZRUG IROORZHG E\ WHQ RFFXUUHQFHV RI DQRWKHU ZRUG WKHQ PRVW RI WKH SDLUV RI DGMDFHQW ZRUGV ZLOO EH HTXDO WR HDFK RWKHU %XW LQ WKH PLGGOH RI WKRVH HTXDO SDLUV WKHUH ZLOO EH D SDLU WKDW DUH QRW HTXDO ZKHQ ZH PDNH WKH WUDQVLWLRQ IURP WKH ILUVW ZRUG WR WKH VHFRQG ZRUG :KHQHYHU ZH VHH VXFK D WUDQVLWLRQ ZH NQRZ WKDW ZH DUH VHHLQJ D QHZ ZRUG WKDW VKRXOG EH DGGHG WR RXU QHZ OLVW /RRNLQJ IRU WUDQVLWLRQV OHDGV WR D FODVVLF IHQFHSRVW SUREOHP )RU H[DPSOH LI WKHUH DUH XQLTXH ZRUGV WKHUH ZLOO EH WUDQVLWLRQV :H FDQ VROYH WKH IHQFHSRVW SUREOHP E\ DGGLQJ WKH ILUVW ZRUG EHIRUH WKH ORRS EHJLQV 7KHQ ZH FDQ ORRN IRU ZRUGV WKDW DUH QRW HTXDO WR WKH ZRUGV WKDW FRPH EHIRUH WKHP DQG DGG WKHP WR WKH OLVW ([SUHVVHG DV SVHXGRFRGH ZH JHW WKH IROORZLQJ construct a new empty list. add first word to new list. for (each i) { if (value at i does not equal value at i-1) { add value at i. } } 7KLV FDQ EH FRQYHUWHG LQWR DFWXDO FRGH IDLUO\ GLUHFWO\ EXW ZH KDYH WR EH FDUHIXO WR VWDUW L DW UDWKHU WKDQ EHFDXVH LQ WKH ORRS ZH FRPSDUH HDFK ZRUG DV WKH RQH WKDW FRPHV EHIRUH LW DQG WKH ILUVW ZRUG KDV QRWKLQJ EHIRUH LW :H DOVR KDYH WR EH FDUHIXO WR FDOO WKH $UUD\/LVW JHW PHWKRG WR REWDLQ LQGLYLGXDO YDOXHV DQG WR XVH WKH HTXDOV PHWKRG WR FRPSDUH 6WULQJV IRU HTXDOLW\ ArrayList<String> result = new ArrayList<String>(); result.add(words.get(0)); for (int i = 1; i < words.size(); i++) { if (!words.get(i).equals(words.get(i - 1))) { result.add(words.get(i)); } } 559 7KHUH LV VWLOO RQH PLQRU SUREOHP ZLWK WKLV FRGH ,I WKH LQSXW ILOH LV HPSW\ WKHQ WKHUH ZRQ W EH D ILUVW ZRUG WR DGG WR WKH QHZ OLVW 6R ZH QHHG DQ H[WUD LI WR PDNH VXUH WKDW ZH GRQ W WU\ WR DGG YDOXHV WR WKH QHZ OLVW LI WKH ILUVW OLVW LV HPSW\ 3XWWLQJ DOO RI WKHVH FKDQJHV WRJHWKHU ZH JHW WKH IROORZLQJ SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 import java.util.*; import java.io.*; public class Vocabulary1B { public static void main(String args) throws FileNotFoundException { Scanner input1 = new Scanner(new File("test1.txt")); Scanner input2 = new Scanner(new File("test2.txt")); ArrayList<String> list1 = getWords(input1); ArrayList<String> list2 = getWords(input2); System.out.println("list1 = " + list1); System.out.println("list2 = " + list2); } public static ArrayList<String> getWords(Scanner input) { // read all words and sort ArrayList<String> words = new ArrayList<String>(); while (input.hasNext()) { String next = input.next().toLowerCase(); words.add(next); } Collections.sort(words); // add unique words to new list and return ArrayList<String> result = new ArrayList<String>(); if (words.size() > 0) { result.add(words.get(0)); for (int i = 1; i < words.size(); i++) { if (!words.get(i).equals(words.get(i - 1))) { result.add(words.get(i)); } } } return result; } } 7KH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW list1 = [all, and, bus, go, on, round, round., the, through, town., wheels] list2 = [all, bus, go, on, swish,, swish., the, through, town., wipers] 9HUVLRQ &RPSXWH 2YHUODS 7KH ILUVW YHUVLRQ RI WKH SURJUDP SURGXFHV WZR VRUWHG $UUD\/LVWV WKDW HDFK KDYH D XQLTXH VHW RI ZRUGV LQ WKHP )RU WKH VHFRQG YHUVLRQ ZH ZDQW WR FRPSXWH WKH RYHUODS EHWZHHQ WKH WZR ZRUGV DQG UHSRUW LW 7KLV RSHUDWLRQ ZLOO EH FRPSOH[ HQRXJK WKDW LW GHVHUYHV WR EH LQ LWV RZQ PHWKRG 6R ZH FDQ DGG WKH IROORZLQJ OLQH RI FRGH WR WKH PDLQ PHWKRG ULJKW DIWHU WKH WZR ZRUG OLVWV DUH FRQVWUXFWHG 560 ArrayList<String> overlap = getOverlap(list1, list2); 7KXV WKH SULPDU\ WDVN IRU WKLV VHFRQG YHUVLRQ LV WR LPSOHPHQW WKH JHW2YHUODS PHWKRG /RRN FORVHO\ DW WKH WZR OLVWV RI ZRUGV IURP WKH ILUVW YHUVLRQ list1 = [all, and, bus, go, on, round, round., the, through, town., wheels] list2 = [all, bus, go, on, swish,, swish., the, through, town., wipers] 3HRSOH DUH SUHWW\ JRRG DW ILQGLQJ PDWFKHV VR \RX FDQ SUREDEO\ VHH H[DFWO\ ZKDW ZRUGV RYHUODS %RWK OLVWV EHJLQ ZLWK DOO VR WKDW LV SDUW RI WKH RYHUODS 6NLSSLQJ SDVW WKH ZRUG DQG LQ WKH ILUVW OLVW ZH ILQG WKH QH[W PDWFK LV IRU WKH ZRUG EXV 7KHQ ZH KDYH DQRWKHU WZR PDWFKHV ZLWK WKH ZRUGV JR DQG RQ $QG WKHQ ZH KDYH VHYHUDO ZRUGV LQ D URZ LQ ERWK OLVWV WKDW GRQ W PDWFK IROORZHG HYHQWXDOO\ E\ WKH PDWFK ZLWK WKH ZRUG WKH $QG ZH ILQG RQH ILQDO PDWK ZLWK WKH ZRUG WKURXJK 6R WKH FRPSOHWH VHW RI PDWFKHV LV DV IROORZV list1 = [all, and, bus, go, on, round, round., the, through, town., wheels] | / / / / / | / / / / / | / / / / / list2 = [all, bus, go, on, swish,, swish., the, through, town., wipers] :H ZDQW WR GHVLJQ DQ DOJRULWKP WKDW SDUDOOHOV ZKDW ZH GR ZKHQ ZH ORRN IRU VXFK PDWFKHV ,PDJLQH SXWWLQJ D ILQJHU IURP \RXU OHIW KDQG RQ WKH ILUVW ZRUG LQ WKH ILUVW OLVW DQG SXWWLQJ D ILQJHU IURP \RXU ULJKW KDQG RQ WKH ILUVW ZRUG LQ WKH VHFRQG OLVW WR NHHS WUDFN RI ZKHUH \RX DUH LQ HDFK OLVW 7KHQ ZH ZLOO UHSHDWHGO\ FRPSDUH WKH ZRUGV \RX DUH SRLQWLQJ DW 'HSHQGLQJ XSRQ KRZ WKH\ FRPSDUH ZH ZLOO PRYH RQH RU ERWK ILQJHUV IRUZDUG :H VWDUW ZLWK WKH OHIW ILQJHU RQ WKH ZRUG DOO LQ WKH ILUVW OLVW DQG WKH ULJKW ILQJHU RQ WKH ZRUG DOO LQ WKH VHFRQG OLVW list1 = [all, and, bus, go, on, round, round., the, through, town., wheels] ^ list2 = [all, bus, go, on, swish,, swish., the, through, town., wipers] ^ 7KH ZRUGV \RX DUH SRLQWLQJ DW PDWFK VR \RX DGG WKDW ZRUG WR WKH RYHUODS DQG PRYH ERWK ILQJHUV IRUZDUG list1 = [all, and, bus, go, on, round, round., the, through, town., wheels] ^ list2 = [all, bus, go, on, swish,, swish., the, through, town., wipers] ^ 7KHVH ZRUGV GRQ W PDWFK 6R ZKDW GR \RX GR" ,W WXUQV RXW WKDW WKH ZRUG EXV LQ OLVW LV JRLQJ WR PDWFK D ZRUG LQ OLVW 6R KRZ GR \RX NQRZ WR PRYH WKH OHIW ILQJHU IRUZDUG" :H NQRZ WKDW EHFDXVH ZH DUH SRLQWLQJ DW WKH ZRUG DQG IURP WKH ILUVW OLVW DQG WKH ZRUG EXV IURP WKH VHFRQG OLVW %HFDXVH WKH OLVW LV VRUWHG DQG EHFDXVH WKH ZRUG DQG FRPHV EHIRUH WKH ZRUG EXV ZH NQRZ WKHUH FDQ W EH D PDWFK IRU WKH ZRUG DQG LQ WKH VHFRQG OLVW (YHU\ ZRUG WKDW FRPHV DIWHU EXV LQ WKH VHFRQG OLVW LV JRLQJ WR EH DOSKDEHWLFDOO\ JUHDWHU WKDQ EXV VR WKH ZRUG DQG MXVW FDQ W EH WKHUH 6R ZH FDQ PRYH WKH OHIW ILQJHU IRUZDUG WR VNLS WKH ZRUG DQG 561 list1 = [all, and, bus, go, on, round, round., the, through, town., wheels] ^ list2 = [all, bus, go, on, swish,, swish., the, through, town., wipers] ^ 7KLV JHWV XV WR WKH VHFRQG PDWFK DQG WKH DOJRULWKP SURFHHGV ,Q JHQHUDO ZH ZLOO ILQG RXUVHOYHV LQ RQH RI WKUHH VLWXDWLRQV ZKHQ ZH FRPSDUH WKH FXUUHQW ZRUG LQ OLVW ZLWK WKH FXUUHQW ZRUG LQ OLVW 7KH ZRUGV PLJKW EH HTXDO LQ ZKLFK FDVH ZH YH IRXQG D PDWFK WKDW VKRXOG EH LQFOXGHG LQ WKH RYHUODS DQG ZH VKRXOG WKHQ VNLS WKH ZRUG LQ HDFK OLVW 7KH ZRUG IURP WKH ILUVW OLVW PLJKW EH DOSKDEHWLFDOO\ OHVV LQ ZKLFK FDVH ZH FDQ VNLS LW EHFDXVH LW FDQ W PDWFK DQ\WKLQJ LQ WKH VHFRQG OLVW 7KH ZRUG IURP WKH VHFRQG OLVW PLJKW EH DOSKDEHWLFDOO\ OHVV LQ ZKLFK FDVH ZH FDQ VNLS LW EHFDXVH LW FDQ W PDWFK DQ\WKLQJ LQ WKH ILUVW OLVW 7KXV WKH EDVLF DSSURDFK ZH ZDQW WR XVH FDQ EH GHVFULEHG ZLWK WKH IROORZLQJ SVHXGRFRGH if (word from list1 equals word from list2) { record match. skip past word in each list. } else if (word from list1 < word from list2) { skip past word in list1. } else { skip past word in list2. } :H FDQ UHILQH WKLV SVHXGRFRGH E\ LQWURGXFLQJ WZR LQGH[ YDULDEOHV DQG SXWWLQJ WKLV FRGH LQVLGH RI D ORRS int index1 = 0; int index2 = 0; while (more values to compare) { if (list1.get(index1) equals list2.get(index2)) { record match. index1++; index2++; } else if (list.get(index1) < list.get(index2)) { index1++; } else { index2++; } } 7KLV LV QRZ IDLUO\ FORVH WR DFWXDO FRGH )LUVW ZH KDYH WR ILJXUH RXW DQ DSSURSULDWH ORRS WHVW :H VWDUW WKH WZR LQGH[ YDULDEOHV DW DQG LQFUHPHQW RQH RU ERWK HDFK WLPH WKURXJK WKH ORRS (YHQWXDOO\ ZH OO UXQ RXW RI YDOXHV LQ RQH RU ERWK OLVWV :KHQ WKDW KDSSHQV WKHUH ZRQ W EH DQ\ PRUH PDWFKHV WR ILQG 6R ZH ZDQW WR FRQWLQXH LQ WKH ZKLOH ORRS DV ORQJ DV WKH WZR LQGH[ YDULDEOHV KDYHQ W UHDFKHG WKH HQG RI WKH OLVW :H DOVR KDYH WR ILJXUH RXW KRZ WR FRPSDUH WKH WZR ZRUGV 5HPHPEHU WKDW WKH 6WULQJ FODVV LPSOHPHQWV WKH &RPSDUDEOH LQWHUIDFH ZKLFK PHDQV LW KDV D FRPSDUH7R PHWKRG )LQDOO\ ZH KDYH WR FRQVWUXFW DQ $UUD\/LVW WR VWRUH WKH RYHUODS DQG ZH KDYH WR UHWXUQ LW DIWHU WKH ORRS 7KXV ZH FDQ WXUQ WKLV LQWR WKH IROORZLQJ DFWXDO FRGH 562 ArrayList<String> result = new ArrayList<String>(); int index1 = 0; int index2 = 0; while (index1 < list1.size() && index2 < list2.size()) { int comparison = list1.get(index1).compareTo(list2.get(index2)); if (comparison == 0) { result.add(list1.get(index1)); index1++; index2++; } else if (comparison < 0) { index1++; } else { // comparison > 0 index2++; } } return result; 7XUQLQJ WKLV LQWR D PHWKRG DQG PRGLI\LQJ PDLQ WR FDOO WKLV PHWKRG DQG WR UHSRUW WKH RYHUODS ZH HQG XS ZLWK WKH IROORZLQJ QHZ YHUVLRQ RI WKH SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import java.util.*; import java.io.*; public class Vocabulary2 { public static void main(String args) throws FileNotFoundException { Scanner input1 = new Scanner(new File("test1.txt")); Scanner input2 = new Scanner(new File("test2.txt")); ArrayList<String> list1 = getWords(input1); ArrayList<String> list2 = getWords(input2); ArrayList<String> overlap = getOverlap(list1, list2); System.out.println("list1 = " + list1); System.out.println("list2 = " + list2); System.out.println("overlap = " + overlap); } public static ArrayList<String> getWords(Scanner input) { // read all words and sort ArrayList<String> words = new ArrayList<String>(); while (input.hasNext()) { String next = input.next().toLowerCase(); words.add(next); } Collections.sort(words); // add unique words to new list and return ArrayList<String> result = new ArrayList<String>(); if (words.size() > 0) { result.add(words.get(0)); for (int i = 1; i < words.size(); i++) { if (!words.get(i).equals(words.get(i - 1))) { result.add(words.get(i)); } } } return result; } 563 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public static ArrayList<String> getOverlap(ArrayList<String> list1, ArrayList<String> list2) { ArrayList<String> result = new ArrayList<String>(); int index1 = 0; int index2 = 0; while (index1 < list1.size() && index2 < list2.size()) { int comparison = list1.get(index1).compareTo(list2.get(index2)); if (comparison == 0) { result.add(list1.get(index1)); index1++; index2++; } else if (comparison < 0) { index1++; } else { // comparison > 0 index2++; } } return result; } } ,W SURGXFHV WKH IROORZLQJ RXWSXW list1 = [all, and, bus, go, on, round, round., the, through, town., wheels] list2 = [all, bus, go, on, swish,, swish., the, through, town., wipers] overlap = [all, bus, go, on, the, through, town.] 9HUVLRQ &RPSOHWH 3URJUDP 2XU SURJUDP QRZ FRUUHFWO\ EXLOGV D YRFDEXODU\ OLVW IRU HDFK RI WZR ILOHV DQG FRPSXWHV WKH RYHUODS EHWZHHQ WKHP 7KH ODVW YHUVLRQ RI WKH SURJUDP SULQWHG WKH WKUHH OLVWV RI ZRUGV EXW WKDW ZRQ W EH YHU\ FRQYHQLHQW IRU ODUJH WH[W ILOHV /DUJH WH[W ILOHV KDYH WKRXVDQGV RI GLIIHUHQW ZRUGV :H FDQ LQVWHDG UHSRUW RYHUDOO VWDWLVWLFV LQFOXGLQJ WKH QXPEHU RI ZRUGV LQ HDFK OLVW WKH QXPEHU RI ZRUGV RI RYHUODS DQG WKH SHUFHQW RI RYHUODS 7KH SURJUDP DOVR VKRXOG KDYH DW OHDVW D EULHI LQWURGXFWLRQ WR H[SODLQ ZKDW LW GRHV DQG ZH FDQ ZULWH LW VR WKDW LW SURPSWV IRU ILOH QDPHV UDWKHU WKDQ XVLQJ KDUG FRGHG ILOH QDPHV %HORZ LV WKH FRPSOHWH SURJUDP 564 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 // This program reads two text files and compares the vocabulary used in each. import java.util.*; import java.io.*; public class Vocabulary3 { public static void main(String args) throws FileNotFoundException { Scanner console = new Scanner(System.in); giveIntro(); System.out.print("file #1 name? "); Scanner input1 = new Scanner(new File(console.nextLine())); System.out.print("file #2 name? "); Scanner input2 = new Scanner(new File(console.nextLine())); System.out.println(); ArrayList<String> list1 = getWords(input1); ArrayList<String> list2 = getWords(input2); ArrayList<String> overlap = getOverlap(list1, list2); reportResults(list1, list2, overlap); } // post: reads all words from the given Scanner, turning them to lowercase // and returning a sorted list of the vocabulary of the file public static ArrayList<String> getWords(Scanner input) { // read all words and sort ArrayList<String> words = new ArrayList<String>(); while (input.hasNext()) { String next = input.next().toLowerCase(); words.add(next); } Collections.sort(words); // add unique words to new list and return ArrayList<String> result = new ArrayList<String>(); if (words.size() > 0) { result.add(words.get(0)); for (int i = 1; i < words.size(); i++) { if (!words.get(i).equals(words.get(i - 1))) { result.add(words.get(i)); } } } return result; } // pre : list1 and list2 are sorted and contain no duplicates // post: constructs and returns an ArrayList containing the words // in common betweeen list1 and list2 public static ArrayList<String> getOverlap(ArrayList<String> list1, ArrayList<String> list2) { ArrayList<String> result = new ArrayList<String>(); int index1 = 0; int index2 = 0; while (index1 < list1.size() && index2 < list2.size()) { int comparison = list1.get(index1).compareTo(list2.get(index2)); if (comparison == 0) { result.add(list1.get(index1)); index1++; 565 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 index2++; } else if (comparison < 0) { index1++; } else { // comparison > 0 index2++; } } return result; } // post: explains program to user public static void giveIntro() { System.out.println("This program compares the vocabulary of two"); System.out.println("text files, reporting the number of words"); System.out.println("in common and the percent of overlap."); System.out.println(); } // pre : overlap contains the words in commmon between list1 and list2 // post: reports statistics about two word lists and their overlap public static void reportResults(ArrayList<String> list1, ArrayList<String> list2, ArrayList<String> overlap) { System.out.println("file #1 words = " + list1.size()); System.out.println("file #2 words = " + list2.size()); System.out.println("common words = " + overlap.size()); double percent1 = 100.0 * overlap.size() / double percent2 = 100.0 * overlap.size() / System.out.println("% of file 1 in overlap System.out.println("% of file 2 in overlap } } list1.size(); list2.size(); = " + percent1); = " + percent2); %HORZ LV DQ H[HFXWLRQ RI WKH SURJUDP WKDW FRPSDUHV WKH WH[W RI 6KDNHVSHDUH V +DPOHW ZLWK WKH WH[W RI 6KDNHVSHDUH V /HDU This program compares the vocabulary of two text files, reporting the number of words in common and the percent of overlap. file #1 name? hamlet.txt file #2 name? lear.txt file #1 words = 7234 file #2 words = 6497 common words = 2482 % of file 1 in overlap = 34.310201824716614 % of file 2 in overlap = 38.20224719101124 %HORZ LV D VHFRQG H[HFXWLRQ WKDW FRPSDUHV WKH WH[W RI 0RE\ 'LFN WR WKH WH[W RI +DPOHW 566 This program compares the vocabulary of two text files, reporting the number of words in common and the percent of overlap. file #1 name? moby.txt file #2 name? hamlet.txt file #1 words = 30368 file #2 words = 7234 common words = 3679 % of file 1 in overlap = 12.11472602739726 % of file 2 in overlap = 50.85706386508156 &KDSWHU 6XPPDU\ 7KH $UUD\/LVW FODVV LQ -DYD V MDYD XWLO SDFNDJH LV D FODVV WKDW UHSUHVHQWV D JURZDEOH OLVW RI REMHFWV LPSOHPHQWHG XVLQJ DQ DUUD\ <RX FDQ XVH DQ $UUD\/LVW WR VWRUH REMHFWV LQ D VHTXHQWLDO RUGHU (DFK HOHPHQW KDV D EDVHG LQGH[ $UUD\/LVW LV D JHQHULF FODVV $ JHQHULF FODVV LV RQH WKDW DFFHSWV D GDWD W\SH DV D SDUDPHWHU ZKHQ FUHDWHG VXFK DV $UUD\/LVW6WULQJ! $Q $UUD\/LVW PDLQWDLQV LWV RZQ VL]H IRU \RX HOHPHQWV FDQ EH DGGHG DQG UHPRYHG DW DQ\ LQGH[ XS WR WKH VL]H RI WKH OLVW $Q $UUD\/LVW FDQ DOVR EH SULQWHG WR WKH FRQVROH 2WKHU $UUD\/LVW RSHUDWLRQV LQFOXGH JHW VHW DQG FOHDU $UUD\/LVWV FDQ EH VHDUFKHG XVLQJ PHWKRGV QDPHG FRQWDLQV LQGH[2I DQG ODVW,QGH[2I -DYD V IRU HDFK ORRS FDQ EH XVHG WR H[DPLQH HDFK HOHPHQW RI DQ $UUD\/LVW 7KH OLVW FDQ W EH PRGLILHG GXULQJ WKH IRU HDFK ORRS :KHQ VWRULQJ SULPLWLYH YDOXHV VXFK DV LQW RU GRXEOH LQWR DQ $UUD\/LVW WKH OLVW PXVW EH GHFODUHG ZLWK VSHFLDO ZUDSSHU W\SHV VXFK DV ,QWHJHU DQG 'RXEOH 2EMHFWV WKDW LPSOHPHQW WKH &RPSDUDEOH LQWHUIDFH FDQ EH SODFHG LQWR DQ $UUD\/LVW DQG VRUWHG &RPSDUDEOH GHILQHV D QDWXUDO RUGHULQJ IRU WKH REMHFWV RI D FODVV 7KH REMHFWV DUH FRPSDUHG WR HDFK RWKHU LQ D PHWKRG QDPHG FRPSDUH7R ZKLFK UHWXUQV D SRVLWLYH ]HUR RU QHJDWLYH LQWHJHU 0DQ\ FRPPRQ W\SHV VXFK DV 6WULQJ DQG ,QWHJHU LPSOHPHQW &RPSDUDEOH DQG \RX FDQ DOVR LPSOHPHQW &RPSDUDEOH LQ \RXU RZQ FODVVHV 6HOI &KHFN 3UREOHPV 6HFWLRQ $UUD\/LVWV :KDW LV DQ $UUD\/LVW" ,Q ZKDW FDVHV VKRXOG \RX XVH DQ $UUD\/LVW UDWKHU WKDQ DQ DUUD\" 7KH QH[W TXHVWLRQV UHIHU WR WKH IROORZLQJ 6WULQJ HOHPHQWV ["It", "was", "a", "stormy", "night"] 567 :ULWH WKH FRGH WR GHFODUH DQ $UUD\/LVW FRQWDLQLQJ WKHVH HOHPHQWV :KDW LV WKH VL]H RI WKH OLVW" :KDW LV LWV W\SH" :ULWH FRGH WR LQVHUW WZR DGGLWLRQDO HOHPHQWV GDUN DQG DQG DW WKH SURSHU SODFH LQ WKH OLVW WR SURGXFH WKH IROORZLQJ $UUD\/LVW DV WKH UHVXOW ["It", "was", "a", "dark", "and", "stormy", "night"] :ULWH FRGH WR FKDQJH WKH VHFRQG HOHPHQW V YDOXH WR ,6 SURGXFLQJ WKH IROORZLQJ $UUD\/LVW DV WKH UHVXOW ["It", "IS", "a", "dark", "and", "stormy", "night"] :ULWH FRGH WR UHPRYH DQ\ 6WULQJV IURP WKH OLVW WKDW FRQWDLQ WKH OHWWHU D 7KH IROORZLQJ VKRXOG EH WKH OLVW V FRQWHQWV DIWHU \RXU FRGH KDV UXQ ["It", "IS", "stormy", "night"] :ULWH FRGH WR GHFODUH DQ $UUD\/LVW KROGLQJ WKH ILUVW WHQ PXOWLSOHV RI ORRS WR ILOO WKH OLVW ZLWK WKH SURSHU HOHPHQWV DQG 8VH D :ULWH D PHWKRG QDPHG maxLength WKDW WDNHV DQ $UUD\/LVW RI 6WULQJV DV D SDUDPHWHU DQG WKDW UHWXUQV WKH OHQJWK RI WKH ORQJHVW 6WULQJ LQ WKH OLVW ,I \RXU PHWKRG LV SDVVHG DQ HPSW\ $UUD\/LVW LW VKRXOG UHWXUQ :ULWH FRGH WR SULQW RXW ZKHWKHU RU QRW D OLVW RI 6WULQJV FRQWDLQV WKH YDOXH ,6 'R QRW XVH D ORRS :ULWH FRGH WR SULQW RXW WKH LQGH[ DW ZKLFK \RXU OLVW FRQWDLQV WKH YDOXH VWRUP\ DQG WKH LQGH[ DW ZKLFK LW FRQWDLQV GDUN 'R QRW XVH D ORRS :ULWH D IRU HDFK ORRS WKDW SULQWV WKH XSSHUFDVH YHUVLRQ RI HDFK 6WULQJ LQ WKH OLVW RQ LWV RZQ OLQH :KHQ UXQ RQ DQ $UUD\/LVW RI 6WULQJV WKH IROORZLQJ FRGH WKURZV DQ H[FHSWLRQ :K\" for (String s : words) { System.out.println(s); if (s.equals("hello")) { list.add("goodbye"); } } 7KH IROORZLQJ FRGH GRHV QRW FRPSLOH :K\ QRW" ([SODLQ KRZ WR IL[ LW ArrayList<int> numbers = new ArrayList<int>(); numbers.add(7); numbers.add(19); System.out.println(numbers); :KDW LV D ZUDSSHU FODVV" 'HVFULEH WKH GLIIHUHQFH EHWZHHQ DQ LQW DQG DQ ,QWHJHU 568 6HFWLRQ 7KH &RPSDUDEOH ,QWHUIDFH 'HVFULEH KRZ WR DUUDQJH DQ $UUD\/LVW LQWR VRUWHG RUGHU :KDW PXVW EH WUXH DERXW WKH W\SH RI HOHPHQWV LQ WKH OLVW WR EH DEOH WR VRUW LW" :KDW LV D QDWXUDO RUGHULQJ" +RZ GR \RX GHILQH D QDWXUDO RUGHULQJ IRU D FODVV \RX YH ZULWWHQ" *LYHQ WKH IROORZLQJ YDULDEOH GHFODUDWLRQV Integer n1 = 15; Integer n2 = 7; Integer n3 = 15; String s1 = "computer"; String s2 = "soda"; String s3 = "pencil"; )RU HDFK RI WKH IROORZLQJ FRPSDULVRQV GHVFULEH ZKHWKHU LWV UHVXOW LV SRVLWLYH QHJDWLYH RU n1.compareTo(n2) n3.compareTo(n1) n2.compareTo(n1) s1.compareTo(s2) s3.compareTo(s1) s2.compareTo(s2) :ULWH FRGH WKDW UHDGV WZR QDPHV IURP WKH FRQVROH DQG SULQWV ZKLFK FRPHV ILUVW LQ DOSKDEHWLFDO RUGHU XVLQJ WKH FRPSDUH7R PHWKRG )RU H[DPSOH Type a name: Tyler Durden Type a name: Marla Singer Marla Singer goes before Tyler Durden :ULWH FRGH WR UHDG D OLQH RI LQSXW IURP WKH XVHU DQG SULQW WKH ZRUGV RI WKDW OLQH LQ VRUWHG RUGHU )RU H[DPSOH Type a message to sort: to be or not to be that is the question Your message sorted: be be is not or question that the to to ([HUFLVHV :ULWH D PHWKRG QDPHG averageVowels WKDW WDNHV DQ $UUD\/LVW RI 6WULQJV DV D SDUDPHWHU DQG WKDW UHWXUQV WKH DYHUDJH QXPEHU RI YRZHO FKDUDFWHUV D H L R X LQ DOO 6WULQJV LQ WKH OLVW ,I \RXU PHWKRG LV SDVVHG DQ HPSW\ $UUD\/LVW LW VKRXOG UHWXUQ :ULWH D PHWKRG QDPHG swapPairs WKDW VZLWFKHV WKH RUGHU RI YDOXHV LQ DQ $UUD\/LVW RI 6WULQJV LQ D SDLUZLVH IDVKLRQ <RXU PHWKRG VKRXOG VZLWFK WKH RUGHU RI WKH ILUVW WZR YDOXHV WKHQ VZLWFK WKH RUGHU RI WKH QH[W WZR VZLWFK WKH RUGHU RI WKH QH[W WZR DQG VR RQ ,I WKHUH DUH DQ RGG QXPEHU RI YDOXHV LQ WKH OLVW WKH ILQDO HOHPHQW LV QRW PRYHG )RU H[DPSOH LI WKH OLVW LQLWLDOO\ VWRUHV > WR EH RU QRW WR EH KDPOHW @ \RXU PHWKRG ZRXOG FKDQJH WKH OLVW V FRQWHQWV WR > EH WR QRW RU EH WR KDPOHW @ :ULWH D PHWKRG QDPHG removeEvenLength WKDW WDNHV DQ $UUD\/LVW RI 6WULQJV DV D SDUDPHWHU DQG WKDW UHPRYHV DOO RI WKH 6WULQJV RI HYHQ OHQJWK IURP WKH OLVW 569 :ULWH D PHWKRG QDPHG stutter WKDW WDNHV DQ $UUD\/LVW RI 6WULQJV DV D SDUDPHWHU DQG WKDW UHSODFHV HYHU\ 6WULQJ ZLWK WZR RI WKDW 6WULQJ )RU H[DPSOH LI WKH OLVW VWRUHV WKH YDOXHV > KRZ DUH \RX" @ EHIRUH WKH PHWKRG LV FDOOHG LW VKRXOG VWRUH WKH YDOXHV > KRZ KRZ DUH DUH \RX" \RX" @ DIWHU WKH PHWKRG ILQLVKHV H[HFXWLQJ :ULWH D PHWKRG QDPHG stutterK WKDW WDNHV DQ $UUD\/LVW RI LQWHJHUV DV D SDUDPHWHU DQG WKDW UHSODFHV HYHU\ LQWHJHU RI YDOXH . ZLWK . FRSLHV RI LWVHOI )RU H[DPSOH LI WKH OLVW VWRUHV WKH YDOXHV > @ EHIRUH WKH PHWKRG LV FDOOHG LW VKRXOG VWRUH WKH YDOXHV > @ DIWHU WKH PHWKRG ILQLVKHV H[HFXWLQJ =HURHV DQG QHJDWLYH QXPEHUV VKRXOG EH UHPRYHG IURP WKH OLVW E\ WKLV PHWKRG :ULWH D VWDWLF PHWKRG PLQ7R)URQW WKDW WDNHV DQ $UUD\/LVW RI LQWV DV D SDUDPHWHU DQG WKDW PRYHV WKH PLQLPXP YDOXH LQ WKH OLVW WR WKH IURQW RWKHUZLVH SUHVHUYLQJ WKH RUGHU RI WKH HOHPHQWV )RU H[DPSOH LI D YDULDEOH FDOOHG OLVW VWRUHV > @ WKH YDOXH LV WKH PLQLPXP VR \RXU PHWKRG ZRXOG PRGLI\ WKH OLVW WR VWRUH WKH YDOXHV > @ :ULWH D PHWKRG QDPHG removeDuplicates WKDW WDNHV DV D SDUDPHWHU D VRUWHG $UUD\/LVW RI 6WULQJV DQG WKDW HOLPLQDWHV DQ\ GXSOLFDWHV IURP WKH OLVW )RU H[DPSOH LI WKH OLVW VWRUHV WKH YDOXHV > EH EH LV QRW RU TXHVWLRQ WKDW WKH WR WR @ EHIRUH WKH PHWKRG LV FDOOHG LW VKRXOG VWRUH WKH YDOXHV > EH LV QRW RU TXHVWLRQ WKDW WKH WR @ DIWHU WKH PHWKRG ILQLVKHV H[HFXWLQJ %HFDXVH WKH YDOXHV ZLOO EH VRUWHG DOO RI WKH GXSOLFDWHV ZLOO EH JURXSHG WRJHWKHU <RX PD\ DVVXPH WKDW WKH $UUD\/LVW FRQWDLQV RQO\ 6WULQJ YDOXHV EXW LW PLJKW EH HPSW\ :ULWH D PHWKRG QDPHG removeZeroes WKDW WDNHV DV D SDUDPHWHU DQ $UUD\/LVW RI LQWHJHUV DQG WKDW HOLPLQDWHV DQ\ RFFXUUHQFHV RI WKH QXPEHU IURP WKH OLVW )RU H[DPSOH LI WKH OLVW VWRUHV WKH YDOXHV > @ EHIRUH WKH PHWKRG LV FDOOHG LW VKRXOG VWRUH WKH YDOXHV > @ DIWHU WKH PHWKRG ILQLVKHV H[HFXWLQJ :ULWH D PHWKRG QDPHG rangeBetweenZeroes WKDW WDNHV DV D SDUDPHWHU DQ $UUD\/LVW RI LQWHJHUV DQG UHWXUQV KRZ PDQ\ LQGH[HV DSDUW DUH WKH WZR IXUWKHVW RFFXUUHQFHV RI WKH QXPEHU )RU H[DPSOH LI WKH OLVW VWRUHV WKH YDOXHV > @ ZKHQ WKH PHWKRG LV FDOOHG LW VKRXOG UHWXUQ EHFDXVH WKH IXUWKHVW RFFXUUHQFHV RI DUH DW LQGH[HV DQG DQG WKH UDQJH WKURXJK KDV HOHPHQWV ,I RQO\ RQH RFFXUV LQ WKH OLVW \RXU PHWKRG VKRXOG UHWXUQ ,I QR V RFFXU \RXU PHWKRG VKRXOG UHWXUQ :ULWH D PHWKRG QDPHG removeInRange WKDW DFFHSWV WKUHH SDUDPHWHUV $Q $UUD\/LVW RI 6WULQJV D EHJLQQLQJ 6WULQJ DQG DQ HQGLQJ 6WULQJ DQG UHPRYHV DQ\ 6WULQJV IURP WKH OLVW WKDW DUH DOSKDEHWLFDOO\ EHWZHHQ WKH VWDUW DQG HQG 6WULQJV )RU H[DPSOH LI WKH OLVW FRQWDLQV WKH HOHPHQWV > WR EH RU QRW WR EH WKDW LV WKH TXHVWLRQ @ DQG WKH PHWKRG LV SDVVHG WKLV OLVW IUHH DV WKH VWDUW 6WULQJ DQG ULFK DV WKH HQG 6WULQJ WKH OLVW V HOHPHQWV ZLOO EH FKDQJHG WR > WR EH WR EH WKDW WKH @ RU QRW LV DQG TXHVWLRQ ZRXOG EH UHPRYHG EHFDXVH WKH\ RFFXU DOSKDEHWLFDOO\ EHWZHHQ IUHH DQG ULFK <RX PD\ DVVXPH WKDW WKH VWDUW 6WULQJ FRPHV HDUOLHU DOSKDEHWLFDOO\ WKDQ WKH HQGLQJ 6WULQJ 0RGLI\ WKH 3RLQW FODVV IURP &KDSWHU VR WKDW LW GHILQHV D QDWXUDO RUGHULQJ E\ LPSOHPHQWLQJ WKH &RPSDUDEOH LQWHUIDFH &RPSDUH WKH 3RLQWV E\ < PDMRU RUGHU WKDW LV SRLQWV ZLWK VPDOOHU < FRRUGLQDWH YDOXHV FRPH EHIRUH WKRVH ZLWK KLJKHU < FRRUGLQDWH YDOXHV %UHDN WLHV E\ FRPSDULQJ ; FRRUGLQDWH YDOXHV 570 0RGLI\ WKH &DOHQGDU'DWH FODVV IURP WKLV FKDSWHU WR LQFOXGH D \HDU GDWD ILHOG DQG PRGLI\ LWV FRPSDUH7R PHWKRG WR WDNH \HDUV LQWR DFFRXQW ZKHQ PDNLQJ FRPSDULVRQV <HDUV WDNH SUHFHGHQFH RYHU PRQWKV ZKLFK WDNH SUHFHGHQFH RYHU GD\V )RU H[DPSOH -XO\ FRPHV EHIRUH 0DUFK 3URJUDPPLQJ 3URMHFWV :ULWH FODVVHV WR PRGHO D VKRSSLQJ OLVW 0DNH DQ ,WHP FODVV WKDW UHSUHVHQWV D JURFHU\ LWHP V QDPH DQG SULFH VXFK DV WLVVXH IRU $OVR LPSOHPHQW DQ ,WHP2UGHU FODVV WKDW UHSUHVHQWV D VKRSSHU V GHVLUH WR SXUFKDVH D JLYHQ LWHP LQ D JLYHQ TXDQWLW\ VXFK DV ER[HV RI WLVVXH <RX PLJKW ZLVK WR LPSOHPHQW EXON GLVFRXQWHG LWHPV VXFK DV ER[HV RI WLVVXH IRU ZKLFK ZRXOG PDNH WKH JLYHQ LWHP RUGHU RI ER[HV WLVVXH FRVW RU /DVWO\ LPSOHPHQW D 6KRSSLQJ&DUW FODVV WKDW VWRUHV ,WHP2UGHUV LQ DQ $UUD\/LVW DQG DOORZV LWHP RUGHUV WR EH DGGHG UHPRYHG RU VHDUFKHG IRU LQ WKH FDUW 7KH FDUW VKRXOG EH DEOH WR UHSRUW WKH WRWDO SULFH RI DOO LWHP RUGHUV LW FXUUHQWO\ FDUULHV :ULWH D SURJUDP WR UHYHUVH WKH OLQHV RI D ILOH DQG DOVR WR UHYHUVH WKH RUGHU RI WKH ZRUGV LQ HDFK OLQH RI WKH ILOH <RX VKRXOG XVH $UUD\/LVWV WR KHOS \RX :ULWH D IDPLO\ GDWDEDVH SURJUDP &UHDWH D FODVV WR UHSUHVHQW D SHUVRQ ZKR VWRUHV UHIHUHQFHV WR KLV KHU PRWKHU DQG IDWKHU DQG D OLVW RI FKLOGUHQ 5HDG D ILOH RI QDPHV WR LQLWLDOL]H WKH SHUVRQV QDPHV DQG SDUHQW FKLOG UHODWLRQVKLSV <RX PLJKW ZLVK WR FUHDWH D ILOH UHSUHVHQWLQJ \RXU RZQ IDPLO\ WUHH 6WRUH WKH RYHUDOO OLVW RI SHUVRQV DV DQ $UUD\/LVW :ULWH DQ RYHUDOO PDLQ XVHU LQWHUIDFH WKDW DVNV IRU D QDPH DQG SULQWV D PDWHUQDO DQG SDWHUQDO IDPLO\ OLQH IRU WKDW SHUVRQ +HUH V D K\SRWKHWLFDO RXWSXW GLDORJXH IURP WKH SURJUDP XVLQJ DQ LQSXW ILOH RI WKH (QJOLVK 7XGRU PRQDUFK OLQH Person's name? Henry VIII Maternal line: Henry VIII Elizabeth of York Paternal line: Henry VIII Henry VII Children: Mary I Elizabeth I Edward VI :ULWH D FODVV WKDW PRGHOV D OLVW RI SRVVLEO\ RYHUODSSLQJ UHFWDQJXODU ' ZLQGRZ UHJLRQV OLNH WKH ZLQGRZV IRU WKH SURJUDPV RI \RXU RSHUDWLQJ V\VWHP 7KH RUGHU RI WKH UHFWDQJOHV LQ WKH OLVW LPSOLHV WKH RUGHU WKH\ G GLVSOD\ RQ VFUHHQ VRPHWLPHV FDOOHG WKH = RUGHU IURP RQ WKH ERWWRP WR VL]H RQ WKH WRS 571 (DFK UHFWDQJOH VWRUHV LWV [ \ SRVLWLRQ ZLGWK DQG KHLJKW <RXU UHFWDQJOH OLVW FODVV VKRXOG KDYH D PHWKRG WKDW WDNHV D 3RLQW DV D SDUDPHWHU DQG WUHDWV LW DV WKRXJK WKH XVHU FOLFNHG WKDW 3RLQW RQ WKH VFUHHQ DQG PRYHV WKH WRSPRVW UHFWDQJOH WRXFKLQJ WKDW SRLQW WR WKH IURQW RI WKH OLVW 6WXDUW 5HJHV 0DUW\ 6WHSS 572 ...
View Full Document

This note was uploaded on 05/18/2008 for the course CSE 142 taught by Professor Martystepp during the Spring '07 term at University of Washington.

Ask a homework question - tutors are online