1 / 15

MSIS 6 55 Advanced Business Applications Programming

Week 7 Arrays. MSIS 6 55 Advanced Business Applications Programming. 7 . 1. Arrays. Data structures Related data items of same type Remain same size once created Fixed-length entries Group of variables Have same type Reference type. Fig. 7.1 | A 12-element array. Index.

russj
Download Presentation

MSIS 6 55 Advanced Business Applications Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 7 Arrays MSIS 655Advanced Business Applications Programming 7.1

  2. Arrays • Data structures • Related data items of same type • Remain same size once created • Fixed-length entries • Group of variables • Have same type • Reference type

  3. Fig. 7.1 | A 12-element array.

  4. Index • Also called subscript • Position number in square brackets • Must be positive integer or integer expression • First element has index zero a = 5; b = 6; c[ a + b ] += 2; • Adds 2 to c[ 11 ] • c.length accesses array c’s length

  5. Declaring and Creating Arrays • Arrays are objects that occupy memory • Created dynamically with keyword new int c[] = newint[ 12 ]; • Equivalent toint c[]; // declare array variable c = newint[ 12 ]; // create array • We can create arrays of objects too String b[] = new String[ 100 ];

  6. Using an array initializer Use initializer list • Items enclosed in braces ({}) • Items in list separated by commas int n[] = { 10, 20, 30, 40, 50}; • Creates a five-element array • Index values of 0, 1, 2, 3, 4 • Do not need keyword new

  7. Enhanced for Statement • New feature of J2SE 5.0 • Allows iterates through elements of an array or a collection without using a counter • Syntax for ( parameter : arrayName ) statement

  8. Multidimensional Arrays • Multidimensional arrays • Tables with rows and columns • Two-dimensional array • m-by-n array

  9. Multidimensional Arrays (Cont.) • Declaring two-dimensional array b[2][2] int b[][] = { { 1, 2 }, { 3, 4 } }; • 1 and 2 initialize b[0][0] and b[0][1] • 3 and 4 initialize b[1][0] and b[1][1] int b[][] = { { 1, 2 }, { 3, 4, 5 } }; • row 0 contains elements 1 and 2 • row 1 contains elements 3, 4 and 5 • Lengths of rows in array are not required to be the same

  10. Multidimensional Arrays (Cont.) • Creating two-dimensional arrays with array-creation expressions • Can be created dynamically • 3-by-4 array int b[][]; b = newint[ 3 ][4 ]; • Rows can have different number of columns int b[][]; b = newint[ 2 ][ ]; // create 2 rows b[ 0 ] = newint[ 5 ]; // create 5 columns for row 0 b[ 1 ] = newint[ 3 ]; // create 3 columnsfor row 1

  11. Lab activities (Week 7) • Exercises (pp. 342-344)7.10, 7.11, 7.12, 7.17

More Related