{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
Announcements: Questions? Example of Objects & Classes: Employee 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 a. Store a value Array Usage Arrays: 10-14 Class Notes CS 107 Friday, October 14, 2011 4:06 PM CS 107 Fall 2011 Page 1
Image of page 1

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

View Full Document Right Arrow Icon
a. Store a value scores[ 3] = 75; // store 75 into array element 3 (the 4th element)
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern