Arrays

posted by: Ms. Martin 19 January 2010 No Comment

Think back to the Student class you wrote recently.  A student has a name as well as some test scores.  In the initial implementation we made, our Student objects could only keep track of up to 3 test scores.  Not very flexible, is it?  Beyond that, many of you noted that the code to set test scores was really redundant.

When dealing with multiple related values, arrays are the way to go.  You can think of them as lists of values that all have the same type.  For example, I could represent this class as a list or array of Strings each representing one of your names.  Similarly, we can represent a student’s score as a list or array of doubles representing test scores.

We can represent an array with the following diagram:

Read more about arrays in these excellent notes.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>