

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Announcements: Homework on course web site โ due Friday while/for loop conversion Array Initialization Let's say that we have the following statement: String[] monthNames = new String[12]; What does this do? What are the values of the array elements? [Draw picture] What would we like them to be? How would we do this? monthNames[0] = "January"; monthNames[1] = "February"; monthNames[2] = "March"; and so on. This is kind of laborious. Java provides a shorthand way of declaring an array and initializing its values at the same time. String[] monthNames = {"January", "February", "March", "April", ..., "December" }; [Draw picture] Another example: int[] daysPerMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Note that this style we don't use new or a size โ Java figures that part out for us.
Comparison of ArrayLists and arrays ArrayLists are variable size, arrays are fixed ArrayLists store objects, arrays can store values of any type (including primitives like int) Declaration: ArrayList