{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 9

# Lecture 9 - Lecture 9 Multidimensional Arrays 1 Review of...

This preview shows pages 1–4. Sign up to view the full content.

Page 1 of 18- Lecture 9: Multidimensional Arrays 1. Review of Array Basics Declaring arrays • Arrays are an indexed set of objects of the same type. • An array can be declared and created in one of the following ways: 1. int row[] = new int [ 5 ]; 2. int [] row = new int [ 5 ]; 3. int [] row; row = new int [ 5 ]; 4. String [] day = new String [ 7 ]; Initializing arrays • An array can be initialized in one of the following ways: 1. for ( int i = 0; i < 5; i++ ) row[i] = (i+1)*(i+1); 2. String [] day = { "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su" }; 3. int [] row = { 1, 4, 9, 16, 25 }; Note the difference on where square brackets must be used and where curly brackets must be used. Note the first element in an array has the index zero and not one. Example 4 declares a new array containing 7 String variables. The value of its only instance variable length is thus 7 and its base type is String. The for loop initializes an array of squares: { 1, 4, 9, 16, 25 } All these methods use the keyword new to create an array. Use square brackets to declare arrays. Examples 1, 2 and 3 all declare a new array of 5 integers named row. The array row has the single instance variable 5, which is its length . Array names are typically singular , since most calculations are performed on one array element at a time.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Page 2 of 18- Chimera Example: Enhanced or counter-based for statement? Find the maximum number in an array of integers and display the progressive candidates for the maximum as they are found. public class MaxOfArray { public static void main(String[] args) { int[] myData = { 2, 3, -6, 8, 3, 5, 0, 8, 6, 8, 12, 3 }; // Display all the points in the array, 4 per row. System.out.println( "\nThe array contains these points."); int index = 0; for ( int number : myData) { if(index%4==0) System.out.printf( "\n\t%+4d", number); else System.out.printf( "\t%+4d", number); index++; } // end of for block which prints array // Begin searching for the maximum and report each new candidate for the max. int max = myData[0]; index =0; System.out.println("\n\nNow searching for the maximum."); System.out.printf("\tLargest found so far is %3d at index %3d" , max, index); index = -1; for ( int number : myData) { index++; if ( number > max ) {max = number; System.out.printf("\n\tLargest found so far is %3d at index %3d" , max, index); } // end of if block } // end of for loop which searches for maximum. System.out.println( "\n\nSearch Finished!"); System.out.printf( "The largest number in the array is %d\n\n", max ); } //end of main method } //end of class Both these for loops work, but are curious chimera. They can't decide if they are enhanced for loops or counter-based loops. If you find yourself referencing array indices like this, it is probably better to use a counter-based for loop instead of the enhanced loop shown.
Page 3 of 18- Program Output: Example: Variable-length arguments Most methods require the number of arguments to be precisely specified. Below, only the middle use of the method Math.max() with the required two arguments is OK.

This preview has intentionally blurred sections. Sign up to view the full version.

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

{[ snackBarMessage ]}