Lecture 4
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4760847/Java.png)
Fall 2018
Narges Norouzi
Recap
-
User Input
-
Conditionals
-
if-statement
-
Nested if-statements
-
switch-case statements
-
-
Loops
-
while loop
-
do-while loop
-
for loop
-
Break & Continue
-
Break:
- Jumps out of a block
-
Continue:
- Jumps to the beginning of the block
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789650/continue.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789651/breakExample.png)
Arrays
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4790171/httpatomoreillycomsourceoreillyimages1944187.png.jpg)
Array
-
Indexed sequence of values of the same type
-
Example:
-
52 playing cards in a deck
-
1 million characters in a book
-
130 students in this course sorted by their ID
-
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789955/arrayJava.png)
Many Variables of Same Type
Goal: 10 variables of type int
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789954/array2.png)
Many Variables of Same Type
Goal: 10 variables of type int
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789952/defining_array.png)
Declares, creates, and initializes values
Arrays in java
-
To make an array, declare, create and initialize it.
-
To access element 'i' of an array named ‘a’, use a[i].
-
Array indices start at 0.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4790167/r_798819_J15Wy.jpg)
Example
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789950/example1.png)
compact alternative
-
Declare, create, and initialize in one statement.
-
Default initialization: all numbers automatically set to 0.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789950/example1.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789949/example2.png)
Explicit Initialization
- Example
- Equivalent to
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789948/explicit1.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789953/explicit2.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789947/dot_product.png)
Demo
Dot product of two vectors
Length of the array
- We can get the length of the array using the length field:
- Can be used for iteration over array elements
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789944/length2.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789946/length1.png)
Consider
- Defined an array to reference a list of 100 integers
- Each element is initialized by default to 0
-
Two exceptions will be thrown here:
- -1 is not a valid index – too small
-
100 is not a valid index – too large
- IndexOutOfBoundsException
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789943/consider.png)
Break
Multi-dimensional Arrays
Two-dimensional array
-
Examples:
-
Table of grades for each student and assignment
-
Table of data for each experiment and outcome
-
-
Mathematical abstraction: Matrix
-
Java abstraction: 2D array
Two-dimensional arrays in java
-
Array Access: use a[i][j] to access element in row i and column j.
-
Zero-based indexing: Row and column indices start at 0.
2D array or Array of Arrays
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789942/multi-array.png)
Example
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789945/codearray.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789941/arrayTable.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/853852/images/4789940/multi-example.png)
Demo
Matrix Addition
Zybooks chapters we covered
- Chapter 4 part 4.9
- Chapter 5 up to and including 5.9
Copy of CMPS12A - Lecture 4
By Narges Norouzi
Copy of CMPS12A - Lecture 4
- 388