# L13 - Computer Science 1 1 SS C Lecture 13 Contents 2 Why...

This preview shows pages 1–3. Sign up to view the full content.

CompSci 101 Lecture 13 1 Computer Science 1 1 SS C Lecture 13 Contents Why do we need arrays? Declaring an array. Creating the array space. Assigning to individual elements of an array. Processing the elements of the array. Coursebook: §14 2 Why do we need arrays? Let's say I want to store the bank balance amount for every student in this class. To calculate the total of the Frst four bank balances? int bank01 = 2000; int bank01 = 231; int bank03 = 21; int bank04 = -3000; …. int total = bank01 + bank01 + bank03 + bank04; To calculate the total of all the bank balances? int total = bank01 + bank01 + bank03 + bank04 bank05 + bank06 + ….; What a pain! 3 Array data structure An array is a sequence of variables all of the same type. Each variable (known as an element of the array) can be accessed using its index number. Each variable in an array has an index number (its position in the array). The index number always starts at 0. 4 Analogy 8172 Green St 3 / 156 Green St singleHome manyHomes 0 1 2 3 4 e.g., manyHomes[3] singleHome 5 One dimensional arrays • holds a sequence of variables of the same type An array: • each element in the array is associated with an index value • index values start at 0 • every array has a length Feld which stores the number of elements in the array • arrays are objects 6 Arrays of different types In this lecture we are discussing arrays of primitive types (int, boolean, char, double).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CompSci 101 Lecture 13 2 7 Declaring an array variable When using arrays we use square brackets [ ] int [] balances; boolean [] isSingle; double [] gPAs; In this lecture we are discussing arrays of primitive types (int, boolean, char, double). balances isSingle gPAs . . . Note: the above declarations do not set aside any space for the elements of the arrays After the declarations, these variables store the value null (which means they don't point to anything).
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 5

L13 - Computer Science 1 1 SS C Lecture 13 Contents 2 Why...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online