10-14 Class Notes CS 107

10-14 Class Notes CS 107 - b. Retrieve a value from an array

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

View Full Document Right Arrow Icon
Announcements: Questions? Last Time: Today: When you create a class (so that the "driver" code works), you will likely need: 1. Instance variables 2. get and set methods 3. toString (for nicely formatted output) 4. Constructors: default, fully-qualified, copy constructor 5. Some other method just to confuse you Sample midterm #2 exam: type[ ] arrayName ; where type can be any type (e.g. char, int, float), arrayName is an identifier you make up 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] ; Syntax for Array Declaration Array Usage Arrays: 10-14 Class Notes CS 107 Friday, October 14, 2011 4:06 PM CS 107 Fall 2011 Page 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
a. Store a value scores[ 3] = 75; // store 75 into array element 3 (the 4th element)
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: b. Retrieve a value from an array System.out.println( scores[ 1]); sum = sum + scores[ i]; // i is an integer between 0 and 10 c. 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. d. 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. Pasted from < https://sites.google.com/site/learnjav/java/arrays > Counting Characters from keyboard input Counting Characters from an input file Arrays Examples: CS 107 Fall 2011 Page 2...
View Full Document

This note was uploaded on 02/10/2012 for the course COP 2273 taught by Professor Lin during the Fall '11 term at University of Central Florida.

Page1 / 2

10-14 Class Notes CS 107 - b. Retrieve a value from an array

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

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