CSE 8B Chapter Notes - Chapter 7.1. 7.2, 7.5: Foreach Loop
Document Summary
Arrays are a type of data structure: fixed-size, sequential, contains elements of same type. An array has a fixed size once it is created. Array reference variables are used to access elements at a specific index. Declaring an array variable creates storage location for reference to an array arrayvarname = new elementtype[arraysize]; Assigns reference of newly created array to arrayvarname. Assign values to an array with: arrayvarname[index] = value. Arrayvarname. length returns the size of the array: assigned default values if not filled. Elements accessed through indices: 0-based start from 0 to arrayvarname. length 1. Syntax to declare, create, and initialize array in one line: elementtype[] arrayvarname = {val(cid:1005), val(cid:1006), val(cid:1007) vali}; Use a for loop to iterate through an array: an example of iterating through an array is printing it out to the console. Foreach loops allow programmers to iterate through an array without an index variable for(cid:894)ele(cid:373)e(cid:374)ttype e : arrayvarna(cid:373)e(cid:895) { }