U3: Arrays
No description yet. This curated deck is ready for daily study.
Card preview (17)
Array: Cells
• Has index starting from 0 and increment by 1 • Index is reference to location of item
Array: Storing Data
• Can only store data all of same type • Ex: Integers, Strings
Array: Names
• Must be done same way as variables • Camel notation if name has +2 words • Ex: studentMarks, cardValues
Array: Size
Programmer should know and state (vertical) size of array before using
Declaring Arrays
• Declare name, size and type of data stored • Ex: int [ ] exampleName = new int [6]; for 0 to 5
Initializing Arrays
int [ ] values = new int [2]; values [0] = 3; values [1] = 71;
Declaring and Initializing Arrays at Same Time
Double [ ] avg = {34.7, 54.3, 8.9}
Storing Inputs In Array
• int [ ] numbers = new int [1]; • numbers[0] = keyedInput.nextInt();
Operations on Array Number Values
Possible
Outputting Array Values
• Can only output one value at time • Ex: System.out.println(values[0]); System.out.println(values[1]);
Element
Actual data stored in array cell
Index
Location/index value where element located
Bounds
Highest and lowest indexes (0 to [size - 1])
i++ and i–
• i++: i = i + 1 • i–: i = i - 1
Finding Odd and Even Values
• Odd: variable % 2 = 1 • Even: variable % 2 = 0
Creating New List Based From Old List
• int[ ] List = new int[5]; • int[ ] List2 = List;
Number of things in an array
variable.length