From 9e842923be36cf8ee0287a01db0ee2ff0817d6e2 Mon Sep 17 00:00:00 2001 From: James Su Date: Fri, 6 Sep 2019 16:26:29 +0000 Subject: [PATCH] Update Class_1.md --- Grade 10/ICS4U1/Class_1.md | 60 +++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/Grade 10/ICS4U1/Class_1.md b/Grade 10/ICS4U1/Class_1.md index e9b331b..3233c09 100644 --- a/Grade 10/ICS4U1/Class_1.md +++ b/Grade 10/ICS4U1/Class_1.md @@ -1,4 +1,4 @@ -# Class 1 notes +# Class 1 Notes # Array Review @@ -8,6 +8,8 @@ - Each **cell** holds a value - All the values are the same **type** - It can store all kinds of data, `int`, `double`, `object`, etc, the type stored can be primitive (`int`) or reference (`object`) +- Once an array has been created, the number of cells **cannot** be changed. + ## Example @@ -36,3 +38,59 @@ +- The array name is `bowlingScores`, you can access specific value at a specific index: eg. `bowlingScores[3]` = 138. +- If there are $`n`$ cells in the array the indexes will be from $`0`$ to $`n-1`$, arrays are 0-index based. +- If we are acessing a value out of the range of the array, such as `bowlingScores[8]`, then we get an `ArrayOutOfBounds Error`. + +## Initializing Arrays +- General syntax to declare an array: + +```java +final int MAX = 8; +int [] bowlingScores= new int[MAX]; +bowlingScores[0] = 180; // initializing +bowlingScores[1] = 120; // initializing +... +``` +- To intialize all the values at once, we can do: + +```java +int[] luckyNumbers = {2, 3, 10, 4, 17, 21}; // known values +``` + +## Summing Values In An Array + +### In Main +```java +int sum; // declare an accumulator +sum = 0; +for(int i=0; i