We have now seen how to store a list of values in arrays. We can use this knowledge to make organized programs with more complex variables.
Throughout the lesson, we have learned about:
- Creating arrays explicitly, using
{
and}
. - Accessing an index of an array using
[
and]
. - Creating empty arrays of a certain size, and filling the indices one by one.
- Getting the length of an array using
length
. - Using the argument array
args
that is passed into themain()
method of a class.
Instructions
In order to practice the skills you’ve learned throughout the lesson, we’re going to make and use some arrays in the main()
method of a new Classroom
class.
First, inside main()
, make a String
array called students
, and set it equal to the following students, in order:
Sade Alexus Sam Koma
Now, we’re going to store the averages on the most recent math test. Create an array called mathScores
and set it to an empty array of size 4. It should hold double
s.
Sade got a 94.5 on the test. Store this value in the 0th index of the mathScores
array.
Sam got a 76.8 on the test. Store this value in the appropriate spot in the mathScores
array.
Write a print statement to say:
The number of students in the class is <numStudents>.
with <numStudents>
replaced by the length of the students
array.
For example, if the array has 7 elements, the printout should read:
The number of students in the class is 7.