Join Peggy Fisher for an in-depth discussion in this video Revisiting arrays, part of Learning Java 8.
- In the segment on data types, we introduced the concept of arrays. Now we will expand on this concept and review a few more examples. What if you wanted to write a program to find the average height of all students in a classroom? To make the program more flexible, we will ask the user to enter how many students are in the class ahead of time, and then use that value as the size of the array. After all heights have been entered, we can use a loop to find the height of the tallest student and the average height for all students in the class.
Let's give it a try. Since we're asking the user to enter the heights, I need to add, on line seven, my import statement, import Java.util.scanner. Now I'll go ahead and create a scanner object on line 18. Okay. The first thing I wanna do is, find out how many students are in the class. Let's declare some variables. We know that you can't have half a student, so the data type for the number of students should be an int. Next we wanna create an array that will hold all of the students.
Since I don't know how many students there are yet, let's go ahead and create the array variable name, but we won't instantiate it until we ask the user how many students are in the class. This array is going to be a double. Remember, to declare an array, you start out with the data type, then an open and closed square bracket, and the name of the array, in our case, heights. Now I'll ask the user to tell me how many students are in the class. I'll read that value into my numStudents variable.
I use in.nextInt, because I'm expecting her to enter an integer. Now I can instantiate my heights array. heights = new. Remember, new is how we instantiate an array or an object. I'll use the numStudents as the value for how many array elements I should have. Now I need a loop to go through and get the heights of each student. Since we know how many students there are, I'll use a for loop.
Inside the loop, I will ask for the height of each student and read that variable right into my array. I use the i value from my for loop as the index value for my array. At this point, the user has entered in all the values for all the students in the class. Now I want to identify the value of the tallest student in the class. This is a common algorithm that you'll see often in programming. I'm gonna start by declaring a double variable that will hold the maxHeight.
Since I don't know who is the tallest yet, I'm gonna go ahead and set it equal to the very first value in my heights array, at position zero. Don't forget, arrays start at position zero. Now I wanna compare that height to all the other heights in the array. Again, I'm gonna use another for loop. This time, I'm gonna start i out at one, since I know I have the height of the very first element in the array. Inside the for loop, I'll compare the maxHeight and see if that is less than the height of this element in the array.
If it is, that means I have a new maxHeight. So I'll reset my maxHeight equal to the value in the array. Okay. When I'm all done, the maxHeight variable will have the maxHeight of the tallest student in the class. You might have noticed that I did not use curly braces to enclose the one line of code after the if statement. If you are only executing one line of code after an if statement or an else statement, you do not need curly braces.
If I wanted to execute more than one statement, then curly braces are required. The last thing I wanna do is to add up all the heights so I can find the average height. Let's do one more for loop. I almost forgot to add a variable to keep track of the total heights. There we go. All right. Now, each time through, I'll add the height from the array to my total. I can abbreviate that as total += heights[i].
Since I'm starting at zero and I'm going until the end of the array, that will add all the elements together. Okay, I have all the information I need. Now I wanna print out the average height and the maxHeight. I'll start with "the tallest student is:." I'll add the maxHeight, and let's go ahead and say "inches." Right below that, I'll print out the average. "The average height "of all students is" ... Whoop. I noticed I don't have a capital T here.
Let's be consistent. There we go ... "Is," plus this time, I need to do a calculation. I need to take the total, need to divide it by the number of students that were in the class. Since the total is a double value because it's the sum of all the heights, I don't have to worry about dividing it by an integer value, numStudents. I'll still get the decimal points because a double divided by an integer will give me a double. Okay. Now let's add the word "inches" at the end.
Now we can give it a try. This is an example where I use an array of type double to keep track of all the heights of the students, and then use that array to find the tallest student and the average of all students. Let's go ahead and run the program. I'm gonna say that we only have five students, and I'm gonna put in some heights in inches. I'll start with 60 inches, 59, 70, 65, and 52. You can see the tallest student in the class was 70 inches.
That's right. And the average height of all the students is 61.2 inches. So it appears that the program's working correctly. Arrays are often used to hold a list of values, such as a list of names, a list of numbers, or even a list of prices. Remember to be careful when choosing the size for your array because arrays always have a fixed size.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs