Docsity

While/For Loop Conversion - Lecture Notes | CSIS 110, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 - Lecture 19
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.
pf3

Partial preview of the text

Download While/For Loop Conversion - Lecture Notes | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 19

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 myList; String[] myArray; Creation: myList = new ArrayList(); myArray = new String[ 10 ]; Note that ArrayList is empty, array has 10 elements whose values are null. We haven't put any Strings in either one yet. Accessing Elements: myList.get(0) myArray[0] Inserting an element in an array list: myList.add( "fried fish" ) or myList.add( 3, "fried fish" ); Don't insert with arrays โ€“ can set value of array element instead: myArray[ 3 ] = "fried fish"; Note that this overwrites whatever value was there before! Storing a value: String s = "Happy Birthday"; myList.add( index, s ); // put at index, move over myArray[ index ] = s; // put at index, overwrite Number of elements: myList.size() -- with parens myArray.length -- no parens! Important โ€“ ArrayList size() method tells us how many items are in the list. array.length field tells us how many elements are in the array, but does not tell us how many of them we are using! If our application does not use all of the array elements, we need to keep track of the size separately. How would we do this?