ࡱ> jli " bjbj 0\uu"     l<z;;;;;;;$>$Ax; ;  ;zzz*  ;z;zz6:@gfw9Ns;;0<9AzA:A :z;;z<A : Computer Programming II Instructor: Greg ShawPRIVATE  COP 3337 Intro to the Java Array I. Terminology data structure: a bunch of related memory locations for storing related values array: a fixed-size data structure occupying consecutive memory locations. Stores related values of a given primitive type or Java class (from the Java Library of programmer-defined). I.e., a list of related values ArrayList: a Java class that implements an abstract, object-oriented, user-friendly, variable-sized list. Although ArrayList objects are implemented as arrays, users of the ArrayList class may remain blissfully unaware of this. This is an excellent example of the OOP principle of information hiding array elements: the components of an array, each containing a value and occupying one memory location. Each array element is just like a non-array variable (aka: scalar variable) except for the name, which requires a subscript (aka: index) For this reason array elements are sometimes called subscripted variables a familiar concept in math and science, although the notation is different math/science: x0, x1, x2, ..., xn Java: x[0], x[1], x[2], ..., x[n] array index: an integer expression that tells you which element (i.e., the position of the element in the list). Aka: an array subscript Array index expressions must be enclosed in square brackets (see above) As with an ArrayList, the index of the first element is always 0. Advantages of Arrays (vs. ArrayLists) Prior to Java 1.5 (aka: Java 5), arrays were less cumbersome when working with lists of primitive types. (This was remedied in 1.5 with autoboxing and autounboxing.) Easier to implement multi-dimensional arrays (tables, etc) Every serious computer language in the galaxy has an array and they all work the same way Disdvantages of Arrays (vs. ArrayLists) Arrays are fixed-size, so may become full. (Although they may be resized easily, this is not done automatically as with ArrayLists) Arrays may be partially filled (i.e., not all the elements declared may actually be used). This requires the programmer to maintain a count of the number of elements actually used (see picture on board) More cumbersome insertions When a new value is to be stored in an array, you must first check to see that the array is not full. If it is, you must resize it. Then, all values at indices greater than or equal to the index where the new value goes will have to be moved down to make room for it (unless appended to the end of the array). And dont forget to increment the counter! This is exactly what is done in ArrayList method add(index,object) More cumbersome deletions When a value is removed from an array, all values at greater indices must be moved up to fill the hole. And dont forget to decrement the counter! This is exactly what is done in ArrayList methods remove(index) and remove(object) Accessing the Individual Elements of an Array We access an element of an array via index (aka: subscript) notation: list[index] list is the name of the array object variable index is an int expression that tells you which element (note the square brackets around the index expression) Each array element is used exactly like a scalar variable. Only the name is different, in that it requires a subscript. Here is a loop that traverses an array of exactly count elements (i.e., elements 0 through count - 1): for (int current = 0 ; current < count ; current++) { // do something here with array[current] } Declaring Array Object Variables and Creating Array Objects type [] name = new type[number-of-elements] ; type is the type of data stored in the array (may be any primitive type or class) name is the name of the array object variable number-of-elements is an integer expression indicating the array's size (i.e., number of elements). Examples: int [] scores = new int[35] ; // holds up to 35 int values boolean [] seatAvailable = new boolean[size] ; // holds up to size booleans (size is an int variable) Rectangle [] list = new Rectangle[number] ; // holds up to number Rectangle object variables Java Arrays are objects and - as with objects of any class - the object variable declaration and the creation of the actual object may be done separately. E.g., BankAccount [] customerList ; . . . customerList = new BankAccount[1000] ; "Overflowing the Bounds of an Array" - a Very Common Error Sililar to ArrayLists, referencing an array element that does not exist (i.e. an index less than 0 or greater than array.length1) will throw an ArrayIndexOutOfBoundsException. (For an ArrayList, it would be an IndexOutOfBoundsException) The length Instance Variable Every array object created has an instance variable called length which stores the size (i.e., number of elements) of the array. Instance variable length is commonly used in a loop when you want to "visit" every element of an array: Example: // create a 10-element array double list [] = new double[10] ; // fill array with first 10 powers of 2 for (int i = 0 ; i < list.length ; i++) { list[i] = Math.pow(2,i) ; } Note that length tells us the capacity of the array (i.e. the number of elements in the array). Unlike ArrayList method size(), it does not tell us the number of elements actually used (i.e. the number of values stored). So, if an array may be only partially filled, it is the programmers responsibility to keep count of the number of elements used. See PartiallyFilled.java Alternate Notation for Array Declarations Another way to declare an array is to specify the initial values stored instead of the size. Java infers the size from the number of initializers provided. Examples: int [] lotto = { 14, 21, 28, 35, 42, 49 } ; String [] colors = { Yellow, Magenta, Cyan } ; BankAccount [] accounts = { new BankAccount(1111111, 15000), new BankAccount(2222222, 20000), new BankAccount(3333333, 12500), new BankAccount(4444444, 37000) } ; These arrays have 6, 3, and 4 elements, respectively 45=>?@HTV_bchmnpɶ~~ncVVLhCJOJQJhvZ56CJO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$P*CJOJQJh#th$P*6CJOJQJh$P*hs(CJOJQJh$P*h W5CJOJQJh$P*h <CJOJQJh$P*h WCJOJQJh~shvZCJOJQJh~shL| CJOJQJh~sh_6CJOJQJh~sCJOJQJh~shvZCJOJQJhpfCJOJQJh~sh=X]CJOJQJ Cr߸ߠwj\N= h=h$P*CJOJQJ^JaJh$P*CJOJQJ^JaJh=CJOJQJ^JaJh~sh=CJOJQJh~sh <6CJOJQJh~shDN6CJOJQJh~sh <CJOJQJh#tCJOJQJh~sh W>*CJOJQJh~sh W6CJOJQJhs(6CJOJQJhs(hs(6CJOJQJhs(CJOJQJh$P*CJOJQJh~sh WCJOJQJ"#>i" $ 0h*$^ha$gd~s($ & F % 0hp @ |*$^`|a$gd= 0*$^gd_637Ujpqv"#$/03:=CD ! " ޷ѪћћюѪh#tCJOJQJh~shhCJOJQJh#thACJOJQJaJh~shSCJOJQJh~sh$P*CJOJQJh~sh=CJOJQJh~shACJOJQJh$P*CJOJQJh=CJOJQJaJh=5CJOJQJ2.:pdF/ =!"#`$`% TD phoenix6G^ 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 8`8 Normal_HmH sH tH P@P  Heading 1$$*$@&a$56CJ OJQJDA D Default Paragraph FontViV  Table Normal :V 44 la (k (No List PK![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}#Ր ֵ+!,^$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JQJo(hH^`OJQJ^Jo(hHopp^p`OJQJo(hH@ @ ^@ `OJQJo(hH^`OJQJ^Jo(hHo^`OJQJo(hH^`OJQJo(hH^`OJQJ^Jo(hHoPP^P`OJQJo(hH0^`056.^`OJQJo(hH^`OJQJ^Jo(hHopp^p`OJQJo(hH@ @ ^@ `OJQJo(hH^`OJQJ^Jo(hHo^`OJQJo(hH^`OJQJo(hH^`OJQJ^Jo(hHoPP^P`OJQJo(hH^`56.0^`056.;@  ^@ `5679;<CJ8H*OJQJS*TXaJ8o(hHFxx^x`OJQJ^Jo(hHoHH^H`OJQJo(hH^`OJQJo(hH^`OJQJ^Jo(hHo^`OJQJo(hH^`OJQJo(hHX X ^X `OJQJ^Jo(hHo(#(#^(#`OJQJo(hHhh^h`CJ OJQJo(l0^`056.0^`056.$N6TO5l*-R&8S,7+ykRSS)R)mLVjW)0DhTzn<@!BukQYs8)wl|p~_R=Q28tU/$Nv}y0J#QZQQ5M0M0i [YG$$T֘e        VJVJ       pD        VJ        4p        %$L| CS$P*-1?5 <DNvZ=X].a~s#tR{2a W_6pf=CW@}Gs(+CfvsAh7[dFx93G"$@_"x@Unknown G*Ax Times New Roman5Symbol3. *Cx Arial?= *Cx Courier New7.@Calibri71 Courier;WingdingsSMonotype SortsSymbolARZapfDingbatsA$BCambria Math"qhӬ!GRksI + +24 3QHX?C2! xxProgramming in Visual BasicThe KingGreg$                           ! " # Oh+'0  4 @ L Xdlt|Programming in Visual Basic The KingNormalGreg7Microsoft Office Word@2 @ M)@JY*@z9[՜.+,0 hp  Dell Computer Corporation+  Programming in Visual Basic Title  !"#$%&'()*+,-.012345689:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXZ[\]^_`bcdefghkRoot Entry F gmData /1Table78BWordDocument0\SummaryInformation(YDocumentSummaryInformation8aCompObjy  F'Microsoft Office Word 97-2003 Document MSWordDocWord.Document.89q