Lecture 9

Lecture 9 - Lecture 9 Multidimensional Arrays 1 Review of Array Basics Declaring arrays Arrays are an indexed set of objects of the same type An

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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.
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/17/2012 for the course ECE 203 taught by Professor Robincarr during the Fall '07 term at Drexel.

Page1 / 18

Lecture 9 - Lecture 9 Multidimensional Arrays 1 Review of Array Basics Declaring arrays Arrays are an indexed set of objects of the same type An

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online