ࡱ> >@= ibjbj ..hhi$4f$;QQQQ,,, $ $ $ $ $ $ $&( $,, $QQ $7772QQ $7 $77:",w#Q׏l\# #6$0f$%#R`)`)w#w#6`)#H,@7N ZY,,, $ $,,,f$`),,,,,,,,, : Chapter 8: One Dimensional Arrays scalar variable is a single built-in data type value ex. char name; int i; double money; A single dimensioned array can be used for a set of data values all of the same type. Its possibly a column of information. The list of related values are all stored in a single group name in contiguous memory. All values are automatically initialized to zero (false or null for reference types). Prices 5.99 13.67 9.40 3.52 11.83 double prices[]; prices = new double[5]; or double prices[] = new double[5]; or final int MAX=5; double prices[]=new double[MAX]; element (or component) each item in an array index (or subscript) an elements position, starting at index 0. prices[0] ~ read as prices sub zero Loops are used to process arrays. There is a special FOR LOOP that can be used called the for/each loop. The  HYPERLINK "http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html" for-each loop is used with both collections and arrays. It is intended to simplify the most common form of iteration, where the iterator or index is used solely for iteration, and not for any other kind of operation, such as removing or editing an item in the collection or array. When there is a choice, the for-each loop should be preferred over the for loop, since it increases legibility // Method returns the sum of the elements of a int sum(int[] a) { int result = 0; for (int i : a) //read as for each element (i) in a result += i; return result; } //typical for-each loop //processes each item, without changing the collection or array. String[] names = {"Ethan Hawke", "Julie Delpy"}; for( String name : names ){ System.out.println("Name : " + name); Old loop: sum=0; for (i=0; i< prices.length;i++) sum=sum + prices[i]; New loop: sum=0; for (int price : prices) sum +=price; Use loops to input /output values of an array. Java bounds check Java checks the value of the index being used at run time. (ie. if array declared as 5 and use an index of 6 notified of ArrayIndexOutofBounds). Array object aggregate type - also referred to as a structured type and a data structure. also operations must be available for retrieving and updating individual values i.e. array of integers, related by their position, index provides a method of accessing and modifying. Example Programs. p 407 & 408 String Arrays String names[]=new String[7]; or String names[]; names= new String[7]; Arrays of reference types are stored differently than an array of built-in types. Declaration is the same. Allocation is different. When a string value is assigned, storage for that string is created and that address is stored in the array. Each string value may vary in length. When using built-in arrays, the actual values are stored directly in the array. Nice Picture p 411 Run-time Dimensioning s1 = JOptionPane.showInputDialog(Enter Grade); num = Integer.parseInt(s1); int grade[] = new int[num]; Array Initialization int grade[] = {98,78,100,95}; //array is sized according to the number of values in the //initialization Deep and Shallow Copies System.arraycopy() method System.arraycopy(source_array, source_start_index, target_array, target_start_index, number_of_elements_to_copy); i.e. System.arraycopy(nums,1,other,2,3); System.arraycopy(nums,0,other,0,nums.length); Target array must be allocated large enough or error will occurArrayIndexOutOfBounds Look at p419 Deep Copy element by element copy Shallow copy when an array assignment is executed ie. other = nums; This assignment statement copies the address stored in nums into the reference variable other. nums and other refer to the same array.(other is just an alias) Shallow copy used in redimensioning p423 #10. Arrays as Arguments - individual elements (passed by value) findMax(grades[2],price[6]); - complete array (passed by reference) { double units[]=new double[500]; calcTotal(units); } //in the method void calcTotal(double array[]) { //units and array are the same array } Examples p428 Pgm 8.10 P429 Pgm 8.11 Assignment: p 431 #6 (Parallel Arrays: Price, Qty, Amount) !"#)  _ f  Q U _ b c g h   > ? C K n ٰysymyssf_f h8:5\ h,5\ hE0J hE0JjhE0JUhEh8:hECJOJQJaJh,CJOJQJaJh{*>*CJOJQJaJh)h)>*CJOJQJaJh{*CJOJQJaJh)>*CJOJQJaJh)CJOJQJaJh&CJOJQJaJh)5>*CJOJQJaJ$"#XYhu   ) , = ^ _  b c ? @ o R")BVgd8:gdEgd,n w ~ QRdqs ()AB,MԵ~qeh=CJOJQJaJh8:>*CJOJQJaJh)CJOJQJaJh{*h8:CJOJQJaJh8:CJOJQJaJh8:h8:>*CJOJQJaJh,CJOJQJaJh8:5B* \phh8:B*phh8:B*phhEB*phhEB* phh8:hEh,B* phh,$VW,.LLMklz{/Hgd= & Fgd=^gd=Mjkly{-?@ABWXYB[ VZw|~۲zhj>*CJOJQJaJh>4hjCJOJQJaJhjhj6CJOJQJaJhjCJOJQJaJh>4>*CJOJQJaJh>4CJOJQJaJh=h=>*CJOJQJaJh=>*CJOJQJaJh=CJOJQJaJh}CJOJQJaJ/,-@BXYABZ[uMNgd=  *Gnp,-^gdja^agdjgdj`gd}gd=)+9ghiǻֻh8:h=CJOJQJaJh>4h>4CJOJQJaJh>4CJOJQJaJh}CJOJQJaJhjhjCJOJQJaJhjCJOJQJaJhj>*CJOJQJaJhjhj>*CJOJQJaJ -higd=,1h/ =!"#$% ^ 666666666vvvvvvvvv666666>6666666666666666666666666666666666666666666666666hH6666666666666666666666666666666666666666666666666666666666666666662 0@P`p2( 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p8XV~_HmH nH sH tH @`@ NormalCJ_HaJmH sH tH DA`D Default Paragraph FontRiR  Table Normal4 l4a (k (No List e@ ,HTML Preformatted7 2( Px 4 #\'*.25@9CJOJQJ^JaJNg@N EHTML TypewriterCJOJPJQJ^JaJ4U@4 E Hyperlink >*phPK![Content_Types].xmlj0Eжr(΢Iw},-j4 wP-t#bΙ{UTU^hd}㨫)*1P' ^W0)T9<l#$yi};~@(Hu* Dנz/0ǰ $ X3aZ,D0j~3߶b~i>3\`?/[G\!-Rk.sԻ..a濭?PK!֧6 _rels/.relsj0 }Q%v/C/}(h"O = C?hv=Ʌ%[xp{۵_Pѣ<1H0ORBdJE4b$q_6LR7`0̞O,En7Lib/SeеPK!kytheme/theme/themeManager.xml M @}w7c(EbˮCAǠҟ7՛K Y, e.|,H,lxɴIsQ}#Ր ֵ+!,^$j=GW)E+& 8PK!Ptheme/theme/theme1.xmlYOo6w toc'vuر-MniP@I}úama[إ4:lЯGRX^6؊>$ !)O^rC$y@/yH*񄴽)޵߻UDb`}"qۋJחX^)I`nEp)liV[]1M<OP6r=zgbIguSebORD۫qu gZo~ٺlAplxpT0+[}`jzAV2Fi@qv֬5\|ʜ̭NleXdsjcs7f W+Ն7`g ȘJj|h(KD- dXiJ؇(x$( :;˹! I_TS 1?E??ZBΪmU/?~xY'y5g&΋/ɋ>GMGeD3Vq%'#q$8K)fw9:ĵ x}rxwr:\TZaG*y8IjbRc|XŻǿI u3KGnD1NIBs RuK>V.EL+M2#'fi ~V vl{u8zH *:(W☕ ~JTe\O*tHGHY}KNP*ݾ˦TѼ9/#A7qZ$*c?qUnwN%Oi4 =3ڗP 1Pm \\9Mؓ2aD];Yt\[x]}Wr|]g- eW )6-rCSj id DЇAΜIqbJ#x꺃 6k#ASh&ʌt(Q%p%m&]caSl=X\P1Mh9MVdDAaVB[݈fJíP|8 քAV^f Hn- "d>znNJ ة>b&2vKyϼD:,AGm\nziÙ.uχYC6OMf3or$5NHT[XF64T,ќM0E)`#5XY`פ;%1U٥m;R>QD DcpU'&LE/pm%]8firS4d 7y\`JnίI R3U~7+׸#m qBiDi*L69mY&iHE=(K&N!V.KeLDĕ{D vEꦚdeNƟe(MN9ߜR6&3(a/DUz<{ˊYȳV)9Z[4^n5!J?Q3eBoCM m<.vpIYfZY_p[=al-Y}Nc͙ŋ4vfavl'SA8|*u{-ߟ0%M07%<ҍPK! ѐ'theme/theme/_rels/themeManager.xml.relsM 0wooӺ&݈Э5 6?$Q ,.aic21h:qm@RN;d`o7gK(M&$R(.1r'JЊT8V"AȻHu}|$b{P8g/]QAsم(#L[PK-![Content_Types].xmlPK-!֧6 +_rels/.relsPK-!kytheme/theme/themeManager.xmlPK-!Ptheme/theme/theme1.xmlPK-! ѐ' theme/theme/_rels/themeManager.xml.relsPK] i.n Mi  V-igiX8@0(  B S  ?L+6k9k?*urn:schemas-microsoft-com:office:smarttags stockticker ȁ$nqrs257?orwzz.1)^ y [ k u  / VZ~*1 #&`gk!#)Y[nq{ ')+,1=C_fcorRY"&),ILNRM ^ ^ z [ l u  0 ~*2IQpv-hk33333333333333333333333333333333333333333333333333333333333!h"ABV.KM a d g j k l { - < ? @ B W B Z [ t      "*+,-9ghka d g j < ?      "*+9gk2̀ Us aa^a`OJPJQJ^Jo(-1 1 ^1 `OJQJ^Jo(hHo^`OJQJo(hH^`OJQJo(hH^`OJQJ^Jo(hHoqq^q`OJQJo(hHAA^A`OJQJo(hH^`OJQJ^Jo(hHo^`OJQJo(hH aa^a`OJPJQJ^Jo(-1 1 ^1 `OJQJ^Jo(hHo^`OJQJo(hH^`OJQJo(hH^`OJQJ^Jo(hHoqq^q`OJQJo(hHAA^A`OJQJo(hH^`OJQJ^Jo(hHo^`OJQJo(hH U2        ƫR        M0=4,Ej}`ik@ix@UnknownG* Times New Roman5Symbol3. * ArialCNComic Sans MS?= * Courier New;WingdingsACambria Math"qhb̦b̦ s s !24aa2QHX ?`2!xx!Chapter 8: One Dimensional Arrays Carolyn Bellaceratestuser  Oh+'0 ,8 X d p|$Chapter 8: One Dimensional Arrays Carolyn Bellacera Normal.dotm testuser2Microsoft Office Word@e@l\@l\s ՜.+,D՜.+,L hp|   a "Chapter 8: One Dimensional Arrays Title 8@ _PID_HLINKSAxm@http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html  !"#$%&'()*+,./012346789:;<?Root Entry Fl\A1Tablet)WordDocument..SummaryInformation(-DocumentSummaryInformation85CompObjy  F'Microsoft Office Word 97-2003 Document MSWordDocWord.Document.89q