Chapter 07 - Arrays - &KDSWHU $UUD\V &RS\ULJKW E\...

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\V &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS 0XOWLGLPHQVLRQDO $UUD\V RSWLRQDO 5HFWDQJXODU 7ZR 'LPHQVLRQDO $UUD\V -DJJHG $UUD\V &DVH 6WXG\ +RXUV :RUNHG 7KH WUDQVIHU)URP 0HWKRG 7KH VXP 0HWKRG 7KH DGG7R 0HWKRG 7KH SULQW 0HWKRG 7KH &RPSOHWH 3URJUDP $UUD\ %DVLFV &RQVWUXFWLQJ DQG $FFHVVLQJ DQ $UUD\ $ 8VHIXO $SSOLFDWLRQ RI $UUD\V 5DQGRP $FFHVV $UUD\V DQG 0HWKRGV 7KH )RU (DFK /RRS /LPLWDWLRQV RI $UUD\V $GYDQFHG $UUD\V 6KLIWLQJ 9DOXHV LQ DQ $UUD\ ,QLWLDOL]LQJ $UUD\V $UUD\V LQ WKH -DYD &ODVV /LEUDULHV $UUD\V RI 2EMHFWV &RPPDQG /LQH $UJXPHQWV ,QWURGXFWLRQ 7KH VHTXHQWLDO QDWXUH RI ILOHV VHYHUHO\ OLPLWV WKH QXPEHU RI LQWHUHVWLQJ WKLQJV \RX FDQ HDVLO\ GR ZLWK WKHP 7KH DOJRULWKPV \RX KDYH H[DPLQHG VR IDU KDYH DOO EHHQ VHTXHQWLDO DOJRULWKPV DOJRULWKPV WKDW FDQ EH SHUIRUPHG E\ H[DPLQLQJ HDFK GDWD LWHP RQFH LQ VHTXHQFH 7KHUH LV DQ HQWLUHO\ GLIIHUHQW FODVV RI DOJRULWKPV WKDW FDQ EH SHUIRUPHG ZKHQ \RX FDQ DFFHVV WKH GDWD LWHPV PXOWLSOH WLPHV DQG LQ DQ DUELWUDU\ RUGHU 7KLV FKDSWHU H[DPLQHV D QHZ REMHFW FDOOHG DQ DUUD\ WKDW SURYLGHV WKLV PRUH IOH[LEOH NLQG RI DFFHVV 7KH GLVFXVVLRQ RI DUUD\ DSSOLFDWLRQV EHJLQV ZLWK VRPH VHTXHQWLDO DOJRULWKPV WKDW DUH HQKDQFHG E\ DUUD\V 383 $UUD\ %DVLFV $Q DUUD\ LV D IOH[LEOH VWUXFWXUH IRU VWRULQJ D VHTXHQFH RI YDOXHV DOO RI WKH VDPH W\SH $UUD\ $ VWUXFWXUH WKDW KROGV PXOWLSOH YDOXHV RI WKH VDPH W\SH 7KH YDOXHV VWRUHG LQ DQ DUUD\ DUH FDOOHG HOHPHQWV &RQVWUXFWLQJ DQG $FFHVVLQJ DQ $UUD\ 6XSSRVH WKDW \RX ZDQWHG WR VWRUH WKUHH GLIIHUHQW WHPSHUDWXUH UHDGLQJV <RX FRXOG NHHS WKHP LQ D VHULHV RI GLIIHUHQW YDULDEOHV double temperature1; double temperature2; double temperature3; 7KLV LVQ W D EDG VROXWLRQ LI \RX KDYH MXVW WKUHH WHPSHUDWXUHV EXW VXSSRVH \RX ZDQW WKUHH WKRXVDQG WHPSHUDWXUHV 7KHQ \RX ZRXOG ZDQW VRPHWKLQJ PRUH IOH[LEOH <RX FDQ LQVWHDG VWRUH WKH WHPSHUDWXUHV LQ DQ DUUD\ $V ZH ZLOO VHH DUUD\V DUH REMHFWV ZKLFK PHDQV WKDW WKH\ KDYH WR EH FRQVWUXFWHG %XW EHIRUH ZH FDQ FRQVWUXFW DQ DUUD\ ZH QHHG D YDULDEOH WKDW ZLOO VWRUH D UHIHUHQFH WR RXU DUUD\ REMHFW :KDW W\SH VKRXOG ZH XVH IRU WKDW YDULDEOH" 7KH W\SH \RX XVH ZLOO GHSHQG RQ WKH W\SH RI HOHPHQWV \RX ZDQW WR KDYH LQ \RXU DUUD\ $V ZH ZLOO VHH ZH XVH VTXDUH EUDFNHWV LQ -DYD WR LQGLFDWH WKDW ZH ZDQW DQ DUUD\ :H ZDQW D VHTXHQFH RI YDOXHV RI W\SH GRXEOH VR ZH XVH WKH W\SH GRXEOH>@ 6R ZH FDQ GHFODUH D YDULDEOH IRU VWRULQJ RXU DUUD\ DV IROORZV double temperature; 5HPHPEHU WKDW REMHFWV PXVW EH FRQVWUXFWHG 6LPSO\ GHFODULQJ D YDULDEOH LVQ W HQRXJK WR EULQJ WKH REMHFW LQWR H[LVWHQFH ,Q RXU FDVH ZH ZDQW DQ DUUD\ RI GRXEOH YDOXHV ZKLFK ZH FDQ FRQVWUXFW DV IROORZV double temperature = new double[3]; 7KLV LV D VOLJKWO\ GLIIHUHQW V\QWD[ WKDQ ZKDW ZH KDYH VHHQ EHIRUH ZKHQ ZH DVNHG IRU D QHZ REMHFW ,W LV D VSHFLDO V\QWD[ IRU DUUD\V RQO\ 1RWLFH WKDW RQ WKH OHIW KDQG VLGH ZH GRQ W SXW DQ\WKLQJ LQVLGH WKH VTXDUH EUDFNHWV EHFDXVH ZH DUH GHVFULELQJ D W\SH 7KH YDULDEOH FDOOHG WHPSHUDWXUH FDQ UHIHU WR DQ\ DUUD\ RI GRXEOH YDOXHV QR PDWWHU KRZ PDQ\ HOHPHQWV LW KDV 2Q WKH ULJKW KDQG VLGH ZH KDYH WR PHQWLRQ D VSHFLILF QXPEHU RI HOHPHQWV EHFDXVH ZH DUH DVNLQJ -DYD WR FRQVWUXFW DQ DFWXDO DUUD\ REMHFW IRU XV DQG LW QHHGV WR NQRZ KRZ PDQ\ HOHPHQWV WR LQFOXGH ,Q H[HFXWLQJ WKH OLQH RI FRGH DERYH -DYD ZLOO FRQVWUXFW DQ DUUD\ RI GRXEOH YDOXHV ZLWK WKH YDULDEOH WHPSHUDWXUH UHIHUULQJ WR WKH DUUD\ 7KH HOHPHQWV DUH DOO LQLWLDOL]HG WR 384 [0] [1] [2] +---+ +-------+-------+-------+ temperature | +-+---> | 0.0 | 0.0 | 0.0 | +---+ +-------+-------+-------+ $V WKH SLFWXUH DERYH LQGLFDWHV WKH YDULDEOH WHPSHUDWXUH LV QRW LWVHOI WKH DUUD\ ,QVWHDG LW VWRUHV D UHIHUHQFH WR WKH DUUD\ 7KH LQGLYLGXDO HOHPHQWV DUH LQGH[HG E\ LQWHJHUV 7KLV LV VLPLODU WR WKH ZD\ SRVW RIILFH ER[HV DUH VHW XS 7KH ER[HV DUH LQGH[HG ZLWK QXPEHUV VR WKDW \RX FDQ UHIHU WR DQ LQGLYLGXDO ER[ E\ XVLQJ D GHVFULSWLRQ OLNH 32 ER[ 7KH DUUD\ LQGH[HV DUH LQGLFDWHG LQ WKH SLFWXUH DERYH LQVLGH RI VTXDUH EUDFNHWV 6R WKHUH LV HOHPHQW NQRZQ DV WHPSHUDWXUH> @ DQ HOHPHQW NQRZQ DV WHPSHUDWXUH> @ DQG DQ HOHPHQW NQRZQ DV WHPSHUDWXUH> @ ,W PLJKW VHHP PRUH QDWXUDO WR KDYH LQGH[HV WKDW VWDUW ZLWK RQH LQVWHDG RI ]HUR EXW 6XQ GHFLGHG WKDW -DYD ZRXOG XVH WKH VDPH ]HUR EDVHG LQGH[LQJ VFKHPH WKDW LV XVHG LQ & DQG & 8VLQJ WKH LQGH[HV ZH FDQ LQLWLDOL]H HDFK HOHPHQW RI WKH DUUD\ temperature[0] = 75.2; temperature[1] = 68.4; temperature[2] = 70.3; ZKLFK ZRXOG PRGLI\ WKH DUUD\ WR KDYH WKH IROORZLQJ YDOXHV [0] [1] [2] +---+ +-------+-------+-------+ temperature | +-+---> | 74.3 | 68.4 | 70.3 | +---+ +-------+-------+-------+ 2EYLRXVO\ DQ DUUD\ LVQ W SDUWLFXODUO\ KHOSIXO ZKHQ \RX KDYH MXVW WKUHH YDOXHV WR VWRUH EXW DUUD\V DUH IOH[LEOH LQ WKDW \RX FDQ HDVLO\ UHTXHVW D PXFK ODUJHU DUUD\ )RU H[DPSOH \RX FRXOG UHTXHVW DQ DUUD\ RI RQH KXQGUHG WHPSHUDWXUHV E\ VD\LQJ double temperature = new double[100]; 7KLV LV DOPRVW WKH VDPH OLQH RI FRGH ZH H[HFXWHG EHIRUH 7KH YDULDEOH LV VWLOO GHFODUHG WR EH RI W\SH GRXEOH>@ %XW LQ FRQVWUXFWLQJ WKH DUUD\ ZH UHTXHVW HOHPHQWV LQVWHDG RI ZKLFK FRQVWUXFWV D PXFK ODUJHU DUUD\ [0] [1] [2] [3] [4] [...] [99] +---+ +-------+-------+-------+-------+-------+-------+-------+ temperature | +-+---> | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | ... | 0.0 | +---+ +-------+-------+-------+-------+-------+-------+-------+ 1RWLFH WKDW WKH KLJKHVW LQGH[ LV UDWKHU WKDQ EHFDXVH RI ]HUR EDVHG LQGH[LQJ <RX DUH QRW UHVWULFWHG WR VLPSOH OLWHUDO YDOXHV LQVLGH RI WKH EUDFNHWV <RX FDQ XVH DQ\ LQWHJHU H[SUHVVLRQ 7KLV DOORZV \RX WR FRPELQH DUUD\V ZLWK ORRSV ZKLFK JUHDWO\ VLPSOLILHV WKH FRGH \RX ZULWH )RU H[DPSOH VXSSRVH ZH ZDQW WR UHDG D VHULHV RI WHPSHUDWXUHV IURP D 6FDQQHU :H FRXOG UHDG HDFK YDOXH LQGLYLGXDOO\ DV LQ 385 temperature[0] = input.nextDouble(); temperature[1] = input.nextDouble(); temperature[2] = input.nextDouble(); ... temperature[99] = input.nextDouble(); 7KH RQO\ WKLQJ WKDW FKDQJHV IURP RQH VWDWHPHQW WR WKH QH[W LV WKH LQGH[ :H FDQ FDSWXUH WKLV SDWWHUQ LQ D IRU ORRS ZLWK D FRQWURO YDULDEOH WKDW WDNHV RQ WKH YDOXHV WR for (int i = 0; i < 100; i++) { temperature[i] = input.nextDouble(); } 7KLV LV D YHU\ FRQFLVH ZD\ WR LQLWLDOL]H DOO HOHPHQWV RI WKH DUUD\ 7KH FRGH ZRUNV ZKHQ WKH DUUD\ KDV D OHQJWK RI EXW \RX FDQ LPDJLQH WKH DUUD\ KDYLQJ D GLIIHUHQW OHQJWK -DYD SURYLGHV D XVHIXO PHFKDQLVP IRU PDNLQJ WKLV FRGH PRUH JHQHUDO (DFK DUUD\ NHHSV WUDFN RI LWV RZQ OHQJWK :H DUH XVLQJ WKH YDULDEOH WHPSHUDWXUH WR UHIHU WR RXU DUUD\ ZKLFK PHDQV WKDW ZH FDQ DVN IRU WHPSHUDWXUH OHQJWK WR ILQG RXW WKH OHQJWK RI WKH DUUD\ %\ XVLQJ WHPSHUDWXUH OHQJWK LQ WKH IRU ORRS WHVW LQVWHDG RI WKH VSHFLILF YDOXH ZH PDNH RXU FRGH PRUH JHQHUDO for (int i = 0; i < temperature.length; i++) { temperature[i] = input.nextDouble(); } 7KLV FRGH SURYLGHV D WHPSODWH WKDW \RX ZLOO VHH RIWHQ ZLWK DUUD\ SURFHVVLQJ FRGH D IRU ORRS WKDW VWDUWV DW DQG WKDW FRQWLQXHV ZKLOH WKH ORRS YDULDEOH LV OHVV WKDQ WKH OHQJWK RI WKH DUUD\ GRLQJ VRPHWKLQJ ZLWK HOHPHQW >L@ LQ WKH ERG\ RI WKH ORRS ,W LV SRVVLEOH WR UHIHU WR DQ LOOHJDO LQGH[ RI DQ DUUD\ LQ ZKLFK FDVH -DYD WKURZV DQ H[FHSWLRQ )RU H[DPSOH LI ZH ZHUH WR DVN IRU WHPSHUDWXUH> @ RU WHPSHUDWXUH> @ ZH ZRXOG EH DVNLQJ IRU DQ DUUD\ HOHPHQW WKDW GRHV QRW H[LVW ,I \RXU FRGH PDNHV VXFK DQ LOOHJDO UHIHUHQFH -DYD ZLOO KDOW \RXU SURJUDP ZLWK DQ $UUD\,QGH[2XW2I%RXQGV([FHSWLRQ $ 8VHIXO $SSOLFDWLRQ RI $UUD\V /HW V ORRN DW D SURJUDP ZKHUH DQ DUUD\ DOORZV XV WR VROYH D SUREOHP ZH FRXOGQ W VROYH EHIRUH ,I \RX WXQH LQ WR DQ\ ORFDO QHZV EURDGFDVW DW QLJKW \RX OO KHDU WKHP UHSRUW WKH KLJK WHPSHUDWXUH IRU WKDW GD\ ,W LV XVXDOO\ UHSRUWHG DV DQ LQWHJHU DV LQ ,W JRW XS WR WRGD\ 6XSSRVH WKDW ZH ZDQW WR H[DPLQH D VHULHV RI KLJK WHPSHUDWXUHV DQG FRPSXWH WKH DYHUDJH WHPSHUDWXUH DQG FRXQW KRZ PDQ\ GD\V ZHUH DERYH DYHUDJH LQ WHPSHUDWXUH :H KDYH EHHQ XVLQJ D 6FDQQHU WR VROYH SUREOHPV OLNH WKLV DQG ZH FDQ DOPRVW VROYH WKH SUREOHP WKDW ZD\ ,I ZH MXVW ZDQWHG WR NQRZ WKH DYHUDJH ZH FRXOG XVH D 6FDQQHU DQG ZULWH D FXPXODWLYH VXP ORRS WR ILQG WKH DYHUDJH 386 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // Reads a series of high temperatures and reports the average. import java.util.*; public class Temperature1 { public static void main(String args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int numDays = console.nextInt(); int sum = 0; for (int i = 1; i <= numDays; i++) { System.out.print("Day " + i + "'s high temp: "); int next = console.nextInt(); sum += next; } double average = (double) sum / numDays; System.out.println(); System.out.println("Average = " + average); } } 7KLV SURJUDP GRHV D SUHWW\ JRRG MRE +HUH LV D VDPSOH H[HFXWLRQ How Day Day Day Day Day many days' temperatures? 5 1's high temp: 78 2's high temp: 81 3's high temp: 75 4's high temp: 79 5's high temp: 71 Average = 76.8 %XW KRZ GR ZH FRXQW KRZ PDQ\ GD\V ZHUH DERYH DYHUDJH" :H FRXOG WU\ WR LQFRUSRUDWH LW LQWR RXU ORRS EXW WKDW ZRQ W ZRUN 7KH SUREOHP LV WKDW ZH FDQ W ILJXUH RXW WKH DYHUDJH XQWLO ZH KDYH JRQH WKURXJK DOO RI WKH GDWD 7KDW PHDQV WKDW ZH QHHG WR PDNH D VHFRQG SDVV WKURXJK WKH GDWD WR ILJXUH RXW KRZ PDQ\ GD\V ZHUH DERYH DYHUDJH :H FDQ W GR WKDW ZLWK D 6FDQQHU $ 6FDQQHU KDV QR UHVHW RSWLRQ WKDW DOORZV XV WR VHH WKH GDWD D VHFRQG WLPH )RUWXQDWHO\ ZH FDQ VROYH WKH SUREOHP ZLWK DQ DUUD\ $V ZH UHDG QXPEHUV LQ DQG FRPSXWH WKH FXPXODWLYH VXP ZH FDQ ILOO XS DQ DUUD\ WKDW VWRUHV WKH WHPSHUDWXUHV 7KHQ ZH FDQ XVH WKH DUUD\ WR PDNH RXU VHFRQG SDVV WKURXJK WKH GDWD ,Q WKH ODVW VHFWLRQ ZH ZHUH XVLQJ DQ DUUD\ RI GRXEOH YDOXHV EXW KHUH ZH ZDQW DQ DUUD\ RI LQW YDOXHV 6R LQVWHDG RI GHFODULQJ D YDULDEOH RI W\SH GRXEOH>@ ZH GHFODUH D YDULDEOH RI W\SH LQW>@ :H DUH DVNLQJ WKH XVHU KRZ PDQ\ GD\V RI WHPSHUDWXUH GDWD WR LQFOXGH VR ZH FDQ FRQVWUXFW WKH DUUD\ ULJKW DIWHU ZH KDYH UHDG WKDW LQIRUPDWLRQ int numDays = console.nextInt(); int temps = new int[numDays]; 7KH ROG ORRS ORRNV OLNH WKLV 387 for (int i = 1; i <= numDays; i++) { System.out.print("Day " + i + "'s high temp: "); int next = console.nextInt(); sum += next; } %HFDXVH ZH DUH XVLQJ DQ DUUD\ ZH ZLOO FKDQJH WKLV WR D ORRS WKDW VWDUWV DW WR PDWFK WKH DUUD\ LQGH[LQJ %XW MXVW EHFDXVH ZH DUH XVLQJ ]HUR EDVHG LQGH[LQJ LQVLGH WKH SURJUDP WKDW GRHVQ W PHDQ WKDW ZH KDYH WR FRQIXVH WKH XVHU E\ DVNLQJ IRU 'D\ V KLJK WHPS 6R ZH FDQ PRGLI\ WKH SULQWOQ WR SURPSW IRU GD\ L :H QR ORQJHU QHHG WKH YDULDEOH QH[W EHFDXVH ZH ZLOO EH VWRULQJ WKH YDOXHV LQ WKH DUUD\ LQVWHDG 6R WKH ORRS FRGH EHFRPHV for (int i = 0; i < numDays; i++) { System.out.print("Day " + (i + 1) + "'s high temp: "); temps[i] = console.nextInt(); sum += temps[i]; } 1RWLFH WKDW ZH QRZ WHVW ZKHWKHU WKH LQGH[ LV VWULFWO\ OHVV WKDQ QXP'D\V $IWHU WKLV ORRS H[HFXWHV ZH FRPSXWH WKH DYHUDJH MXVW DV ZH GLG EHIRUH 7KHQ ZH FDQ ZULWH D QHZ ORRS WKDW FRXQWV KRZ PDQ\ GD\V ZHUH DERYH DYHUDJH int above = 0; for (int i = 0; i < temps.length; i++) { if (temps[i] > average) { above++; } } 1RWLFH WKDW LQ WKLV ORRS WKH WHVW LQYROYHV WHPSV OHQJWK :H FRXOG LQVWHDG KDYH WHVWHG ZKHWKHU WKH YDULDEOH LV OHVV WKDQ QXP'D\V (LWKHU FKRLFH ZRUNV LQ WKLV SURJUDP EHFDXVH WKH\ VKRXOG EH HTXDO WR HDFK RWKHU ,I ZH SXW WKHVH YDULRXV FRGH IUDJPHQWV WRJHWKHU DQG LQFOXGH FRGH WR UHSRUW WKH QXPEHU RI GD\V DERYH DYHUDJH ZH JHW WKH IROORZLQJ FRPSOHWH SURJUDP 388 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 // Reads a series of high temperatures and reports the average // and the number of days above average. import java.util.*; public class Temperature2 { public static void main(String args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int numDays = console.nextInt(); int temps = new int[numDays]; int sum = 0; for (int i = 0; i < numDays; i++) { System.out.print("Day " + (i + 1) + "'s high temp: "); temps[i] = console.nextInt(); sum += temps[i]; } double average = (double) sum / numDays; int above = 0; for (int i = 0; i < temps.length; i++) { if (temps[i] > average) { above++; } } System.out.println(); System.out.println("Average = " + average); System.out.println(above + " days above average"); } } %HORZ LV D VDPSOH H[HFXWLRQ How Day Day Day Day Day Day Day Day Day many days' temperatures? 9 1's high temp: 75 2's high temp: 78 3's high temp: 85 4's high temp: 71 5's high temp: 69 6's high temp: 82 7's high temp: 74 8's high temp: 80 9's high temp: 87 Average = 77.88888888888889 5 days above average &RPPRQ 3URJUDPPLQJ (UURU 2II %\ 2QH %XJ ,Q FRQYHUWLQJ WKH ILUVW YHUVLRQ RI WKH 7HPSHUDWXUH SURJUDP WR RQH WKDW XVHV DQ DUUD\ ZH PRGLILHG RXU IRU ORRS WR VWDUW ZLWK DQ LQGH[ RI LQVWHDG RI 7KH RULJLQDO IRU ORRS ZDV ZULWWHQ WKLV ZD\ for (int i = 1; i <= numDays; i++) { System.out.print("Day " + i + "'s high temp: "); int next = console.nextInt(); sum += next; } 389 :H GHFLGHG WKDW ZH ZRXOG UHDG WKH YDOXHV LQWR WKH DUUD\ UDWKHU WKDQ UHDGLQJ WKHP LQWR D YDULDEOH FDOOHG QH[W ZKLFK PHDQW WKDW ZH UHSODFHG QH[W ZLWK WHPSV>L@ // wrong loop bounds for (int i = 1; i <= numDays; i++) { System.out.print("Day " + i + "'s high temp: "); temp[i] = console.nextInt(); sum += temp[i]; } %HFDXVH WKH DUUD\ LV LQGH[HG VWDUWLQJ DW ZH FKDQJHG WKH ERXQGV RI WKH IRU ORRS WR VWDUW DW 6XSSRVH WKDW ZDV WKH RQO\ FKDQJH ZH PDGH FKDQJLQJ WR // still wrong loop bounds for (int i = 0; i <= numDays; i++) { System.out.print("Day " + (i + 1) + "'s high temp: "); temps[i] = console.nextInt(); sum += temps[i]; } 7KLV ORRS JHQHUDWHV DQ HUURU ZH UXQ LW ,W DVNV IRU DQ H[WUD GD\ V ZRUWK RI GDWD DQG WKHQ WKURZV DQ H[FHSWLRQ DV LQ WKH IROORZLQJ VDPSOH H[HFXWLRQ How many days' temperatures? 5 Day 1's high temp: 82 Day 2's high temp: 80 Day 3's high temp: 79 Day 4's high temp: 71 Day 5's high temp: 75 Day 6's high temp: 83 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Temperature2.main(test.java:15) 7KH SUREOHP LV WKDW LI ZH DUH JRLQJ WR VWDUW WKH IRU ORRS YDULDEOH DW WKHQ ZH ZDQW WR WHVW IRU LW EHLQJ VWULFWO\ OHVV WKDQ WKH QXPEHU RI LWHUDWLRQV ZH ZDQW :H FKDQJHG WKH WR D EXW OHIW WKH WHVW $V D UHVXOW WKH ORRS LV SHUIRUPLQJ DQ H[WUD LWHUDWLRQ DQG WU\LQJ WR PDNH D UHIHUHQFH WR DQ DUUD\ HOHPHQW WHPSV> @ WKDW GRHVQ W H[LVW 7KLV LV D FODVVLF RII E\ RQH HUURU 7KH IL[ LV WR FKDQJH WKH ORRS ERXQGV WR XVH D VWULFWO\ OHVV WKDQ WHVW // correct bounds for (int i = 0; i < numDays; i++) { System.out.print("Day " + (i + 1) + "'s high temp: "); temps[i] = console.nextInt(); sum += temps[i]; } 5DQGRP $FFHVV 0RVW RI WKH DOJRULWKPV ZH KDYH VHHQ KDYH LQYROYHG VHTXHQWLDO DFFHVV 6HTXHQWLDO $FFHVV 0DQLSXODWLQJ YDOXHV LQ D VHTXHQWLDO PDQQHU IURP ILUVW WR ODVW 390 $ 6FDQQHU REMHFW LV RIWHQ DOO \RX QHHG IRU D VHTXHQWLDO DOJRULWKP EHFDXVH LW DOORZV \RX WR DFFHVV GDWD LQ D IRUZDUGV PDQQHU IURP ILUVW WR ODVW %XW DV ZH KDYH VHHQ WKHUH LV QR ZD\ WR UHVHW D 6FDQQHU EDFN WR WKH EHJLQQLQJ :H MXVW H[DPLQHG D VDPSOH SURJUDP WKDW XVHV DQ DUUD\ WR DOORZ D VHFRQG SDVV WKURXJK WKH GDWD EXW HYHQ WKLV LV IXQGDPHQWDOO\ D VHTXHQWLDO DSSURDFK EHFDXVH LW LQYROYHV WZR SDVVHV WKURXJK WKH GDWD $Q DUUD\ LV D SRZHUIXO GDWD VWUXFWXUH WKDW DOORZV D PRUH VRSKLVWLFDWHG NLQG RI DFFHVV NQRZQ DV UDQGRP DFFHVV 5DQGRP $FFHVV 0DQLSXODWLQJ YDOXHV LQ DQ\ RUGHU ZKDWVRHYHU ZLWK TXLFN DFFHVV WR HDFK YDOXH $UUD\V FDQ SURYLGH UDQGRP DFFHVV EHFDXVH WKH\ DUH DOZD\V DOORFDWHG DV D FRQWLJXRXV EORFN RI PHPRU\ 7KDW PHDQV WKDW WKH FRPSXWHU FDQ TXLFNO\ FRPSXWH H[DFWO\ ZKHUH D SDUWLFXODU YDOXH ZLOO EH VWRUHG EHFDXVH LW NQRZV KRZ PXFK VSDFH HDFK HOHPHQW WDNHV XS LQ PHPRU\ DQG LW NQRZV WKDW WKH\ DUH DOO DOORFDWHG ULJKW QH[W WR HDFK RWKHU LQ WKH DUUD\ /HW V H[SORUH D SUREOHP ZKHUH UDQGRP DFFHVV LV LPSRUWDQW 6XSSRVH WKDW D WHDFKHU JLYHV TXL]HV WKDW DUH VFRUHG RQ D VFDOH RI WR DQG WKH WHDFKHU ZDQWV WR NQRZ WKH GLVWULEXWLRQ RI TXL] VFRUHV ,Q RWKHU ZRUGV WKH WHDFKHU ZDQWV WR NQRZ KRZ PDQ\ VFRUHV RI WKHUH DUH DQG KRZ PDQ\ VFRUHV RI KRZ PDQ\ VFRUHV RI KRZ PDQ\ VFRUHV RI DQG KRZ PDQ\ VFRUHV RI 6XSSRVH WKDW WKH WHDFKHU KDV LQFOXGHG DOO RI WKH VFRUHV LQ D GDWD ILOH OLNH WKH IROORZLQJ 2 8 0 7 4 12 0 2 1 8 7 1 7 4 4 6 79 5 8 5 1 2 7 4 3 2 4 2 7 0 4 5 4 10 2 0 275 93 1 5 4 5 6 8 1 2 6 -999 9 5 7 0 64 3 4 2 3 10 2 4 9 0 5 3 -36 9 1 0 6 2 2 1 6 6 1 3 8 6 7KH WHDFKHU FRXOG KDQG FRXQW WKH VFRUHV EXW LW VHHPV PXFK HDVLHU WR XVH D FRPSXWHU WR GR WKH FRXQWLQJ +RZ GR ZH VROYH WKH SUREOHP" )LUVW \RX KDYH WR UHFRJQL]H WKDW \RX DUH GRLQJ ILYH VHSDUDWH FRXQWLQJ WDVNV <RX DUH FRXQWLQJ WKH RFFXUUHQFHV RI WKH QXPEHU WKH QXPEHU WKH QXPEHU WKH QXPEHU DQG WKH QXPEHU :H ZLOO QHHG ILYH FRXQWHUV WR VROYH WKLV SUREOHP ZKLFK PHDQV WKDW DQ DUUD\ LV D JUHDW ZD\ WR VWRUH WKH GDWD ,Q JHQHUDO ZKHQHYHU \RX ILQG \RXUVHOI WKLQNLQJ WKDW \RX QHHG Q RI VRPH NLQG RI GDWD \RX VKRXOG WKLQN DERXW XVLQJ DQ DUUD\ RI OHQJWK Q (DFK FRXQWHU ZLOO EH DQ LQW VR ZH ZDQW DQ DUUD\ RI ILYH LQW YDOXHV int count = new int[5]; 7KLV ZLOO DOORFDWH WKH DUUD\ RI ILYH LQWV DQG ZLOO LQLWLDOL]H HDFK WR [0] [1] [2] [3] [4] +---+ +-------+-------+-------+-------+-------+ count | +-+---> | 0 | 0 | 0 | 0 | 0 | +---+ +-------+-------+-------+-------+-------+ :H DUH UHDGLQJ IURP D ILOH VR ZH ZLOO QHHG D 6FDQQHU DQG D ORRS WKDW UHDGV VFRUHV XQWLO WKHUH DUH QR PRUH VFRUHV WR UHDG 391 Scanner input = new Scanner(new File("tally.dat")); while (input.hasNextInt()) { int next = input.nextInt(); // process next } 7R FRPSOHWH WKLV FRGH ZH QHHG WR ILJXUH RXW KRZ WR SURFHVV HDFK YDOXH :H NQRZ WKDW QH[W ZLOO EH RQH RI GLIIHUHQW YDOXHV RU ,I LW LV ZH ZDQW WR LQFUHPHQW RXU FRXQWHU IRU ZKLFK LV FRXQW> @ ,I LW LV ZH ZDQW WR LQFUHPHQW RXU FRXQW IRU ZKLFK LV FRXQW> @ $QG VR RQ :H KDYH EHHQ VROYLQJ SUREOHPV OLNH WKLV RQH ZLWK QHVWHG LI HOVH VWDWHPHQWV if (next == 0) { count[0]++; } else if (next == 1) { count[1]++; } else if (next == 2) { count[2]++; } else if (next == 3) { count[3]++; } else { // next == 4 count[4]++; } :LWK DQ DUUD\ ZH FDQ VROYH WKLV PXFK PRUH GLUHFWO\ count[next]++; 7KLV OLQH RI FRGH LV VR VKRUW FRPSDUHG WR WKH QHVWHG LI HOVH WKDW \RX PLJKW QRW UHDOL]H DW ILUVW WKDW LW GRHV WKH VDPH WKLQJ /HW V VLPXODWH H[DFWO\ ZKDW KDSSHQV DV ZH UHDG YDULRXV YDOXHV IURP WKH ILOH :KHQ WKH DUUD\ LV FRQVWUXFWHG DOO RI WKH FRXQWV DUH LQLWLDOL]HG WR [0] [1] [2] [3] [4] +---+ +-------+-------+-------+-------+-------+ count | +-+---> | 0 | 0 | 0 | 0 | 0 | +---+ +-------+-------+-------+-------+-------+ 7KH ILUVW YDOXH LQ WKH LQSXW ILOH LV D count[next]++; VR ZH UHDG WKDW LQWR QH[W 7KHQ ZH H[HFXWH WKLV OLQH RI FRGH %HFDXVH QH[W LV count[1]++; WKLV EHFRPHV 6R ZH LQFUHPHQW WKH FRXQWHU DW LQGH[ > @ [0] [1] [2] [3] [4] +---+ +-------+-------+-------+-------+-------+ count | +-+---> | 0 | 1 | 0 | 0 | 0 | +---+ +-------+-------+-------+-------+-------+ 7KHQ ZH UHDG D IURP WKH LQSXW ILOH ZKLFK PHDQV ZH LQFUHPHQW FRXQW> @ 392 [0] [1] [2] [3] [4] +---+ +-------+-------+-------+-------+-------+ count | +-+---> | 0 | 1 | 1 | 0 | 0 | +---+ +-------+-------+-------+-------+-------+ 7KHQ ZH UHDG D IURP WKH LQSXW ILOH ZKLFK LQFUHPHQWV FRXQW> @ [0] [1] [2] [3] [4] +---+ +-------+-------+-------+-------+-------+ count | +-+---> | 1 | 1 | 1 | 0 | 0 | +---+ +-------+-------+-------+-------+-------+ 7KHQ ZH UHDG DQRWKHU IURP WKH LQSXW ILOH ZKLFK LQFUHPHQWV FRXQW> @ [0] [1] [2] [3] [4] +---+ +-------+-------+-------+-------+-------+ count | +-+---> | 1 | 2 | 1 | 0 | 0 | +---+ +-------+-------+-------+-------+-------+ 7KHQ ZH UHDG D IURP WKH LQSXW ILOH ZKLFK LQFUHPHQWV FRXQW> @ [0] [1] [2] [3] [4] +---+ +-------+-------+-------+-------+-------+ count | +-+---> | 1 | 2 | 1 | 1 | 0 | +---+ +-------+-------+-------+-------+-------+ 7KHQ ZH UHDG DQRWKHU IURP WKH LQSXW ILOH ZKLFK LQFUHPHQWV FRXQW> @ [0] [1] [2] [3] [4] +---+ +-------+-------+-------+-------+-------+ count | +-+---> | 1 | 2 | 2 | 1 | 0 | +---+ +-------+-------+-------+-------+-------+ 7KHQ ZH UHDG DQRWKHU IURP WKH LQSXW ILOH ZKLFK LQFUHPHQWV FRXQW> @ [0] [1] [2] [3] [4] +---+ +-------+-------+-------+-------+-------+ count | +-+---> | 1 | 3 | 2 | 1 | 0 | +---+ +-------+-------+-------+-------+-------+ 7KH SURJUDP FRQWLQXHV H[HFXWLQJ LQ WKLV PDQQHU LQFUHPHQWLQJ WKH DSSURSULDWH FRXQWHU IRU HDFK VFRUH LW UHDGV 1RWLFH WKDW WKH VFRUHV FDQ DSSHDU LQ DQ\ RUGHU ZKDWVRHYHU :H FDQ EH LQFUHPHQWLQJ WKH ILUVW YDOXH LQ WKH DUUD\ RQH PRPHQW DQG LQFUHPHQWLQJ WKH ODVW YDOXH D PRPHQW ODWHU 7KHQ ZH FDQ MXPS WR WKH PLGGOH DQG EDFN WR WKH IURQW DQG EDFN WR WKH HQG DQG EDFN WR WKH PLGGOH $Q\ RUGHU ZKDWVRHYHU ZLOO ZRUN 7KLV DELOLW\ WR MXPS DURXQG LQ WKH GDWD VWUXFWXUH LV ZKDW ZH PHDQ E\ UDQGRP DFFHVV $IWHU WKLV ORRS ILQLVKHV H[HFXWLQJ ZH FDQ UHSRUW WKH WRWDO IRU HDFK VFRUH for (int i = 0; i < count.length; i++) { System.out.println(i + "\t" + count[i]); } :H FDQ SXW WKLV DOO WRJHWKHU DQG DGG D KHDGHU IRU WKH RXWSXW WR JHW WKH IROORZLQJ FRPSOHWH SURJUDP 393 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // Reads a series of values and reports the frequency of occurrence // of each value. import java.io.*; import java.util.*; public class Tally { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("tally.dat")); int count = new int[5]; while (input.hasNextInt()) { int next = input.nextInt(); count[next]++; } System.out.println("Value\tOccurrences"); for (int i = 0; i < count.length; i++) { System.out.println(i + "\t" + count[i]); } } } *LYHQ RXU VDPSOH LQSXW ILOH LW SURGXFHV WKH IROORZLQJ RXWSXW Value 0 1 2 3 4 Occurrences 5 9 7 9 10 ,W LV LPSRUWDQW WR UHDOL]H WKDW WKH SURJUDP ZULWWHQ ZLWK DQ DUUD\ LV PXFK PRUH IOH[LEOH WKDQ WKH NLQG RI SURJUDPV ZH KDYH ZULWWHQ ZLWK VLPSOH YDULDEOHV DQG LI HOVH VWDWHPHQWV )RU H[DPSOH VXSSRVH WKDW ZH ZDQW WR DGDSW WKLV SURJUDP WR SURFHVV DQ LQSXW ILOH ZLWK H[DP VFRUHV WKDW UDQJH IURP WR 7KH RQO\ FKDQJH ZH ZRXOG ZRXOG KDYH WR PDNH ZRXOG EH WR DOORFDWH D ODUJHU DUUD\ int count = new int[101]; ,I ZH KDG ZULWWHQ WKH SURJUDP ZLWK DQ LI HOVH DSSURDFK ZH ZRXOG KDYH WR DGG QHZ EUDQFKHV WR DFFRXQW IRU WKH QHZ UDQJH RI YDOXHV :LWK WKH DUUD\ VROXWLRQ ZH MXVW KDYH WR PRGLI\ WKH RYHUDOO VL]H RI WKH DUUD\ 1RWLFH WKDW WKH DUUD\ VL]H LV RQH PRUH WKDQ WKH KLJKHVW VFRUH UDWKHU WKDQ EHFDXVH WKH DUUD\ LV ]HUR EDVHG DQG EHFDXVH WKHUH DFWXDOO\ DUH GLIIHUHQW VFRUHV WKDW \RX FDQ JHW RQ WKH WHVW ZKHQ LV D SRVVLELOLW\ $UUD\V DQG 0HWKRGV $UUD\V FDQ EH SDVVHG DV SDUDPHWHUV WR PHWKRGV DQG FDQ EH UHWXUQHG E\ PHWKRGV <RX KDYH WR NHHS LQ PLQG KRZ WR GHVFULEH WKH DUUD\ W\SH :H KDYH VHHQ DQ DUUD\ RI GRXEOH YDOXHV WKDW ZH GHFODUHG WR EH RI W\SH GRXEOH>@ DQG DQ DUUD\ RI LQW YDOXHV WKDW ZH GHFODUHG WR EH RI W\SH LQW>@ ,Q JHQHUDO IRU DQ\ W\SH LQ -DYD \RX FDQ SXW VTXDUH EUDFNHWV DIWHU WKH W\SH WR LQGLFDWH DQ DUUD\ RI WKDW W\SH $Q DUUD\ RI FKDU YDOXHV LV RI W\SH FKDU>@ DQ DUUD\ RI ERROHDQ LV RI W\SH ERROHDQ>@ DQ DUUD\ RI 6WULQJ YDOXHV LV RI W\SH 6WULQJ>@ DQ DUUD\ RI 3RLQW REMHFWV LV RI W\SH 3RLQW>@ DQG VR RQ 394 :H FDQ H[SORUH WKH XVH RI DUUD\V DV SDUDPHWHUV E\ UHZULWLQJ WKH 7DOO\ SURJUDP WR KDYH PHWKRGV 7KH SURJUDP EHJLQV E\ FRQVWUXFWLQJ D 6FDQQHU DQG DQ DUUD\ DQG WKHQ LW KDV WZR ORRSV RQH WR UHDG WKH LQSXW ILOH DQG RQH WR UHSRUW WKH UHVXOWV :H FDQ SXW HDFK ORRS LQ LWV RZQ PHWKRG 7KH ILUVW UHDGV IURP WKH 6FDQQHU DQG VWRUHV LWV UHVXOW LQ WKH DUUD\ VR LW ZLOO QHHG ERWK REMHFWV DV SDUDPHWHUV 7KH VHFRQG UHSRUWV WKH YDOXHV LQ WKH DUUD\ VR LW QHHGV MXVW WKH DUUD\ DV D SDUDPHWHU 7KXV WKH PDLQ PHWKRG ZRXOG EH UHZULWWHQ DV IROORZV public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("tally.dat")); int count = new int[5]; readData(input, count); reportResults(count); } 7R ZULWH WKH UHDG'DWD PHWKRG ZH MXVW QHHG WR PRYH WKH ILOH SURFHVVLQJ ORRS LQWR WKH PHWKRG DQG SURYLGH DQ DSSURSULDWH KHDGHU )RU WKH DUUD\ SDUDPHWHU ZH OLVW WKH W\SH DV LQW>@ public static void readData(Scanner input, int count) { while (input.hasNextInt()) { int next = input.nextInt(); count[next]++; } } 0DQ\ QRYLFHV WKLQN WKDW WKLV PHWKRG QHHGV WR UHWXUQ WKH DUUD\ EHFDXVH LW LV FKDQJLQJ WKH YDOXHV VWRUHG LQ WKH DUUD\ 7KDW V QRW WUXH EHFDXVH DUUD\V DUH REMHFWV :H KDYH VHHQ WKDW PHWKRGV FDQ FKDQJH WKH VWDWH RI D 6FDQQHU REMHFW IRU H[DPSOH VLPSO\ E\ KDYLQJ WKH 6FDQQHU SDVVHG DV D SDUDPHWHU 7KH UHDG'DWD PHWKRG ZLOO EH SDVVHG D UHIHUHQFH WR WKH DUUD\ DQG WKDW LV DOO LW QHHGV WR FKDQJH WKH YDOXHV VWRUHG LQVLGH WKH DUUD\ 7KH RQO\ WLPH ZH ZRXOG QHHG WR UHWXUQ WKH DUUD\ ZRXOG EH LI WKH PHWKRG FRQVWUXFWV WKH DUUD\ ,Q WKDW FDVH RQO\ WKH PHWKRG ZLOO KDYH D UHIHUHQFH WR WKH QHZO\ FRQVWUXFWHG DUUD\ XQOHVV LW UHWXUQV LW :H FDQ UHZULWH WKH UHDG'DWD PHWKRG WR GR H[DFWO\ WKLV ,Q LWV FXUUHQW IRUP LW DVVXPHV WKDW WKH DUUD\ KDV DOUHDG\ EHHQ FRQVWUXFWHG ZKLFK LV ZK\ ZH ZURWH WKHVH WZR OLQHV RI FRGH LQ PDLQ int count = new int[5]; readData(input, count); :H FRXOG LQVWHDG GHFLGH WKDW WKH PHWKRG ZLOO FRQVWUXFW WKH DUUD\ ,Q WKDW FDVH LW GRHVQ W KDYH WR EH SDVVHG DV D SDUDPHWHU EXW LW ZLOO KDYH WR EH UHWXUQHG E\ WKH PHWKRG :H ZRXOG UHZULWH WKHVH WZR OLQHV RI FRGH IURP PDLQ DV D VLQJOH OLQH int count = readData(input); $QG ZH ZRXOG UHZULWH WKH PHWKRG VR WKDW LW FRQVWUXFWV DQG UHWXUQV WKH DUUD\ 395 public static int readData(Scanner input) { int count = new int[5]; while (input.hasNextInt()) { int next = input.nextInt(); count[next]++; } return count; } 3D\ FORVH DWWHQWLRQ WR WKH KHDGHU RI WKLV PHWKRG ,W QR ORQJHU KDV WKH DUUD\ DV D SDUDPHWHU DQG LWV UHWXUQ W\SH LV LQW>@ UDWKHU WKDQ YRLG ,W DOVR HQGV ZLWK D UHWXUQ VWDWHPHQW WKDW UHWXUQV D UHIHUHQFH WR WKH DUUD\ WKDW LW FRQVWUXFWV ,I ZH FRPELQH WKLV QHZ YHUVLRQ RI WKH PHWKRG ZLWK DQ LPSOHPHQWDWLRQ RI WKH UHSRUW5HVXOWV PHWKRG ZH HQG XS ZLWK WKH IROORZLQJ FRPSOHWH 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 // Variation of Tally program with methods. import java.io.*; import java.util.*; public class Tally2 { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("tally.dat")); int count = readData(input); reportResults(count); } public static int readData(Scanner input) { int count = new int[5]; while (input.hasNextInt()) { int next = input.nextInt(); count[next]++; } return count; } public static void reportResults(int count) { System.out.println("Value\tOccurrences"); for (int i = 0; i < count.length; i++) { System.out.println(i + "\t" + count[i]); } } } 7KH )RU (DFK /RRS -DYD LQWURGXFHG D QHZ ORRS FRQVWUXFW WKDW VLPSOLILHV FHUWDLQ DUUD\ ORRSV ,W LV NQRZQ DV WKH HQKDQFHG IRU ORRS RU WKH IRU HDFK ORRS ,W FDQ EH XVHG ZKHQHYHU \RX ILQG \RXUVHOI ZDQWLQJ WR H[DPLQH HDFK YDOXH LQ WKH DUUD\ Q )RU H[DPSOH LQ WKH SURJUDP 7HPSHUDWXUH ZH KDG DQ DUUD\ YDULDEOH FDOOHG WHPSV DQG ZH ZURWH WKH IROORZLQJ ORRS 396 for (int i = 0; i < temps.length; i++) { if (temps[i] > average) { above++; } } :H FDQ UHZULWH WKLV DV D IRU HDFK ORRS for (int n : temps) { if (n > average) { above++; } } 7KLV ORRS LV QRUPDOO\ UHDG DV DSSHDUV EHORZ )RU HDFK LQW Q LQ WHPSV 7KH EDVLF V\QWD[ RI WKH IRU HDFK ORRS for (<type> <name> : <array>) { <statement>; <statement>; ... <statement>; } 7KHUH LV QRWKLQJ VSHFLDO DERXW WKH YDULDEOH QDPH DV ORQJ DV \RX DUH FRQVLVWHQW LQ WKH ERG\ RI WKH ORRS )RU H[DPSOH WKH SUHYLRXV ORRS DERYH FRXOG DOVR EH ZULWWHQ ZLWK WKH YDULDEOH [ LQVWHDG RI WKH YDULDEOH Q for (int x : temps) { if (x > average) { above++; } } 7KH IRU HDFK ORRS LV PRVW XVHIXO ZKHQ \RX VLPSO\ ZDQW WR H[DPLQH HDFK YDOXH LQ VHTXHQFH 7KHUH DUH PDQ\ VLWXDWLRQV ZKHUH WKH IRU HDFK ORRS LV QRW DSSURSULDWH )RU H[DPSOH WKH IROORZLQJ ORRS ZRXOG GRXEOH HYHU\ YDOXH LQ DQ DUUD\ FDOOHG OLVW for (int i = 0; i < list.length; i++) { list[i] *= 2; } <RX PLJKW LPDJLQH WKDW \RX FRXOG UHSODFH WKLV ZLWK D IRU HDFK ORRS for (int n : list) { n *= 2; // changes only n, not the array } 8QIRUWXQDWHO\ \RX FDQ QRW PRGLI\ WKH DUUD\ HOHPHQWV ZLWK D IRU HDFK ORRS $V WKH FRPPHQW LQGLFDWHV WKH ORRS DERYH GRXEOHV WKH YDULDEOH Q ZLWKRXW FKDQJLQJ WKH DUUD\ HOHPHQWV ,Q VRPH FDVHV WKH IRU HDFK ORRS LV QRW WKH PRVW FRQYHQLHQW HYHQ ZKHQ WKH FRGH LQYROYHV H[DPLQLQJ HDFK DUUD\ HOHPHQW LQ VHTXHQFH &RQVLGHU IRU H[DPSOH WKLV ORRS IURP WKH 7DOO\ SURJUDP 397 for (int i = 0; i < count.length; i++) { System.out.println(i + "\t" + count[i]); } $ IRU HDFK ORRS FDQ EH XVHG WR UHSODFH WKH DUUD\ DFFHVV DV LQ for (int n : count) { System.out.println(i + "\t" + n); } // illegal 7KH SUREOHP DERYH LV WKDW ZH ZDQW WR SULQW WKH YDOXH RI L EXW ZH HOLPLQDWHG L ZKHQ ZH FRQYHUWHG WKLV WR D IRU HDFK ORRS :H ZRXOG KDYH WR DGG H[WUD FRGH WR NHHS WUDFN RI WKH YDOXH RI L DV LQ int i = 0; for (int n : count) { System.out.println(i + "\t" + n); i++; } 7KLV FRGH LV OHJDO EXW \RX KDYH WR ZRQGHU ZKHWKHU WKH IRU HDFK ORRS LV UHDOO\ VLPSOLI\LQJ WKLQJV ,Q WKLV FDVH WKH RULJLQDO YHUVLRQ LV SUREDEO\ PRUH FOHDU /LPLWDWLRQV RI $UUD\V <RX VKRXOG EH DZDUH RI VRPH JHQHUDO OLPLWDWLRQV RI DUUD\V <RX FDQ W FKDQJH WKH VL]H RI DQ DUUD\ LQ WKH PLGGOH RI SURJUDP H[HFXWLRQ 7R PDNH DQ DUUD\ ELJJHU \RX G KDYH WR FRQVWUXFW D QHZ DUUD\ WKDW LV ODUJHU WKDQ WKH ROG RQH DQG FRS\ YDOXHV IURP WKH ROG WR WKH QHZ DUUD\ <RX FDQ W FRPSDUH DUUD\V IRU HTXDOLW\ XVLQJ D VLPSOH == WHVW 5HPHPEHU WKDW DUUD\V DUH REMHFWV VR LI \RX DVN ZKHWKHU RQH DUUD\ LV == WR DQRWKHU DUUD\ \RX DUH DVNLQJ ZKHWKHU WKH\ DUH WKH VDPH REMHFW QRW ZKHWKHU WKH\ VWRUH WKH VDPH YDOXHV <RX ZRXOG KDYH WR ZULWH \RXU RZQ FRPSDULVRQ PHWKRG WKDW ZRXOG GHWHUPLQH ZKHWKHU WZR DUUD\V VWRUH WKH VDPH YDOXHV <RX FDQ W SULQW DQ DUUD\ XVLQJ D VLPSOH SULQW RU SULQWOQ VWDWHPHQW <RX ZLOO JHW DQ RGG RXWSXW ZKHQ \RX GR VR 7R SULQW WKH FRQWHQWV RI DQ DUUD\ \RX KDYH WR ZULWH \RXU RZQ IRU ORRS WKDW SULQWV HDFK LQGLYLGXDO FRPSRQHQW <RX FDQ W FKDQJH WKH VL]H RI DQ DUUD\ DIWHU LW KDV EHHQ DOORFDWHG <RX FDQ W IRU H[DPSOH WU\ WR FKDQJH WKH YDOXH RI WKH OHQJWK ILHOG RI DQ DUUD\ ,I \RX ILQG WKDW \RX QHHG D ODUJHU DUUD\ \RX ZRXOG KDYH WR DOORFDWH D QHZ DUUD\ DQG FRS\ YDOXHV IURP WKH ROG DUUD\ WR WKH QHZ RQH $GYDQFHG $UUD\V ,Q WKLV VHFWLRQ ZH OO GLVFXVV VRPH DGYDQFHG XVHV RI DUUD\V VXFK DV D VKRUWFXW V\QWD[ IRU LQLWLDOL]LQJ DQ HQWLUH DUUD\ ZLWK RQH VWDWHPHQW :H OO DOVR VHH KRZ WR FUHDWH DUUD\V WKDW VWRUH REMHFWV LQVWHDG RI SULPLWLYH YDOXHV DQG ZH OO VHH KRZ WR ZULWH FRGH WKDW ZLOO VKLIW YDOXHV LQ DQ DUUD\ 398 6KLIWLQJ 9DOXHV LQ DQ $UUD\ :H RIWHQ ZDQW WR PRYH D VHULHV RI YDOXHV LQ DQ DUUD\ )RU H[DPSOH VXSSRVH ZH KDYH DQ DUUD\ RI LQWHJHUV WKDW VWRUHV WKH VHTXHQFH RI YDOXHV DQG ZH ZDQW WR URWDWH WKH YDOXHV VR WKDW WKH YDOXH DW WKH IURQW RI WKH OLVW JRHV WR WKH EDFN DQG WKH RUGHU RI WKH RWKHU YDOXHV VWD\V WKH VDPH ,Q RWKHU ZRUGV ZH ZDQW WR PRYH WKH WR WKH EDFN \LHOGLQJ WKH OLVW /HW V H[SORUH KRZ WR ZULWH FRGH WR SHUIRUP WKDW DFWLRQ 6XSSRVH WKDW ZH KDYH D YDULDEOH RI W\SH LQW>@ FDOOHG OLVW RI OHQJWK [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 9 | 7 | 5 | +---+ +-----+-----+-----+-----+-----+ WKDW VWRUHV WKH YDOXHV 7KH DW WKH IURQW RI WKH OLVW LV VXSSRVHG WR JR WR WKH EDFN RI WKH OLVW DQG WKH RWKHU YDOXHV DUH VXSSRVHG WR URWDWH :H FDQ PDNH WKH WDVN HDVLHU E\ VWRULQJ WKH YDOXH DW WKH IURQW RI WKH OLVW WKH LQ RXU H[DPSOH LQWR D ORFDO YDULDEOH int first = list[0]; :LWK WKDW YDOXH VDIHO\ WXFNHG DZD\ ZKDW ZH KDYH OHIW WR GR LV WR VKLIW WKH RWKHU IRXU YDOXHV OHIW E\ RQH SRVLWLRQ [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 9 | 7 | 5 | +---+ +-----+-----+-----+-----+-----+ / / / / / / / / / / / / V V V V +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 8 | 9 | 7 | 5 | 5 | +---+ +-----+-----+-----+-----+-----+ 7KH RYHUDOO WDVN EUHDNV GRZQ LQWR IRXU GLIIHUHQW VKLIWLQJ RSHUDWLRQV HDFK RI ZKLFK LV D VLPSOH DVVLJQPHQW VWDWHPHQW list[0] list[1] list[2] list[3] = = = = list[1]; list[2]; list[3]; list[4]; 2EYLRXVO\ ZH G ZDQW WR ZULWH WKLV DV D ORRS UDWKHU WKDQ ZULWLQJ D VHULHV RI LQGLYLGXDO DVVLJQPHQW VWDWHPHQWV (DFK RI WKH VWDWHPHQWV DERYH LV RI WKH IRUP list[i] = list[i + 1]; :H UHSODFH OLVW HOHPHQW >L@ ZLWK WKH YDOXH FXUUHQWO\ VWRUHG LQ OLVW HOHPHQW >L @ ZKLFK VKLIWV WKDW YDOXH WR WKH OHIW :H ZDQW WKLV LQVLGH RI D IRU ORRS 0DQ\ DUUD\ RSHUDWLRQV WKDW LQYROYH DFFHVVLQJ HDFK HOHPHQW RI WKH DUUD\ FDQ EH ZULWWHQ XVLQJ D IRU ORRS WKDW ORRNV OLNH WKLV 399 for (int i = 0; i < list.length; i++) { do something with list[i]. } 7KDW V QRW D EDG VWDUWLQJ SRLQW IRU WKLV RSHUDWLRQ 6R ZH FDQ XVH WKH DVVLJQPHQW VWDWHPHQW ZH FDPH XS ZLWK LQVLGH RI WKLV IRU ORRS ERG\ for (int i = 0; i < list.length; i++) { list[i] = list[i + 1]; } 7KLV LV DOPRVW WKH ULJKW DQVZHU EXW LW KDV D FODVVLF SUREOHP NQRZQ DV DQ RII E\ RQH EXJ 7KLV ORRS ZLOO H[HFXWH WLPHV IRU RXU VDPSOH DUUD\ EXW ZH ZDQW WR VKLIW MXVW IRXU GLIIHUHQW YDOXHV :H ZDQW WR GR WKLV IRU L HTXDO WR DQG EXW QRW IRU L HTXDO WR 6R WKLV ORRS JRHV RQH WRR PDQ\ WLPHV 2Q WKH ODVW LWHUDWLRQ RI WKH ORRS ZKHQ L LV HTXDO WR ZH H[HFXWH WKLV OLQH RI FRGH list[i] = list[i + 1]; ZKLFK EHFRPHV list[4] = list[5]; 7KHUH LV QR YDOXH OLVW> @ EHFDXVH WKH DUUD\ KDV RQO\ HOHPHQWV ZLWK LQGH[HV WKURXJK 6R WKLV FRGH JHQHUDWHV DQ $UUD\,QGH[2XW2I%RXQGV([FHSWLRQ 7R IL[ WKH SUREOHP ZH DOWHU WKH ORRS VR WKDW LW VWRSV RQH HDUO\ for (int i = 0; i < list.length - 1; i++) { list[i] = list[i + 1]; } 1RWLFH WKDW LQ SODFH RI WKH XVXDO OLVW OHQJWK ZH XVH OLVW OHQJWK 7KDW FDXVHV WKH ORRS WR VWRS RQH HDUO\ <RX FDQ WKLQN RI WKH PLQXV RQH LQ WKLV H[SUHVVLRQ DV RIIVHWWLQJ WKH SOXV RQH LQ WKH DVVLJQPHQW VWDWHPHQW 2I FRXUVH WKHUH LV RQH GHWDLO OHIW WR GHDO ZLWK $IWHU VKLIWLQJ YDOXHV WR WKH OHIW ZH KDYH PDGH URRP DW WKH HQG RI WKH OLVW IRU WKH YDOXH WKDW XVHG WR EH DW WKH IURQW RI WKH OLVW 5HFDOO WKDW ZH VWRUHG LW LQ D ORFDO YDULDEOH FDOOHG ILUVW %XW ZH KDYH WR DFWXDOO\ SODFH LW WKHUH DIWHU WKH ORRS H[HFXWHV list[list.length - 1] = first; 3XWWLQJ DOO RI WKLV WRJHWKHU LQWR D VWDWLF PHWKRG ZH JHW public static void rotateLeft(int list) { int first = list[0]; for (int i = 0; i < list.length - 1; i++) { list[i] = list[i + 1]; } list[list.length - 1] = first; } 400 1RWLFH WKDW WKLV LV ZULWWHQ DV D YRLG PHWKRG :H GRQ W QHHG WR UHWXUQ WKH DUUD\ HYHQ WKRXJK ZH KDYH FKDQJHG LW %HFDXVH DUUD\V DUH REMHFWV WKH PHWKRG LV SDVVHG D UHIHUHQFH WR WKH ORFDWLRQ RI WKH DUUD\ ,Q RWKHU ZRUGV LW NQRZV ZKHUH WKH DUUD\ OLYHV 7KXV DQ\ FKDQJHV WKDW DUH PDGH E\ WKH PHWKRG DUH FKDQJLQJ WKH DUUD\ REMHFW LWVHOI $Q LQWHUHVWLQJ YDULDWLRQ LV WR URWDWH WKH YDOXHV WR WKH ULJKW LQVWHDG RI URWDWLQJ WR WKH OHIW ZKLFK LV WKH LQYHUVH RSHUDWLRQ 6R LQ WKLV FDVH ZH ZDQW WR WDNH WKH YDOXH WKDW LV FXUUHQWO\ DW WKH HQG RI WKH OLVW DQG EULQJ LW WR WKH IURQW 6R LI D YDULDEOH FDOOHG OLVW VWRUHV WKH YDOXHV EHIRUHKDQG WKHQ LW VKRXOG EULQJ WKH WR WKH IURQW DQG VWRUH WKH YDOXHV :H FDQ DJDLQ EHJLQ E\ WXFNLQJ DZD\ WKH YDOXH WKDW LV EHLQJ URWDWHG LQWR D WHPSRUDU\ YDULDEOH int last = list[list.length - 1]; 7KHQ ZH ZDQW WR VKLIW YDOXHV WR WKH ULJKW [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 9 | 7 | 5 | +---+ +-----+-----+-----+-----+-----+ \ \ \ \ \ \ \ \ \ \ \ \ V V V V +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 3 | 8 | 9 | 7 | +---+ +-----+-----+-----+-----+-----+ ,Q WKLV FDVH WKH IRXU LQGLYLGXDO DVVLJQPHQW VWDWHPHQWV ZRXOG EH list[1] list[2] list[3] list[4] = = = = list[0]; list[1]; list[2]; list[3]; 2U ZULWWHQ PRUH JHQHUDOO\ list[i] = list[i - 1]; ,I ZH SXW WKLV LQVLGH RI WKH VWDQGDUG IRU ORRS ZH JHW for (int i = 0; i < list.length; i++) { list[i] = list[i - 1]; } 7KHUH DUH WZR SUREOHPV ZLWK WKLV FRGH )LUVW WKHUH LV DQRWKHU RII E\ RQH EXJ 7KH ILUVW DVVLJQPHQW VWDWHPHQW ZH ZDQW WR SHUIRUP ZRXOG VHW OLVW> @ WR EH ZKDW LV FXUUHQWO\ LQ OLVW> @ EXW WKLV ORRS VHWV OLVW> @ WR OLVW> @ 7KDW JHQHUDWHV DQ $UUD\,QGH[2XW2I%RXQGV([FHSWLRQ EHFDXVH WKHUH LV QR YDOXH OLVW> @ 6R ZH GRQ W ZDQW WR VWDUW L DW ZH ZDQW WR VWDUW LW DW for (int i = 1; i < list.length; i++) { list[i] = list[i - 1]; } 401 7KLV GRHVQ W ZRUN HLWKHU ,W DYRLGV WKH $UUD\,QGH[2XW2I%RXQGV([FHSWLRQ EXW WKLQN DERXW ZKDW LW GRHV 7KH ILUVW WLPH WKURXJK WKH ORRS LW DVVLJQV OLVW> @ WR ZKDW LV LQ OLVW> @ [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 9 | 7 | 5 | +---+ +-----+-----+-----+-----+-----+ \ \ \ V +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 3 | 9 | 7 | 5 | +---+ +-----+-----+-----+-----+-----+ :KDW KDSSHQHG WR WKH YDOXH " :H KDYH RYHUZULWWHQ LW ZLWK WKH YDOXH WKH ORRS ZH VHW OLVW> @ WR EH OLVW> @ [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 3 | 9 | 7 | 5 | +---+ +-----+-----+-----+-----+-----+ \ \ \ V +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 3 | 3 | 7 | 5 | +---+ +-----+-----+-----+-----+-----+ 6R WKH QH[W WLPH WKURXJK <RX PLJKW VD\ :DLW D PLQXWH OLVW> @ LVQ W D LW V DQ ,W ZDV DQ ZKHQ ZH VWDUWHG EXW WKH ILUVW LWHUDWLRQ RI WKH ORRS UHSODFHG WKH ZLWK D $QG QRZ ZH YH FRSLHG WKDW LQWR WKH VSRW ZKHUH XVHG WR EH 6R WKH QH[W WLPH WKURXJK WKH ORRS ZH VHW OLVW> @ WR EH ZKDW LV LQ OLVW> @ [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 3 | 3 | 7 | 5 | +---+ +-----+-----+-----+-----+-----+ \ \ \ V +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 3 | 3 | 3 | 5 | +---+ +-----+-----+-----+-----+-----+ 7KLV RYHUZULWHV WKH LQ OLVW> @ ZLWK D 7KH ILQDO WLPH WKURXJK WKH ORRS ZH VHW OLVW> @ WR EH ZKDW LV FXUUHQWO\ 402 [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 3 | 3 | 3 | 5 | +---+ +-----+-----+-----+-----+-----+ \ \ \ V +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 3 | 3 | 3 | 3 | +---+ +-----+-----+-----+-----+-----+ 6R WKLV ORRS HQGV XS SXWWLQJ LQWR HYHU\ FHOO RI WKH DUUD\ 2EYLRXVO\ WKDW LV QRW ZKDW ZH ZDQW 7R PDNH WKLV FRGH ZRUN ZH KDYH WR UXQ WKH ORRS LQ UHYHUVH RUGHU IURP ULJKW WR OHIW LQVWHDG RI OHIW WR ULJKW 6R OHW V EDFN XS WR ZKHUH ZH VWDUWHG [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 9 | 7 | 5 | +---+ +-----+-----+-----+-----+-----+ :H WXFNHG DZD\ WKH ILQDO YDOXH RI WKH OLVW LQWR D ORFDO YDULDEOH 7KDW IUHHV XS WKH ILQDO DUUD\ SRVLWLRQ 6R ZH VWDUW E\ ILUVW DVVLJQLQJ OLVW> @ WR EH ZKDW LV LQ OLVW> @ [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 9 | 7 | 5 | +---+ +-----+-----+-----+-----+-----+ \ \ \ V +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 9 | 7 | 7 | +---+ +-----+-----+-----+-----+-----+ 7KLV ZLSHV RXW WKH WKDW ZDV D WKH HQG RI WKH OLVW EXW ZH KDYH WKDW YDOXH WXFNHG DZD\ LQ D ORFDO YDULDEOH $QG RQFH ZH KDYH SHUIRUPHG WKLV DVVLJQPHQW VWDWHPHQW ZH IUHH XS OLVW> @ ZKLFK PHDQV QRZ ZH FDQ VHW OLVW> @ WR EH ZKDW LV FXUUHQWO\ LQ OLVW> @ [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 9 | 7 | 7 | +---+ +-----+-----+-----+-----+-----+ \ \ \ V +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 9 | 9 | 7 | +---+ +-----+-----+-----+-----+-----+ 1RZ OLVW> @ LV IUHH VR ZH FDQ VHW OLVW> @ WR EH ZKDW LV FXUUHQWO\ LQ OLVW> @ 403 [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 9 | 9 | 7 | +---+ +-----+-----+-----+-----+-----+ \ \ \ V +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 8 | 9 | 7 | +---+ +-----+-----+-----+-----+-----+ $QG ZH HQG E\ VHWWLQJ OLVW> @ WR EH ZKDW LV FXUUHQWO\ LQ OLVW> @ [0] [1] [2] [3] [4] +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 8 | 8 | 9 | 7 | +---+ +-----+-----+-----+-----+-----+ \ \ \ V +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 3 | 3 | 8 | 9 | 7 | +---+ +-----+-----+-----+-----+-----+ $W WKLV SRLQW WKH RQO\ WKLQJ OHIW WR GR LV WR SXW WKH WKDW ZH WXFNHG DZD\ LQWR D ORFDO YDULDEOH DW WKH IURQW RI WKH OLVW DQG ZH UH GRQH +---+ +-----+-----+-----+-----+-----+ list | +-+---> | 5 | 3 | 8 | 9 | 7 | +---+ +-----+-----+-----+-----+-----+ :H FDQ UHYHUVH WKH IRU ORRS E\ FKDQJLQJ WKH L WR L DQG DGMXVWLQJ WKH LQLWLDOL]DWLRQ DQG WHVW 3XWWLQJ DOO RI WKLV WRJHWKHU ZH JHW WKH IROORZLQJ PHWKRG public static void rotateRight(int list) { int last = list[list.length - 1]; for (int i = list.length - 1; i > 0; i--) { list[i] = list[i - 1]; } list[0] = last; } ,QLWLDOL]LQJ $UUD\V -DYD KDV D VSHFLDO V\QWD[ IRU LQLWLDOL]LQJ DQ DUUD\ ZKHQ \RX NQRZ H[DFWO\ ZKDW \RX ZDQW WR SXW LQWR LW )RU H[DPSOH \RX FRXOG ZULWH WKH IROORZLQJ FRGH WR LQLWLDOL]H DQ DUUD\ RI LQWV DQG DQ DUUD\ RI 6WULQJV 404 int numbers = new int[5]; numbers[0] = 13; numbers[1] = 23; numbers[2] = 480; numbers[3] = -18; numbers[4] = 75; String words[0] words[1] words[2] words[3] words[4] words[5] words = new String[6]; = "hello"; = "how"; = "are"; = "you"; = "feeling"; = "today"; 7KLV ZRUNV EXW LW V D UDWKHU WHGLRXV ZD\ WR GHFODUH WKHVH DUUD\V -DYD SURYLGHV D VKRUWKDQG int numbers = {13, 23, 480, -18, 75}; String words = {"hello", "how", "are", "you", "feeling", "today"}; <RX XVH WKH FXUO\ EUDFHV WR HQFORVH D VHULHV RI YDOXHV WKDW ZLOO EH VWRUHG LQ WKH DUUD\ 2UGHU LV LPSRUWDQW 7KH ILUVW YDOXH ZLOO JR LQWR LQGH[ WKH VHFRQG YDOXH ZLOO JR LQWR LQGH[ DQG VR RQ -DYD FRXQWV KRZ PDQ\ YDOXHV \RX LQFOXGH DQG FRQVWUXFWV DQ DUUD\ RI MXVW WKH ULJKW VL]H ,W WKHQ VWRUHV WKH YDULRXV YDOXHV LQWR WKH DSSURSULDWH VSRWV LQ WKH DUUD\ 7KLV LV RQH RI RQO\ WZR SODFHV ZKHUH -DYD ZLOO FRQVWUXFW DQ REMHFW ZLWKRXW WKH new NH\ZRUG 7KH RWKHU SODFH ZH VDZ WKLV ZDV ZLWK 6WULQJ OLWHUDOV ZKHUH -DYD FRQVWUXFWV 6WULQJ REMHFWV IRU \RX ZLWKRXW \RX KDYLQJ WR FDOO QHZ %RWK RI WKHVH DUH FRQYHQLHQFHV IRU SURJUDPPHUV EHFDXVH WKHVH WDVNV DUH VR FRPPRQ WKDW WKH GHVLJQHUV RI WKH ODQJXDJH ZDQWHG WR PDNH LW HDV\ WR GR WKHP $UUD\V LQ WKH -DYD &ODVV /LEUDULHV <RX ZLOO ILQG DUUD\V DSSHDULQJ WKURXJKRXW WKH -DYD FODVV OLEUDULHV ,Q IDFW WKHUH LV D VSHFLDO FODVV FDOOHG $UUD\V WKDW KDV D FROOHFWLRQ RI XWLOLW\ SURJUDPV IRU PDQLSXODWLQJ DUUD\V 2QH SDUWLFXODUO\ KDQG\ PHWKRG LQ WKH $UUD\V FODVV LV WKH PHWKRG VRUW )RU H[DPSOH JLYHQ WKH IROORZLQJ YDULDEOH GHFODUDWLRQV int numbers = {13, 23, 480, -18, 75}; String words = {"hello", "how", "are", "you", "feeling", "today"}; :H FRXOG PDNH WKH IROORZLQJ FDOOV Arrays.sort(numbers); Arrays.sort(words); ,I \RX WKHQ H[DPLQH WKH YDOXHV RI WKH DUUD\V \RX ZLOO VHH WKDW WKH QXPEHUV DSSHDU LQ QXPHULFDOO\ LQFUHDVLQJ RUGHU DQG WKH ZRUGV DSSHDU LQ DOSKDEHWLFDOO\ LQFUHDVLQJ RUGHU %HORZ LV D FRPSOHWH SURJUDP WKDW GHPRQVWUDWHV WKLV 7KH $UUD\V FODVV LV SDUW RI WKH MDYD XWLO SDFNDJH VR \RX ZLOO VHH WKDW WKH SURJUDP LQFOXGHV DQ LPSRUW IURP MDYD XWLO 405 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 import java.util.*; public class SortingExample { public static void main(String args) { int numbers = {13, 23, 480, -18, 75}; String words = {"hello", "how", "are", "you", "feeling", "today"}; System.out.println("Before sorting:"); display(numbers, words); Arrays.sort(numbers); Arrays.sort(words); System.out.println("After sorting:"); display(numbers, words); } public static void display(int numbers, String words) { for (int n : numbers) { System.out.print(n + " "); } System.out.println(); for (String s : words) { System.out.print(s + " "); } System.out.println(); System.out.println(); } } 1RWLFH WKDW ZH ZHUH DEOH WR XVH IRU HDFK ORRSV LQ WKH GLVSOD\ PHWKRG 7KH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW Before sorting: 13 23 480 -18 75 hello how are you feeling today After sorting: -18 13 23 75 480 are feeling hello how today you $UUD\V DSSHDU LQ PDQ\ RWKHU SODFHV LQ WKH -DYD FODVV OLEUDULHV )RU H[DPSOH WKH 6WULQJ FODVV KDV D PHWKRG FDOOHG WR&KDU$UUD\ WKDW ZLOO FRQVWUXFW DQ DUUD\ RI FKDUDFWHUV WKDW VWRUHV WKH LQGLYLGXDO FKDUDFWHUV RI WKH DUUD\ 7KHUH LV D FRUUHVSRQGLQJ FRQVWUXFWRU IRU WKH 6WULQJ FODVV WKDW DOORZV \RX WR FRQVWUXFW D 6WULQJ IURP D FKDUDFWHU DUUD\ $V DQ H[DPSOH WKH FRGH EHORZ FRQYHUWV D ZRUG LQWR D FRUUHVSRQGLQJ FKDUDFWHU DUUD\ WKHQ VRUWV WKH FKDUDFWHUV LQ WKH DUUD\ WKHQ UHFRPELQHV WKH FKDUDFWHUV WR PDNH D VWULQJ public static String sorted(String s) { char letters = s.toCharArray(); Arrays.sort(letters); String result = new String(letters); return result; } 406 7KLV PLJKW VHHP OLNH D VWUDQJH PHWKRG WR GHILQH EXW LW FDQ EH YHU\ KHOSIXO IRU VROYLQJ ZRUG SX]]OHV NQRZQ DV DQDJUDPV $Q DQDJUDP RI D ZRUG LV D ZRUG WKDW KDV WKH VDPH OHWWHUV EXW LQ D GLIIHUHQW RUGHU )RU H[DPSOH WKH IROORZLQJ ZRUGV DUH DOO DQDJUDPV RI HDFK RWKHU SHDUV VSHDU SDUHV UHDSV VSDUH 8VLQJ WKH PHWKRG DERYH DOO RI WKHVH ZRUGV ZRXOG EH FRQYHUWHG WR "aeprs" 6R WR VROYH DQ DQDJUDP \RX FDQ VLPSO\ JR WKURXJK D GLFWLRQDU\ FRQYHUWLQJ HYHU\ ZRUG WR LWV VRUWHG IRUP DQG VHH LI LW PDWFKHV WKH VRUWHG IRUP RI WKH DQDJUDP \RX DUH ZRUNLQJ ZLWK $UUD\V RI 2EMHFWV $OO RI WKH DUUD\V ZH KDYH ORRNHG DW VR IDU KDYH VWRUHG SULPLWLYH YDOXHV OLNH VLPSOH LQWHJHU YDOXHV EXW \RX FDQ KDYH DUUD\V RI DQ\ -DYD W\SH $UUD\V RI REMHFWV EHKDYH VOLJKWO\ GLIIHUHQWO\ EHFDXVH RI WKH IDFW WKDW REMHFWV DUH VWRUHG DV UHIHUHQFHV UDWKHU WKDQ DV GDWD YDOXHV &RQVWUXFWLQJ DQ DUUD\ RI REMHFWV LV XVXDOO\ D WZR VWHS SURFHVV EHFDXVH ZH QRUPDOO\ KDYH WR FRQVWUXFW ERWK WKH DUUD\ DQG WKH LQGLYLGXDO REMHFWV &RQVLGHU IRU H[DPSOH WKH IROORZLQJ VWDWHPHQW Point points = new Point[3]; 7KLV GHFODUHV D YDULDEOH FDOOHG SRLQWV WKDW UHIHUV WR DQ DUUD\ RI OHQJWK WKDW VWRUHV UHIHUHQFHV WR 3RLQW REMHFWV 7KH new NH\ZRUG FRQVWUXFWV WKH DUUD\ EXW LW GRHVQ W FRQVWUXFW DQ\ DFWXDO 3RLQW REMHFWV ,QVWHDG LW FRQVWUXFWV DQ DUUD\ RI OHQJWK HDFK HOHPHQW RI ZKLFK FDQ VWRUH D UHIHUHQFH WR D 3RLQW :KHQ -DYD FRQVWUXFWV WKH DUUD\ LW LQLWLDOL]HV WKHVH DUUD\ HOHPHQWV WR D VSHFLDO YDOXH NQRZQ DV null WKDW LQGLFDWHV WKDW WKH YDULDEOH GRHV QRW FXUUHQWO\ SRLQW WR DQ\ REMHFW [0] [1] [2] +---+ +-------+-------+-------+ points | +-+---> | null | null | null | +---+ +-------+-------+-------+ ,I ZH ZDQW WR KDYH VRPH DFWXDO 3RLQW REMHFWV WKH\ KDYH WR EH FRQVWUXFWHG VHSDUDWHO\ ZLWK WKH new NH\ZRUG DV LQ Point points = new Point[3]; points[0] = new Point(3, 7); points[1] = new Point(4, 5); points[2] = new Point(6, 2); $IWHU WKHVH OLQHV RI FRGH H[HFXWH ZH ZRXOG KDYH LQGLYLGXDO 3RLQW REMHFWV WKDW WKH YDULRXV DUUD\ HOHPHQWV UHIHU WR 407 [0] [1] [2] +---+ +-------+-------+-------+ points | +-+---> | | | | | | | +---+ +---+---+---+---+---+---+ | | | +----------------+ | +------------------+ | | | V V V +--------------------+ +--------------------+ +--------------------+ | +----+ +----+ | | +----+ +----+ | | +----+ +----+ | | x | 3 | y | 7 | | | x | 4 | y | 5 | | | x | 6 | y | 2 | | | +----+ +----+ | | +----+ +----+ | | +----+ +----+ | +--------------------+ +--------------------+ +--------------------+ 1RWLFH WKDW ZH QHHG WKH new NH\ZRUG LQ IRXU GLIIHUHQW SODFHV EHFDXVH WKHUH DUH IRXU REMHFWV WR EH FRQVWUXFWHG WKH DUUD\ LWVHOI DQG WKH WKUHH LQGLYLGXDO 3RLQW REMHFWV :H FRXOG DOVR XVH WKH FXUO\ EUDFH QRWDWLRQ IRU LQLWLDOL]LQJ WKH DUUD\ LQ ZKLFK FDVH ZH GRQ W QHHG WKH new NH\ZRUG WR FRQVWUXFW WKH DUUD\ LWVHOI Point points = {new Point(3, 7), new Point(4, 5), new Point(6, 2)}; &RPPDQG /LQH $UJXPHQWV :H KDYH VHHQ D UHIHUHQFH DQ DUUD\ RI REMHFWV HYHU VLQFH ZH ZURWH WKH KHOOR ZRUOG SURJUDP RI FKDSWHU :KHQHYHU ZH GHILQH D PDLQ PHWKRG ZH DUH UHTXLUHG WR LQFOXGH DV LWV SDUDPHWHU String args ZKLFK LV DQ DUUD\ RI 6WULQJ REMHFWV 7KLV DUUD\ LV LQLWLDOL]HG E\ -DYD LWVHOI LI WKH XVHU SURYLGHV ZKDW DUH NQRZQ DV FRPPDQG OLQH DUJXPHQWV ZKHQ -DYD LV LQYRNHG )RU H[DPSOH QRUPDOO\ D -DYD FODVV FDOOHG 'R6RPHWKLQJ ZRXOG EH VWDUWHG IURP WKH FRPPDQG LQWHUIDFH E\ D FRPPDQG OLNH WKLV java DoSomething 7KH XVHU KDV WKH RSWLRQ WR W\SH H[WUD DUJXPHQWV DV LQ java DoSomething temperature.dat temperature.out ,Q WKLV FDVH WKH XVHU KDV VSHFLILHG WZR H[WUD DUJXPHQWV WKDW DUH ILOH QDPHV WKDW WKH SURJUDP VKRXOG XVH H J WKH QDPHV RI DQ LQSXW DQG RXWSXW ILOH ,I WKH XVHU W\SHV WKHVH H[WUD DUJXPHQWV ZKHQ VWDUWLQJ XS -DYD WKHQ WKH String args SDUDPHWHU WR PDLQ ZLOO EH LQLWLDOL]HG WR DQ DUUD\ RI OHQJWK VWRULQJ WKHVH WZR VWULQJV [0] [1] +---+ +-------+-------+ args | +-+---> | | | | | +---+ +---+---+---+---+ | | +--------+ +-------+ | | V V +-------------------+ +-------------------+ | "temperature.dat" | | "temperature.out" | +-------------------+ +-------------------+ 408 0XOWLGLPHQVLRQDO $UUD\V RSWLRQDO 7KH DUUD\ H[DPSOHV LQ WKH SUHYLRXV VHFWLRQV DOO LQYROYHG ZKDW DUH NQRZQ DV RQH GLPHQVLRQDO DUUD\V D VLQJOH URZ RU D VLQJOH FROXPQ RI GDWD 2IWHQ ZH ZDQW WR VWRUH GDWD LQ D PXOWLGLPHQVLRQDO ZD\ )RU H[DPSOH ZH PLJKW ZDQW D WZR GLPHQVLRQDO JULG RI GDWD WKDW KDV ERWK URZV DQG FROXPQV )RUWXQDWHO\ \RX FDQ IRUP DUUD\V RI DUELWUDULO\ PDQ\ GLPHQVLRQV RQH GRXEOH double D RQH GLPHQVLRQDO DUUD\ RI GRXEOHV double D GLPHQVLRQDO JULG RI GRXEOHV double D GLPHQVLRQDO FROOHFWLRQ RI GRXEOHV double $UUD\V RI PRUH WKDQ RQH GLPHQVLRQ LQ JHQHUDO DUH FDOOHG PXOWLGLPHQVLRQDO DUUD\V 0XOWLGLPHQVLRQDO $UUD\ $Q DUUD\ RI DUUD\V WKH HOHPHQWV RI ZKLFK DUH DFFHVVHG ZLWK PXOWLSOH LQWHJHU LQGH[HV 5HFWDQJXODU 7ZR 'LPHQVLRQDO $UUD\V 7KH PRVW FRPPRQ XVH RI D PXOWLGLPHQVLRQDO DUUD\ LV D WZR GLPHQVLRQDO DUUD\ RI D FHUWDLQ ZLGWK DQG KHLJKW )RU H[DPSOH VXSSRVH WKDW RQ WKUHH VHSDUDWH GD\V \RX WRRN D VHULHV RI ILYH WHPSHUDWXUH UHDGLQJV <RX FDQ GHILQH D WZR GLPHQVLRQDO DUUD\ WKDW KDV WKUHH URZV DQG ILYH FROXPQV DV IROORZV double temps = new double[3][5]; 1RWLFH WKDW RQ ERWK WKH OHIW DQG ULJKW VLGHV RI WKLV DVVLJQPHQW VWDWHPHQW ZH KDYH WR XVH D GRXEOH VHW RI VTXDUH EUDFNHWV 2Q WKH OHIW LQ GHVFULELQJ WKH W\SH ZH KDYH WR PDNH LW FOHDU WKDW WKLV LV QRW MXVW D RQH GLPHQVLRQDO VHTXHQFH RI YDOXHV ZKLFK ZRXOG EH RI W\SH GRXEOH>@ EXW LQVWHDG D WZR GLPHQVLRQDO JULG RI YDOXHV ZKLFK LV RI W\SH GRXEOH>@>@ 2Q WKH ULJKW LQ FRQVWUXFWLQJ WKH DUUD\ ZH KDYH WR VSHFLI\ WKH GLPHQVLRQV RI WKH JULG 7KH QRUPDO FRQYHQWLRQ LV WR OLVW WKH URZ ILUVW IROORZHG E\ WKH FROXPQ VR ZH DVN IRU URZV DQG FROXPQV 7KH UHVXOWLQJ DUUD\ ZRXOG ORRN OLNH WKLV [0] [1] [2] [3] [4] +-------+-------+-------+-------+-------+ [0] | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | +---+ +-------+-------+-------+-------+-------+ temps | +-+---> [1] | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | +---+ +-------+-------+-------+-------+-------+ [2] | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | +-------+-------+-------+-------+-------+ $V ZLWK RQH GLPHQVLRQDO DUUD\V WKH YDOXHV DUH LQLWLDOL]HG WR DQG WKH LQGH[HV VWDUW ZLWK IRU ERWK URZV DQG FROXPQV 2QFH ZH KDYH FUHDWHG VXFK DQ DUUD\ ZH FDQ UHIHU WR LQGLYLGXDO HOHPHQWV E\ SURYLGLQJ VSHFLILF URZ DQG FROXPQ QXPEHUV LQ WKDW RUGHU )RU H[DPSOH WR VHW WKH IRXUWK YDOXH RI WKH ILUVW URZ WR DQG WR VHW WKH ILUVW YDOXH RI WKH WKLUG URZ WR ZH ZRXOG VD\ temps[0][3] = 98.3; temps[2][0] = 99.4; // fourth value of first row // first value of third row 409 $IWHU H[HFXWLQJ WKHVH OLQHV RI FRGH WKH DUUD\ ZRXOG ORRN OLNH WKLV [0] [1] [2] [3] [4] +-------+-------+-------+-------+-------+ [0] | 0.0 | 0.0 | 0.0 | 98.3 | 0.0 | +---+ +-------+-------+-------+-------+-------+ temps | +-+---> [1] | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | +---+ +-------+-------+-------+-------+-------+ [2] | 99.4 | 0.0 | 0.0 | 0.0 | 0.0 | +-------+-------+-------+-------+-------+ ,W LV KHOSIXO WR WKLQN RI WKLV LQ D VWHSZLVH IDVKLRQ VWDUWLQJ ZLWK WKH QDPH RI WKH DUUD\ )RU H[DPSOH LI \RX ZDQW WR UHIHU WR WKH ILUVW YDOXH RI WKH WKLUG URZ \RX REWDLQ WKDW WKURXJK WKH IROORZLQJ VWHSV temps temps[2] temps[2][0] the entire grid the entire third row the first element of the third row <RX FDQ SDVV PXOWLGLPHQVLRQDO DUUD\V DV SDUDPHWHUV MXVW DV \RX SDVV RQH GLPHQVLRQDO DUUD\V <RX QHHG WR EH FDUHIXO DERXW WKH W\SH 7R SDVV WKH WHPSHUDWXUH JULG \RX ZRXOG KDYH WR XVH D SDUDPHWHU RI W\SH GRXEOH>@>@ ZLWK ERWK VHWV RI EUDFNHWV )RU H[DPSOH EHORZ LV D PHWKRG WKDW SULQWV WKH JULG public static void print(double grid) { for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { System.out.print(grid[i][j] + " "); } System.out.println(); } } 1RWLFH WKDW WR DVN IRU WKH QXPEHU RI URZV ZH DVN IRU JULG OHQJWK DQG WR DVN IRU WKH QXPEHU RI FROXPQV ZH DVN IRU JULG>L@ OHQJWK $UUD\V FDQ KDYH DV PDQ\ GLPHQVLRQV DV \RX ZDQW )RU H[DPSOH LI \RX ZDQWHG D WKUHH GLPHQVLRQDO FXEH RI LQWHJHUV WKDW ZDV D E\ E\ FXEH \RX ZRXOG VD\ int numbers = new int[4][4][4]; 7KH QRUPDO FRQYHQWLRQ ZRXOG EH WR DVVXPH WKLV LV SODQH QXPEHU IROORZHG E\ URZ QXPEHU IROORZHG E\ FROXPQ QXPEHU DOWKRXJK \RX FDQ XVH DQ\ FRQYHQWLRQ \RX ZDQW DV ORQJ DV \RXU FRGH LV ZULWWHQ FRQVLVWHQWO\ -DJJHG $UUD\V 7KH SUHYLRXV H[DPSOHV KDYH LQYROYHG UHFWDQJXODU JULGV WKDW KDYH D IL[HG QXPEHU RI URZV DQG FROXPQV ,W LV DOVR SRVVLEOH WR FUHDWH D MDJJHG DUUD\ ZKHUH WKH QXPEHU RI FROXPQV YDULHV IURP URZ WR URZ )RU H[DPSOH VXSSRVH WKDW \RX ZDQWHG WR KDYH D GDWD VWUXFWXUH WKDW VWRUHV WKH URZV RI 3DVFDO V 7ULDQJOH 3DVFDO V 7ULDQJOH FRQWDLQV ZKDW DUH NQRZQ DV WKH ELQRPLDO FRHIILFLHQWV 7KH QWK URZ RI 3DVFDO V 7ULDQJOH FRQWDLQV WKH FRHIILFLHQWV REWDLQHG ZKHQ \RX H[SDQG [ \Q 410 3DVFDO V WULDQJOH ORRNV OLNH WKLV 7KH WULDQJOH KDV DQ LQWHUHVWLQJ SURSHUW\ LQ WKDW HDFK URZ EHJLQV DQG HQGV ZLWK D DQG WKH RWKHU QXPEHUV LQ WKH URZ DUH HDFK WKH VXP RI WZR QXPEHUV IURP WKH SUHYLRXV URZ 7KHVH REVHUYDWLRQV FDQ EH XVHG WR FRPSXWH HDFK URZ LQ D IDLUO\ VWUDLJKWIRUZDUG PDQQHU %XW HDFK URZ KDV D GLIIHUHQW QXPEHU RI HOHPHQWV 5RZ KDV FROXPQ URZ KDV FROXPQV URZ KDV FROXPQV DQG VR RQ 7R FRQVWUXFW D MDJJHG DUUD\ ZH GLYLGH WKH FRQVWUXFWLRQ LQWR WZR VWHSV :H FRQVWUXFW WKH DUUD\ IRU KROGLQJ URZV ILUVW DQG WKHQ ZH FRQVWUXFW HDFK LQGLYLGXDO URZ VHFRQG )RU H[DPSOH WR FRQVWUXFW DQ DUUD\ WKDW KDV HOHPHQWV LQ WKH ILUVW URZ HOHPHQWV LQ WKH VHFRQG URZ DQG HOHPHQWV LQ WKH WKLUG URZ ZH FDQ VD\ int jagged = new int[3]; jagged[0] = new int[2]; jagged[1] = new int[4]; jagged[2] = new int[3]; 7KLV ZRXOG FRQVWUXFW DQ DUUD\ WKDW ORRNV OLNH WKLV [0] [1] [2] [3] +-------+-------+ [0] | 0 | 0 | +---+ +-------+-------+-------+-------+ jagged | +-+---> [1] | 0 | 0 | 0 | 0 | +---+ +-------+-------+-------+-------+ [2] | 0 | 0 | 0 | +-------+-------+-------+ 3XWWLQJ WKLV DOO WRJHWKHU EHORZ LV D SURJUDP WKDW FRPSXWHV WKH ILUVW URZV RI 3DVFDO V WULDQJOH ,Q WKH PDLQ PHWKRG LW GRHV WKH ILUVW SDUW RI WKH FRQVWUXFWLRQ VSHFLI\LQJ WKH QXPEHU RI URZV WR XVH ,W WKHQ FDOOV D PHWKRG ILOO,Q WKDW FRQVWUXFWV HDFK LQGLYLGXDO URZ ,W WKHQ SDVVHV WKH UHVXOW WR D SULQW PHWKRG WKDW SULQWV WKH UHVXOW 411 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 // // // // This program constructs a jagged two-dimensional array that stores Pascal's Triangle. It takes advantage of the fact that each value other than the 1's that appear at the beginning and end of each row is the sum of two values from the previous row. public class PascalsTriangle { public static void main(String args) { int triangle = new int[11]; fillIn(triangle); print(triangle); } public static void fillIn(int triangle) { for (int i = 0; i < triangle.length; i++) { triangle[i] = new int[i + 1]; triangle[i][0] = 1; triangle[i][i] = 1; for (int j = 1; j < i; j++) { triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]; } } } public static void print(int triangle) { for (int i = 0; i < triangle.length; i++) { for (int j = 0; j < triangle[i].length; j++) { System.out.print(triangle[i][j] + " "); } System.out.println(); } } } ,W SURGXFHV WKH IROORZLQJ RXWSXW 1 1 1 1 1 1 1 1 1 1 1 1 2 1 3 3 1 4 6 4 1 5 10 10 5 1 6 15 20 15 6 1 7 21 35 35 21 7 1 8 28 56 70 56 28 8 1 9 36 84 126 126 84 36 9 1 10 45 120 210 252 210 120 45 10 1 &DVH 6WXG\ +RXUV :RUNHG /HW V ORRN DW D PRUH FRPSOH[ SURJUDP H[DPSOH WKDW LQYROYHV XVLQJ DUUD\V 6XSSRVH WKDW \RX KDYH DQ LQSXW ILOH WKDW KDV GDWD LQGLFDWLQJ KRZ PDQ\ KRXUV DQ HPSOR\HH KDV ZRUNHG ZLWK HDFK OLQH RI WKH LQSXW ILOH LQGLFDWLQJ WKH KRXUV ZRUNHG IRU D GLIIHUHQW ZHHN (DFK ZHHN KDV GD\V VR WKHUH FRXOG EH XS WR QXPEHUV OLVWHG RQ HDFK OLQH :H JHQHUDOO\ FRQVLGHU 0RQGD\ WR EH WKH VWDUW RI WKH ZRUN ZHHN VR OHW V DVVXPH WKDW HDFK OLQH OLVWV KRXUV ZRUNHG RQ 0RQGD\ IROORZHG E\ KRXUV ZRUNHG 412 RQ 7XHVGD\ DQG VR RQ DQG HQGLQJ ZLWK KRXUV ZRUNHG RQ 6XQGD\ %XW ZH OO DOORZ WKH OLQHV WR KDYH IHZHU WKDQ QXPEHUV LI VRPHRQH ZRUNHG IHZHU WKDQ GD\V %HORZ LV D VDPSOH LQSXW ILOH WKDW ZH OO FDOO hours.txt 8 8 8 3 8 0 8 8 4 4 0 8 0 8 8 8 8 0 8 4 4 8 8 8 4 4 8 6 4 4 8 8 4 8 4 /HW V ZULWH D SURJUDP WKDW UHDGV WKLV LQSXW ILOH UHSRUWLQJ WRWDOV IRU HDFK URZ DQG HDFK FROXPQ 7KH WRWDOV IRU HDFK URZ ZLOO WHOO XV KRZ PDQ\ KRXUV WKH SHUVRQ KDV ZRUNHG HDFK ZHHN 7KH WRWDOV IRU HDFK FROXPQ ZLOO WHOO XV KRZ PDQ\ KRXUV WKH SHUVRQ ZRUNHG RQ 0RQGD\V YHUVXV 7XHVGD\V YHUVXV :HGQHVGD\V DQG VR RQ ,W V FOHDU WKDW WKH QXPEHU LV OLNHO\ WR VKRZ XS LQ VHYHUDO SODFHV LQ WKLV SURJUDP VR LW PDNHV VHQVH WR GHILQH D FODVV FRQVWDQW WKDW ZH FDQ XVH LQVWHDG RI WKH PDJLF QXPEHU public static final int DAYS = 7; // # of days in a week 2XU PDLQ PHWKRG FDQ EH IDLUO\ VKRUW RSHQLQJ WKH LQSXW ILOH ZH ZDQW WR UHDG DQG FDOOLQJ D PHWKRG WR SURFHVV WKH ILOH public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours.txt")); processFile(input); } :H VDZ LQ FKDSWHU WKDW IRU OLQH RULHQWHG GDWD ZH FDQ JHQHUDOO\ XVH WKH IROORZLQJ SDWWHUQ DV D VWDUWLQJ SRLQW IRU ILOH SURFHVVLQJ FRGH while (input.hasNextLine()) { String text = input.nextLine(); process text. } 7KLV ORRS DOORZV XV WR SURFHVV RQH OLQH DW D WLPH %XW HDFK OLQH KDV LQWHUQDO VWUXFWXUH EHFDXVH WKHUH DUH XS WR QXPEHUV RQ HDFK OLQH 6R LQ WKLV FDVH LW PDNHV VHQVH WR FRQVWUXFW D 6FDQQHU IRU HDFK LQSXW OLQH DQG WR ZULWH D VHSDUDWH PHWKRG IRU SURFHVVLQJ D OLQH RI GDWD while (input.hasNextLine()) { String text = input.nextLine(); Scanner data = new Scanner(text); call method to process data. } 6R ZKDW DUH ZH VXSSRVHG WR GR WR SURFHVV D OLQH RI GDWD" 2QH WKLQJ ZH KDYH WR GR LV WR DGG XS WKH KRXUV ZRUNHG GXULQJ WKH ZHHN DQG UHSRUW WKDW QXPEHU ,I WKDW V DOO ZH KDG WR GR WKHQ ZH FRXOG VROYH WKLV DV D VLPSOH ILOH SURFHVVLQJ SUREOHP DQG DYRLG DUUD\V DOWRJHWKHU %XW ZH DOVR KDYH WR DGG XS FROXPQV UHSRUWLQJ WKH WRWDO KRXUV ZRUNHG RQ 0RQGD\ 7XHVGD\ DQG VR RQ 413 7KLV LV D SHUIHFW DSSOLFDWLRQ IRU DQ DUUD\ :H DUH LQ HIIHFW VROYLQJ VHYHQ GLIIHUHQW FXPXODWLYH VXP WDVNV :H ZDQW WR VXP XS DOO KRXUV ZRUNHG RQ 0RQGD\ DOO KRXUV ZRUNHG RQ 7XHVGD\ DQG VR RQ 6R KDYLQJ DQ DUUD\ RI LQWHJHUV ZLOO DOORZ XV WR FDOFXODWH WKHVH FROXPQ WRWDOV 6R RXU SVHXGRFRGH EHFRPHV int total = new int[DAYS]; while (input.hasNextLine()) { String text = input.nextLine(); Scanner data = new Scanner(text); call method to process data. update total to include this week's data. } report total. 5HPHPEHU WKDW LQ FXPXODWLYH VXP ZH DOZD\V LQLWLDOL]H RXU VXP WR :KHQ ZH FRQVWUXFW DQ LQW DUUD\ -DYD LQLWLDOL]HV WKH YDULDEOHV WR 6R RXU WRWDO DUUD\ ZLOO VWRUH LQWHJHUV HDFK ZLWK YDOXH [0] [1] [2] [3] [4] [5] [6] +---+ +-------+-------+-------+-------+-------+-------+-------+ total | +-+---> | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +---+ +-------+-------+-------+-------+-------+-------+-------+ 6R KRZ GR ZH SURFHVV WKH GDWD IURP DQ LQSXW OLQH LQ VXFK D ZD\ WKDW ZH FDQ UHSRUW WKH VXP DFURVV DQG DOVR DGG WKH YDOXHV LQWR WKLV WRWDO DUUD\" 7KH ILUVW OLQH RI WKH VDPSOH LQSXW ILOH LV "8 8 8 8 8" 7KHUH DUH VHYHUDO DSSURDFKHV ZH FRXOG WDNH EXW LI ZH UH JRLQJ WR EH PDQLSXODWLQJ DQ DUUD\ WR NHHS WUDFN RI WKH WRWDO IRU HDFK FROXPQ WKHQ LW PDNHV VHQVH WR FUHDWH D VHFRQG DUUD\ WKDW UHSUHVHQWV WKH GDWD IRU WKH FXUUHQW ZHHN ,Q HIIHFW ZH WUDQVIHU WKH GDWD IURP WKH LQSXW ILOH LQWR DQ DUUD\ 2QFH ZH KDYH WKH GDWD LQ DUUD\ IRUP ZH FDQ GR DOO VRUWV RI PDQLSXODWLRQV RQ LW ,Q RXU SVHXGRFRGH DERYH ZH LQGLFDWH WKDW ZH VKRXOG FDOO D PHWKRG WR SURFHVV GDWD WKH 6FDQQHU WKDW FRQWDLQV WKH QH[W OLQH V ZRUWK RI GDWD 6R OHW V KDYH WKDW PHWKRG WUDQVIHU WKH GDWD IURP WKH 6FDQQHU LQWR DQ DUUD\ ,W VKRXOG WDNH WKH 6FDQQHU DV D SDUDPHWHU DQG VKRXOG UHWXUQ D UHIHUHQFH WR D QHZ DUUD\ WKDW LW FRQVWUXFWV 7KXV RXU SVHXGRFRGH EHFRPHV int total = new int[DAYS]; while (input.hasNextLine()) { String text = input.nextLine(); Scanner data = new Scanner(text); int next = transferFrom(data); do any processing necessary on next. update total to include this week's data. } report total. 7KH FRGH QHFHVVDU\ WR WUDQVIHU GDWD IURP WKH 6FDQQHU LQWR DQ DUUD\ LV QRW SDUWLFXODUO\ FRPSOH[ EXW LW PDNHV VHQVH WR VHSDUDWH WKLV NLQG RI RSHUDWLRQ LQWR DQRWKHU PHWKRG WR NHHS WKH ILOH SURFHVVLQJ PHWKRG VKRUW DQG HDV\ WR UHDG :H ZLOO ZULWH WKH FRGH IRU WUDQVIHU)URP VKRUWO\ EXW ZH DUH DFWXDOO\ IDLUO\ FORVH WR ILQLVKLQJ WKLV SVHXGRFRGH :H KDYH MXVW WKUHH SLHFHV OHIW WR ILOO LQ 2QFH ZH KDYH WUDQVIHUHG WKH GDWD IURP DQ LQSXW OLQH LQWR D 6FDQQHU ZKDW GR ZH GR ZLWK LW" :H KDYH WR UHSRUW WKH WRWDO KRXUV IRU WKLV SDUWLFXODU ZHHN DQG ZH KDYH WR DGG WKLV ZHHN V KRXUV LQWR RXU FXPXODWLYH VXP IRU WKH FROXPQV 1HLWKHU RI WKHVH RSHUDWLRQV LV SDUWLFXODUO\ FRPSOH[ EXW ZH FDQ DJDLQ VLPSOLI\ RXU ILOH SURFHVVLQJ FRGH E\ LQWURGXFLQJ PHWKRGV 414 WKDW SHUIRUP PRVW RI WKH GHWDLOV DVVRFLDWHG ZLWK WKHVH WDVNV :H PLJKW RIWHQ ILQG RXUVHOYHV ZDQWLQJ WR DGG XS WKH QXPEHUV LQ DQ DUUD\ VR LW PDNHV VHQVH WR KDYH D PHWKRG FDOOHG VXP WKDW ZH FDQ XVH WR DGG XS WKLV URZ :H PLJKW DOVR ILQG RXUVHOYHV ZDQWLQJ WR DGG RQH DUUD\ WR DQRWKHU WKH DUUD\ HTXLYDOHQW RI VD\LQJ VXP QH[W VR LW DOVR PDNHV VHQVH WR PDNH WKDW D VHSDUDWH PHWKRG 7KH ILQDO HOHPHQW WR ILOO LQ IRU RXU SVHXGRFRGH LV UHSRUWLQJ WKH WRWDO 7KLV LV VRPHWKLQJ WKDW DOVR FDQ EH HDVLO\ SXW LQ D VHSDUDWH PHWKRG :LWK WKHVH WKUHH PHWKRGV LQ PLQG ZH FDQ ILQLVK WUDQVODWLQJ RXU SVHXGRFRGH LQWR DFWXDO FRGH int total = new int[DAYS]; while (input.hasNextLine()) { String text = input.nextLine(); Scanner data = new Scanner(text); int next = transferFrom(data); System.out.println("Total hours = " + sum(next)); addTo(total, next); } System.out.println(); print(total); 7KH WUDQVIHU)URP 0HWKRG 7KH WUDQVIHU)URP PHWKRG LV JLYHQ D 6FDQQHU DV D SDUDPHWHU DQG LV VXSSRVHG WR FRQVWUXFW D QHZ DUUD\ WKDW KDV WKH QXPEHUV IURP WKH 6FDQQHU (DFK LQSXW OLQH KDV DW PRVW QXPEHUV EXW LW PLJKW KDYH IHZHU $V D UHVXOW LW PDNHV VHQVH WR XVH D ZKLOH ORRS WKDW WHVWV ZKHWKHU WKHUH DUH PRUH QXPEHUV OHIW WR UHDG IURP WKH 6FDQQHU construct an array of 7 integers. while (data.hasNextInt()) { process data.nextInt(). } ,Q WKLV FDVH SURFHVVLQJ WKH QH[W LQWHJHU IURP WKH LQSXW PHDQV VWRULQJ LW LQ WKH DUUD\ 7KH ILUVW QXPEHU VKRXOG JR LQWR SRVLWLRQ WKH VHFRQG QXPEHU LQ SRVLWLRQ WKH WKLUG QXPEHU LQ SRVLWLRQ DQG VR RQ 7KDW PHDQV WKDW ZH QHHG VRPH NLQG RI LQWHJHU FRXQWHU WKDW JRHV XS E\ RQH HDFK WLPH WKURXJK WKH ORRS construct an array of 7 integers. start i at 0. while (data.hasNextInt()) { store data.nextInt() in position i of the array. increment i. } 7KLV LV QRZ IDLUO\ HDV\ WR WUDQVODWH LQWR DFWXDO FRGH int result = new int[DAYS]; int i = 0; while (data.hasNextInt()) { result[i] = data.nextInt(); i++; } return result; 415 ,W PD\ VHHP RGG WR KDYH D PHWKRG UHWXUQ DQ DUUD\ EXW LW PDNHV SHUIHFW VHQVH WR GR VR LQ -DYD $UUD\V DUH REMHFWV VR ZKDW LV UHWXUQHG LV D UHIHUHQFH WR WKH DUUD\ :H KDYH WR EH FDUHIXO WR VSHFLI\ WKLV SURSHUO\ LQ RXU KHDGHU :H DUH UHWXUQLQJ VRPHWKLQJ RI W\SH LQW>@ VR WKH KHDGHU ZRXOG ORRN OLNH WKLV public static int transferFrom(Scanner data) 7KH VXP 0HWKRG 7KLV LV WKH VLPSOHVW RI WKH IRXU DUUD\ SURFHVVLQJ PHWKRGV WKDW ZH QHHG WR ZULWH :H VLPSO\ ZDQW WR DGG XS WKH QXPEHUV VWRUHG LQ WKH DUUD\ 7KLV LV D FODVVLF FXPXODWLYH VXP SUREOHP DQG ZH FDQ XVH D IRU HDFK ORRS WR DFFRPSOLVK WKH WDVN public static int sum(int numbers) { int sum = 0; for (int n : numbers) { sum += n; } return sum; } 7KH DGG7R 0HWKRG 7KH LGHD IRU WKLV PHWKRG LV WR ZULWH WKH DUUD\ HTXLYDOHQW RI sum += next; :H ZLOO EH JLYHQ WZR DUUD\V RQH ZLWK WKH WRWDO KRXUV DQG RQH ZLWK WKH QH[W ZHHN V KRXUV /HW V FRQVLGHU ZKHUH ZH OO EH LQ WKH PLGGOH RI SURFHVVLQJ WKH VDPSOH LQSXW ILOH 7KH ILUVW WKUHH OLQHV RI LQSXW DUH DV IROORZV 8 8 8 8 8 8 4 8 4 8 4 4 8 4 8 4 8 6XSSRVH ZH KDYH SURSHUO\ SURFHVVHG WKH ILUVW WZR OLQHV DQG KDYH MXVW UHDG LQ WKH WKLUG OLQH IRU SURFHVVLQJ 7KHQ RXU WZR DUUD\V ZLOO ORRN OLNH WKLV [0] [1] [2] [3] [4] [5] [6] +---+ +-------+-------+-------+-------+-------+-------+-------+ total | +-+---> | 16 | 12 | 16 | 12 | 16 | 4 | 4 | +---+ +-------+-------+-------+-------+-------+-------+-------+ [0] [1] [2] [3] [4] [5] [6] +---+ +-------+-------+-------+-------+-------+-------+-------+ next | +-+---> | 8 | 4 | 8 | 4 | 8 | 0 | 0 | +---+ +-------+-------+-------+-------+-------+-------+-------+ ,W ZRXOG EH QLFH LI ZH FRXOG MXVW VD\ total += next; 416 8QIRUWXQDWHO\ \RX FDQ W XVH RSHUDWLRQV OLNH + DQG += RQ DUUD\V %XW WKRVH RSHUDWLRQV FDQ EH SHUIRUPHG RQ VLPSOH LQWHJHUV DQG WKHVH DUUD\V DUH FRPSRVHG RI VLPSOH LQWHJHUV 6R ZH EDVLFDOO\ MXVW KDYH WR WHOO WKH FRPSXWHU WR GR GLIIHUHQW += RSHUDWLRQV RQ WKH LQGLYLGXDO DUUD\ HOHPHQWV 7KLV FDQ EH HDVLO\ ZULWWHQ ZLWK D IRU ORRS public static void addTo(int total, int next) { for (int i = 0; i < DAYS; i++) { total[i] += next[i]; } } 7KH SULQW 0HWKRG 7KH ILQDO PHWKRG ZH KDYH WR ZULWH LQYROYHV SULQWLQJ RXW WKH FROXPQ WRWDOV WKH WRWDOV IRU HDFK GD\ RI WKH ZHHN 7KH EDVLF FRGH LQYROYHV D VLPSOH IRU ORRS DV LQ for (int i = 0; i < DAYS; i++) { System.out.println(total[i]); } %XW ZH GRQ W ZDQW WR VLPSO\ ZULWH RXW OLQHV RI RXWSXW HDFK ZLWK D QXPEHU RQ LW ,W ZRXOG EH QLFH WR JLYH VRPH LQIRUPDWLRQ DERXW ZKDW WKH QXPEHUV PHDQ :H NQRZ WKDW WRWDO> @ UHSUHVHQWV WKH WRWDO KRXUV ZRUNHG RQ YDULRXV 0RQGD\V DQG WRWDO> @ UHSUHVHQWV WKH WRWDO KRXUV ZRUNHG RQ 7XHVGD\V DQG VR RQ EXW VRPHERG\ UHDGLQJ RXU RXWSXW PLJKW QRW NQRZ 6R LW ZRXOG EH KHOSIXO WR ODEHO WKH RXWSXW ZLWK VRPH LQIRUPDWLRQ DERXW ZKLFK GD\ HDFK WRWDO JRHV ZLWK :H FDQ GR VR E\ GHILQLQJ RXU RZQ DUUD\ RI 6WULQJ OLWHUDOV String dayNames = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; 8VLQJ WKLV DUUD\ ZH FDQ LPSURYH RXU IRU ORRS WR SULQW RXW WKH QDPH RI HDFK GD\ DORQJ ZLWK LWV WRWDO :H FDQ DOVR FDOO WKH VXP PHWKRG WR ZULWH RXW WKH RYHUDOO WRWDO KRXUV ZRUNHG DIWHU WKH IRU ORRS public static void print(int total) { String dayNames = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; for (int i = 0; i < DAYS; i++) { System.out.println(dayNames[i] + " hours = " + total[i]); } System.out.println("Total hours = " + sum(total)); } 7KH &RPSOHWH 3URJUDP 3XWWLQJ DOO WKH SLHFHV WRJHWKHU ZH HQG XS ZLWK WKH IROORZLQJ FRPSOHWH SURJUDP 417 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 an input file with information about hours worked and produces a list of total hours worked each week and total hours worked for each day of the week. Each line of the input file should contain information for one week and should have up to 7 integers representing how many hours were worked each day (Monday first, then Tuesday, through Sunday). import java.io.*; import java.util.*; public class Hours { public static final int DAYS = 7; // # of days in a week public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours.txt")); processFile(input); } // processes an input file of data about hours worked by an employee public static void processFile(Scanner input) { int total = new int[DAYS]; while (input.hasNextLine()) { String text = input.nextLine(); Scanner data = new Scanner(text); int next = transferFrom(data); System.out.println("Total hours = " + sum(next)); addTo(total, next); } System.out.println(); print(total); } // constructs an array of DAYS integers initialized to 0 and transfers // data from the given Scanner into the array in order; assumes the // Scanner has no more than DAYS integers public static int transferFrom(Scanner data) { int result = new int[DAYS]; int i = 0; while (data.hasNextInt()) { result[i] = data.nextInt(); i++; } return result; } // returns the sum of the integers in the given array public static int sum(int numbers) { int sum = 0; for (int n : numbers) { sum += n; } return sum; } // adds the values in next to their corresponding entries in total public static void addTo(int total, int next) { for (int i = 0; i < DAYS; i++) { total[i] += next[i]; } } 418 61 62 63 64 65 66 67 68 69 70 71 // prints information about the totals including total hours for each // day of the week and total hours overall public static void print(int total) { String dayNames = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; for (int i = 0; i < DAYS; i++) { System.out.println(dayNames[i] + " hours = " + total[i]); } System.out.println("Total hours = " + sum(total)); } } :KHQ H[HFXWHG RQ WKH VDPSOH LQSXW ILOH 8 8 8 3 8 0 8 8 4 4 0 8 0 8 8 8 8 0 8 4 4 8 8 8 4 4 8 6 4 4 8 8 4 8 4 ,W SURGXFHV WKH IROORZLQJ RXWSXW Total Total Total Total Total Total Total hours hours hours hours hours hours hours = = = = = = = 40 40 32 25 16 16 32 Mon hours = Tue hours = Wed hours = Thu hours = Fri hours = Sat hours = Sun hours = Total hours 43 32 36 40 34 8 8 = 201 &KDSWHU 6XPPDU\ $Q DUUD\ LV DQ REMHFW WKDW JURXSV PDQ\ SULPLWLYH YDOXHV RU REMHFWV E\ RQH QDPH (DFK LQGLYLGXDO YDOXH FDOOHG DQ HOHPHQW LV DFFHVVHG XVLQJ DQ LQWHJHU LQGH[ EHJLQQLQJ DW $UUD\V DUH GHFODUHG ZLWK VTXDUH EUDFNHWV VXFK DV int a = new int[5]; $Q DUUD\ V HOHPHQWV DUH LQGH[HG IURP WR RQH OHVV WKDQ WKH DUUD\ V OHQJWK $Q DUUD\ HOHPHQW V YDOXH PD\ EH DFFHVVHG RU PRGLILHG E\ ZULWLQJ WKH DUUD\ V QDPH DQG WKH HOHPHQW V LQGH[ LQ VTXDUH EUDFNHWV VXFK DV a[2] WR UHIHU WR WKH WKLUG HOHPHQW LQGH[ LQ WKH DUUD\ D $UUD\V DUH RIWHQ WUDYHUVHG XVLQJ IRU ORRSV 7KH OHQJWK RI DQ DUUD\ LV IRXQG E\ DFFHVVLQJ LWV length ILHOG VR WKH ORRS RYHU DQ DUUD\ D FDQ UXQ IURP LQGH[HV WR D OHQJWK 419 $WWHPSWLQJ WR DFFHVV DQ DUUD\ HOHPHQW ZLWK DQ LQGH[ OHVV WKDQ RU JUHDWHU WKDQ RU HTXDO WR WKH DUUD\ V OHQJWK ZLOO FDXVH WKH SURJUDP WR FUDVK ZLWK DQ $UUD\,QGH[2XW2I%RXQGV([FHSWLRQ $UUD\V KDYH D IL[HG VL]H ,I ZH ILQG WKDW ZH ZDQW D ODUJHU DUUD\ ZH KDYH WR FUHDWH D QHZ DUUD\ RI WKH ODUJHU VL]H DQG FRS\ WKH ROG DUUD\ V FRQWHQWV LQWR WKH QHZ DUUD\ $UUD\V GR QRW VXSSRUW FRPPRQ RSHUDWLRQV OLNH IRU FRPSDULVRQ QRU FDQ DQ DUUD\ V HOHPHQWV EH SULQWHG XVLQJ 6\VWHP RXW SULQWOQ 7R DFKLHYH VXFK EXON RSHUDWLRQV \RX PXVW H[SOLFLWO\ ZULWH ORRSV WR SULQW RU FRPSDUH HDFK HOHPHQW RI WKH DUUD\ $ VKRUWFXW V\QWD[ IRU LQLWLDOL]LQJ DUUD\ HOHPHQWV LV WR ZULWH WKH HOHPHQWV EHWZHHQ ^ ` EUDFHV ZKHQ GHFODULQJ WKH DUUD\ VXFK DV int numbers = {42, 17, 26, -5, 2}; 7KH DUUD\ ZLOO EH FUHDWHG ZLWK VXFK D OHQJWK WKDW LW H[DFWO\ ILWV WKH HOHPHQWV ZULWWHQ LQ WKLV FDVH 7KH $UUD\V FODVV LQ WKH MDYD XWLO OLEUDU\ SDFNDJH FRQWDLQV VHYHUDO XVHIXO PHWKRGV IRU PDQLSXODWLQJ DUUD\V VXFK DV Arrays.sort ZKLFK UHDUUDQJHV WKH HOHPHQWV RI WKH JLYHQ DUUD\ LQWR DVFHQGLQJ RUGHU $UUD\V RI REMHFWV DUH DFWXDOO\ DUUD\V RI UHIHUHQFHV WR REMHFWV $ QHZO\ GHFODUHG DQG LQLWLDOL]HG DUUD\ RI REMHFWV DFWXDOO\ VWRUHV null LQ DOO RI LWV HOHPHQW LQGH[HV VR HDFK HOHPHQW PXVW EH LQLWLDOL]HG LQGLYLGXDOO\ RU LQ D ORRS WR VWRUH DQ DFWXDO REMHFW $ PXOWLGLPHQVLRQDO DUUD\ LV DQ DUUD\ RI DUUD\V 7KHVH DUH RIWHQ XVHG WR VWRUH WZR GLPHQVLRQDO GDWD DV URZV DQG FROXPQV RU [ \ GDWD LQ D ' VSDFH 6HOI &KHFN 3UREOHPV 6HFWLRQ $UUD\ %DVLFV :KDW H[SUHVVLRQ VKRXOG EH XVHG WR DFFHVV WKH ILUVW HOHPHQW RI DQ DUUD\ RI LQWHJHUV QDPHG numbers" :KDW H[SUHVVLRQ VKRXOG EH XVHG WR DFFHVV WKH ODVW HOHPHQW RI numbers DVVXPLQJ LW FRQWDLQV HOHPHQWV" :KDW H[SUHVVLRQ FDQ EH XVHG WR DFFHVV LWV ODVW HOHPHQW UHJDUGOHVV RI LWV OHQJWK" :ULWH FRGH WKDW FUHDWHV DQ DUUD\ RI LQWHJHUV QDPHG data RI VL]H ZLWK WKH IROORZLQJ FRQWHQWV [0] [1] [2] [3] [4] +---+ +-------+-------+-------+-------+-------+ data | +-+---> | 27 | 51 | 33 | -1 | 101 | +---+ +-------+-------+-------+-------+-------+ :ULWH FRGH WKDW VWRUHV DOO RGG QXPEHUV EHWZHHQ DQG LQWR DQ DUUD\ XVLQJ D ORRS 0DNH WKH DUUD\ V VL]H H[DFWO\ ODUJH HQRXJK WR VWRUH WKH QXPEHUV 7U\ JHQHUDOL]LQJ \RXU FRGH VR WKDW LW ZRXOG ZRUN IRU DQ\ PLQLPXP DQG PD[LPXP YDOXH QRW MXVW DQG :KDW HOHPHQWV GRHV WKH DUUD\ numbers FRQWDLQ DIWHU WKH IROORZLQJ FRGH" 420 int numbers = new int[8]; numbers[1] = 4; numbers[4] = 99; numbers[7] = 2; int x = numbers[1]; numbers[x] = 44; numbers[numbers[7]] = 11; // uses numbers[7] as index :ULWH FRGH WKDW FRPSXWHV WKH VXP RI DOO LQWHJHUV LQ DQ DUUD\ )RU H[DPSOH LI WKH DUUD\ FRQWDLQV WKH YDOXHV ^ ` \RXU VXP VKRXOG UHWXUQ <RX PD\ ZLVK WR sumAll WKDW DFFHSWV DQ DUUD\ RI LQWHJHUV DV LWV SDUDPHWHU SXW WKLV FRGH LQWR D PHWKRG QDPHG DQG UHWXUQV WKH VXP :ULWH FRGH WKDW FRPSXWHV WKH DYHUDJH DULWKPHWLF PHDQ RI DOO HOHPHQWV LQ DQ DUUD\ RI LQWHJHUV DV D GRXEOH )RU H[DPSOH LI WKH DUUD\ SDVVHG FRQWDLQV WKH YDOXHV ^ ` \RXU DYHUDJH VKRXOG EH <RX PD\ ZLVK WR SXW WKLV FRGH LQWR D PHWKRG QDPHG average WKDW DFFHSWV DQ DUUD\ RI LQWHJHUV DV LWV SDUDPHWHU DQG UHWXUQV WKH DYHUDJH :KDW LV ZURQJ ZLWK WKH IROORZLQJ FRGH" int first = new int[2]; first[0] = 3; first[1] = 7; int second = new int[2]; second[0] = 3; second[1] = 7; // print the array elements System.out.println(first); System.out.println(second); // see if the elements are the same if (first == second) { System.out.println("They contain the same elements."); } else { System.out.println("The elements are different."); } :ULWH D SLHFH RI FRGH WKDW H[DPLQHV WZR DUUD\V RI LQWHJHUV DV LWV DUJXPHQWV DQG UHSRUWV ZKHWKHU WKH WZR DUUD\V FRQWDLQ WKH VDPH HOHPHQWV %\ GHILQLWLRQ DQ DUUD\ LV DOZD\V HTXDO WR LWVHOI 1RWH WKDW DUUD\V RI GLIIHUHQW OHQJWKV FDQQRW EH HTXDO VR WKH PHWKRG VKRXOG UHWXUQ IDOVH LQ VXFK FDVHV &RQVLGHU SXWWLQJ \RXU FRGH LQWR D PHWKRG QDPHG equal WKDW DFFHSWV WKH WZR DUUD\V DV SDUDPHWHUV DQG UHWXUQV WUXH LI WKH\ DUH HTXDO 6HFWLRQ $GYDQFHG $UUD\V :ULWH D SLHFH RI FRGH WKDW GHFODUHV DQ DUUD\ QDPHG data ZLWK WKH HOHPHQWV 8VH RQO\ RQH VWDWHPHQW WR LQLWLDOL]H WKH DUUD\ :KDW DUH WKH YDOXHV RI WKH HOHPHQWV LQ WKH DUUD\ numbers DIWHU WKH IROORZLQJ FRGH" 421 DQG int numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; for (int i = 0; i < 9; i++) { numbers[i] = numbers[i + 1]; } :KDW DUH WKH YDOXHV RI WKH HOHPHQWV LQ WKH DUUD\ numbers DIWHU WKH IROORZLQJ FRGH" int numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; for (int i = 1; i < 10; i++) { numbers[i] = numbers[i - 1]; } :ULWH FRGH WKDW SULQWV HDFK HOHPHQW RI DQ DUUD\ RI LQWHJHUV QDPHG data DV IROORZV element element element element element [0] [1] [2] [3] [4] is is is is is 14 5 27 -3 2598 )RU DGGHG FKDOOHQJH JHQHUDOL]H \RXU FRGH VR WKDW LW ZRXOG ZRUN RQ DQ DUUD\ RI DQ\ VL]H &RQVLGHU WKH IROORZLQJ PHWKRG P\VWHU\ public static void mystery(int a, int b) { for (int i = 0; i < a.length; i++) { a[i] += b[b.length - 1 - i]; } } :KDW DUH WKH YDOXHV RI WKH HOHPHQWV LQ DUUD\ a1 DIWHU WKH IROORZLQJ FRGH H[HFXWHV" int a1 = {1, 3, 5, 7, 9}; int a2 = {1, 4, 9, 16, 25}; mystery(a1, a2); &RQVLGHU WKH IROORZLQJ PHWKRG P\VWHU\ public static void mystery2(int a, int b) { for (int i = 0; i < a.length; i++) { a[i] = a[2 * i % a.length] - b[3 * i % b.length]; } } :KDW DUH WKH YDOXHV RI WKH HOHPHQWV LQ DUUD\ a1 DIWHU WKH IROORZLQJ FRGH H[HFXWHV" int a1 = {2, 4, 6, 8, 10, 12, 14, 16}; int a2 = {1, 1, 2, 3, 5, 8, 13, 21}; mystery2(a1, a2); &RQVLGHU WKH IROORZLQJ PHWKRG P\VWHU\ public static void mystery3(int data, int x, int y) { data[data[x]] = data[y]; data[y] = x; } 422 :KDW DUH WKH YDOXHV RI WKH HOHPHQWV LQ WKH DUUD\ BB DIWHU WKH IROORZLQJ FRGH H[HFXWHV" int numbers = {3, 7, 1, 0, 25, 4, 18, -1, 5}; mystery3(numbers, 3, 1); mystery3(numbers, 5, 6); mystery3(numbers, 8, 4); :ULWH D SLHFH RI FRGH WKDW FRPSXWHV WKH DYHUDJH 6WULQJ OHQJWK RI WKH HOHPHQWV RI DQ DUUD\ RI 6WULQJV )RU H[DPSOH LI WKH DUUD\ FRQWDLQV ^ EHOW KDW MHOO\ EXEEOH JXP ` WKH DYHUDJH OHQJWK LV :ULWH FRGH WKDW DFFHSWV DQ DUUD\ RI 6WULQJV DV LWV DUJXPHQW DQG LQGLFDWHV ZKHWKHU WKDW DUUD\ LV D SDOLQGURPH WKDW LV WKH VDPH IRUZDUGV DV EDFNZDUGV )RU H[DPSOH WKH DUUD\ ^ DOSKD EHWD JDPPD GHOWD JDPPD EHWD DOSKD ` LV D SDOLQGURPH ([HUFLVHV :ULWH D PHWKRG QDPHG printAll WKDW DFFHSWV DQ DUUD\ RI LQWHJHUV DV LWV DUJXPHQW DQG SULQWV WKH DUUD\ V FRQWHQWV RQ RQH OLQH )RU H[DPSOH LI WKH DUUD\ YDULDEOH FRQWDLQV WKH HOHPHQWV DQG \RXU PHWKRG VKRXOG SURGXFH WKH IROORZLQJ RXWSXW {74, 85, 102, 99, 101, 56, 84} :ULWH D PHWKRG QDPHG swap WKDW DFFHSWV DQ DUUD\ RI LQWHJHUV DQG WZR LQWHJHU LQGH[HV DV LWV DUJXPHQWV DQG VZDSV WKH HOHPHQWV DW WKRVH LQGH[HV LQ WKH DUUD\ )RU H[DPSOH LI DQ DUUD\ QDPHG numbers FRQWDLQV WKH HOHPHQWV ^ ` WKH FDOO swap(numbers, 1, 3); ZRXOG FDXVH WKH DUUD\ WR FRQWDLQ WKH HOHPHQWV ^ ` <RX PD\ DVVXPH WKDW WKH LQGH[HV SDVVHG WR \RXU swap PHWKRG DUH ZLWKLQ WKH ERXQGV RI WKH DUUD\ <RX PD\ ZLVK WR XVH WKH printAll PHWKRG IURP WKH SUHYLRXV H[HUFLVH WR SULQW WKH DUUD\ V FRQWHQWV WR FKHFN \RXU DQVZHU :ULWH D PHWKRG QDPHG reverse WKDW DFFHSWV DQ DUUD\ RI LQWHJHUV DV LWV DUJXPHQW DQG UHYHUVHV WKH RUGHU RI WKH HOHPHQWV LQ WKDW DUUD\ )RU H[DPSOH LI WKH DUUD\ ^ ` LV SDVVHG WR \RXU reverse PHWKRG DIWHUZDUG LWV FRQWHQWV VKRXOG EH ^ ` <RXU PHWKRG VKRXOG ZRUN RQ DQ DUUD\ RI DQ\ OHQJWK 1RWH WKDW \RX PD\ KDYH WR GHDO ZLWK DUUD\V RI HYHQ RU RGG VL]H +,17 <RX PD\ ZLVK WR XVH D VROXWLRQ WR DQ HDUOLHU H[HUFLVH WR KHOS \RX VROYH WKLV SUREOHP :ULWH D PHWKRG QDPHG copyAll WKDW DFFHSWV WZR DUUD\V RI LQWHJHUV DV LWV DUJXPHQWV DQG FRSLHV WKH HOHPHQW YDOXHV IURP WKH ILUVW DUUD\ LQWR WKH VHFRQG DUUD\ )RU H[DPSOH FRQVLGHU WKH IROORZLQJ FRGH int array1 = {2, 1, 4, 3, 6, 5}; int array2 = new int[6]; copyAll(array1, array2); $IWHU \RXU copyAll PHWKRG H[HFXWHV WKH DUUD\ array2 VKRXOG KDYH WKH FRQWHQWV ^ ` MXVW OLNH array1 <RX PD\ DVVXPH WKDW WKH VHFRQG DUUD\ LV DOZD\V DW OHDVW DV ODUJH LQ OHQJWK DV WKH ILUVW DUUD\ 423 $OVR WU\ ZULWLQJ D YDULDWLRQ RI WKLV PHWKRG WKDW RQO\ DFFHSWV RQH DUUD\ DV D SDUDPHWHU DQG UHWXUQV WKH FRSLHG DUUD\ ,QVLGH WKH PHWKRG FUHDWH D VHFRQG DUUD\ RI QXPEHUV WR KROG WKH FRS\ DQG UHWXUQ LW :ULWH D PHWKRG QDPHG copyRange WKDW WDNHV WZR DUUD\V D DQG D WZR VWDUWLQJ LQGH[HV L DQG L DQG D OHQJWK O DQG FRSLHV WKH ILUVW O HOHPHQWV RI D VWDUWLQJ DW LQGH[ L LQWR DUUD\ D VWDUWLQJ DW LQGH[ L $VVXPH WKDW WKH DUJXPHQWV YDOXHV DUH YDOLG WKDW WKH DUUD\V DUH ODUJH HQRXJK WR KROG WKH GDWD DQG VR RQ :ULWH D PHWKRG QDPHG mode WKDW UHWXUQV WKH PRVW IUHTXHQWO\ RFFXUULQJ HOHPHQW RI DQ DUUD\ RI LQWHJHUV $VVXPH WKDW WKH DUUD\ KDV DW OHDVW RQH HOHPHQW DQG WKDW HYHU\ HOHPHQW LQ WKH DUUD\ KDV D YDOXH EHWZHHQ DQG LQFOXVLYH %UHDN WLHV E\ FKRRVLQJ WKH ORZHU YDOXH )RU H[DPSOH LI WKH DUUD\ SDVVHG FRQWDLQV WKH YDOXHV ^ ` \RXU PHWKRG VKRXOG UHWXUQ +LQW <RX PD\ ZLVK WR ORRN DW WKH WDOO\ SURJUDP WR JHW DQ LGHD KRZ WR VROYH WKLV SUREOHP &DQ \RX ZULWH D YHUVLRQ RI WKLV PHWKRG WKDW GRHV QRW UHO\ RQ WKH HOHPHQWV EHLQJ EHWZHHQ DQG " :ULWH D PHWKRG QDPHG stdev WKDW UHWXUQV WKH VWDQGDUG GHYLDWLRQ RI DQ DUUD\ RI LQWHJHUV 6WDQGDUG GHYLDWLRQ LV FRPSXWHG E\ WDNLQJ WKH VTXDUH URRW RI WKH VXP RI WKH VTXDUHV RI WKH GLIIHUHQFHV EHWZHHQ HDFK HOHPHQW DQG WKH PHDQ GLYLGHG E\ RQH OHVV WKDQ WKH QXPEHU RI HOHPHQWV ,W V MXVW WKDW VLPSOH 0RUH FRQFLVHO\ DQG PDWKHPDWLFDOO\ WKH VWDQGDUG GHYLDWLRQ RI DQ DUUD\ D LV ZULWWHQ DV IROORZV )RU H[DPSOH LI WKH DUUD\ SDVVHG FRQWDLQV WKH YDOXHV ^ PHWKRG VKRXOG UHWXUQ DSSUR[LPDWHO\ ` \RXU :ULWH D PHWKRG QDPHG kthLargest WKDW DFFHSWV DQ LQWHJHU N DQG DQ DUUD\ D DV LWV SDUDPHWHUV DQG UHWXUQV WKH HOHPHQW VXFK WKDW N HOHPHQWV KDYH JUHDWHU RU HTXDO YDOXH ,I N UHWXUQ WKH ODUJHVW HOHPHQW LI N UHWXUQ WKH QG ODUJHVW HOHPHQW DQG VR RQ )RU H[DPSOH LI WKH DUUD\ SDVVHG FRQWDLQV WKH YDOXHV ^ ` DQG WKH LQWHJHU N SDVVHG LV \RXU PHWKRG VKRXOG UHWXUQ EHFDXVH WKHUH DUH YDOXHV DW OHDVW DV ODUJH DV DQG N D OHQJWK $VVXPH WKDW +LQW FRQVLGHU VRUWLQJ WKH DUUD\ RU VRUWLQJ D FRS\ RI WKH DUUD\ ILUVW :ULWH D PHWKRG QDPHG median WKDW DFFHSWV DQ DUUD\ RI LQWHJHUV DV LWV DUJXPHQW DQG UHWXUQV WKH PHGLDQ RI WKH QXPEHUV LQ WKH DUUD\ 7KH PHGLDQ LV WKH QXPEHU VXFK WKDW LI \RX DUUDQJHG WKH HOHPHQWV LQ RUGHU LW ZRXOG DSSHDU LQ WKH PLGGOH $VVXPH WKDW WKH DUUD\ LV RI RGG VL]H VR WKDW RQH VROH HOHPHQW FRQVWLWXWHV WKH PHGLDQ DQG WKDW WKH QXPEHUV LQ WKH DUUD\ DUH EHWZHHQ DQG LQFOXVLYH )RU H[DPSOH WKH PHGLDQ RI ^ ` LV DQG WKH PHGLDQ RI ^ ` LV +LQW <RX PD\ ZLVK WR ORRN DW WKH 7DOO\ SURJUDP IURP HDUOLHU LQ WKLV FKDSWHU IRU LGHDV 424 5HZULWH \RXU median PHWKRG VR WKDW LW ZRUNV UHJDUGOHVV RI WKH HOHPHQWV YDOXHV DQG DOVR ZRUNV IRU DUUD\V RI HYHQ VL]H 7KH PHGLDQ RI DQ DUUD\ RI HYHQ VL]H LV WKH DYHUDJH RI WKH PLGGOH WZR HOHPHQWV 8VH \RXU median PHWKRG IURP WKH SUHYLRXV H[HUFLVHV WR ZULWH D SURJUDP WKDW UHDGV D ILOH IXOO RI LQWHJHUV DQG SULQWV WKH PHGLDQ RI WKRVH LQWHJHUV :ULWH D PHWKRG QDPHG wordLengths WKDW DFFHSWV D 6WULQJ UHSUHVHQWLQJ D ILOH QDPH DV LWV DUJXPHQW <RXU PHWKRG VKRXOG RSHQ WKH JLYHQ ILOH DQG FRXQW WKH QXPEHU RI OHWWHUV LQ HDFK WRNHQ LQ WKH ILOH DQG RXWSXW D UHVXOW GLDJUDP RI KRZ PDQ\ ZRUGV FRQWDLQHG HDFK QXPEHU RI OHWWHUV )RU H[DPSOH LI WKH ILOH FRQWDLQV WKH IROORZLQJ WH[W Before sorting: 13 23 480 -18 75 hello how are you feeling today After sorting: -18 13 23 75 480 are feeling hello how today you <RXU PHWKRG VKRXOG SURGXFH WKH IROORZLQJ RXWSXW WR WKH FRQVROH 1: 2: 3: 4: 5: 6: 7: 8: 0 6 10 0 5 1 2 2 ****** ********** ***** * ** ** $VVXPH WKDW QR WRNHQ LQ WKH ILOH LV PRUH WKDQ FKDUDFWHUV LQ OHQJWK 3URJUDPPLQJ 3URMHFWV -DYD V W\SH LQW KDV D OLPLW RQ KRZ ODUJH RI DQ LQWHJHU LW FDQ VWRUH 7KLV OLPLW FDQ EH FLUFXPYHQWHG E\ UHSUHVHQWLQJ DQ LQWHJHU DV DQ DUUD\ RI GLJLWV :ULWH DQ LQWHUDFWLYH SURJUDP WKDW DGGV WZR LQWHJHUV RI XS WR GLJLWV HDFK 3HUVRQDO PDLOLQJ ODEHOV FDQ SURYH TXLWH XVHIXO :ULWH D SURJUDP WKDW UHDGV D ILYH OLQH DGGUHVV IURP DQ LQSXW ILOH DQG SURGXFHV DQ RXWSXW ILOH ZLWK WKH DGGUHVV UHSHDWHG WLPHV LQ WKUHH FROXPQV :ULWH D SURJUDP WKDW UHDGV DQ LQSXW ILOH RI QXPEHUV DQG UHSRUWV WKH DYHUDJH RI WKH QXPEHUV DV ZHOO DV KRZ IDU HDFK QXPEHU LV IURP WKH DYHUDJH :ULWH D SURJUDP WKDW SOD\V D YDULDWLRQ RI WKH JDPH RI 0DVWHUPLQG ZLWK D XVHU )RU H[DPSOH WKH SURJUDP FDQ XVH SVHXGRUDQGRP QXPEHUV WR JHQHUDWH D GLJLW QXPEHU 7KH XVHU VKRXOG EH DOORZHG WR PDNH JXHVVHV XQWLO WKH XVHU JHWV WKH QXPEHU FRUUHFW &OXHV VKRXOG EH JLYHQ WR WKH XVHU LQGLFDWLQJ KRZ PDQ\ GLJLWV RI WKH JXHVV DUH FRUUHFW DQG LQ WKH FRUUHFW SODFH DQG KRZ PDQ\ DUH FRUUHFW EXW LQ WKH ZURQJ SODFH 425 6WXDUW 5HJHV 0DUW\ 6WHSS 426 ...
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