{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

10-13 Class Notes CS 107

# 10-13 Class Notes CS 107 - 10-13 Class Notes CS 107...

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

Announcements: Questions? Last Time: Description of program #4, Idea of arrays Today: Syntax for arrays, Arrays examples Rather than having to store multiple values of the same type in different variables, we can store them in an array . This is appropriate when we have a group of values of the same type, such as a list of numbers, or a group of characters representing a playing board for a game. To declare an array, we use: type[ ] arrayName ; where type can be any type (e.g. char, int, float), arrayName is an identifier you make up, and size indicates how many items will be stored in the array. We then must initialize the array using: arrayName = new type[ size] ; For example int[ ] scores; scores = new int[ 10] ; declares and initializes an array called theScores that can hold 10 integers. In the array shown above the first element is the 0th element, and the last is the 9th element. This element number is known as the index (a.k.a. the subscript ) , which starts at 0 and goes to n-1 for an array of n elements. Note that the default value of 0 is stored in each of the 10 array positions at this point. Syntax for arrays: Store a value into an array by using the array name with the subscript on the left-hand side of an assignment statement. E.g.: scores[ 3] = 75; // store 75 into array element 3 (the 4th element) 1. Retrieve a value from an array by using the array name with the subscript anywhere a value is needed. E.g.: System.out.println( scores[ 1]); sum = sum + scores[ i]; // i is an integer between 0 and 10 2. You must make sure that you never read or write past the end of the array. The compiler will not stop you, but your program will generate an error and stop when you try to access an out-of-bounds element. 3. Arrays can be initialized element by element in a loop for (i=0; i<10; i++) { theScores[ i] = 0; } or by putting initial values in braces when the array is declared: int[ ] scores = {0,0,0,0,0,0,0,0,0,0}; In the above case the size is inferred from the number of initializing values. 4. Array Usage 10-13 Class Notes CS 107 Wednesday, October 13, 2010 1:00 PM 107 Fall 2010 Page 1

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

View Full Document
} or by putting initial values in braces when the array is declared: int[ ] scores = {0,0,0,0,0,0,0,0,0,0}; In the above case the size is inferred from the number of initializing values. Pasted from < http://logos.cs.uic.edu/Examples%20And%20Notes/notes/Java/ArraysAverageGrades/Arrays.htm > Sample programs using arrays (see code on web site) import java.io.*; import java.util.Scanner; class UsingAnArray { public static void main(String[] args) throws IOException { int[] scores; // declare the array scores = new int[3]; // initialize the array Scanner keyboard = new Scanner( System.in); // Scanner for keyboard input System.out.println( "Enter 3 scores to be averaged:"); scores[0] = keyboard.nextInt(); scores[1] = keyboard.nextInt(); scores[2] = keyboard.nextInt(); System.out.print( "The scores are: " ); System.out.println( scores[0] + ", " + scores[1] + ", " + scores[2]); System.out.print( "The average is: " ); System.out.println( (float)(scores[0]+scores[1]+scores[2])/3);
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

10-13 Class Notes CS 107 - 10-13 Class Notes CS 107...

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

View Full Document
Ask a homework question - tutors are online