![Page 1: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/1.jpg)
Lecture 3: Loops, Arrays
![Page 2: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/2.jpg)
Outline
Conditionals
Loops
Arrays
ArrayLists. Maps
![Page 3: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/3.jpg)
if statement
if (CONDITION) { STATEMENTS
}
![Page 4: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/4.jpg)
public static void test(int x) { if (x > 5) {
System.out.println(x + " is > 5"); }
}
public static void main(String[] arguments) { test(6); test(5); test(4);
}
![Page 5: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/5.jpg)
Comparison operators
x > y: x is greater than y x < y: x is less than y x >= y: x is greater than or equal to x x <= y: x is less than or equal to y
x == y: x equals y ( equality: ==, assignment: = )
![Page 6: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/6.jpg)
Boolean operators
&&: logical AND ||: logical OR
if ( x > 6 && x < 9) { if (x > 6) { …if (x < 9) {
}… }
}
![Page 7: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/7.jpg)
else
if (CONDITION) { STATEMENTS
} else { STATEMENTS
}
![Page 8: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/8.jpg)
public static void test(int x) { if (x > 5) {
System.out.println(x + " is > 5"); } else {
System.out.println(x + " is not > 5"); }
}
public static void main(String[] arguments) { test(6); test(5); test(4);
}
![Page 9: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/9.jpg)
else if
if (CONDITION) { STATEMENTS
} else if (CONDITION) { STATEMENTS
} else if (CONDITION) { STATEMENTS
} else { STATEMENTS
}
![Page 10: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/10.jpg)
public static void test(int x) { if (x > 5) {
System.out.println(x + " is > 5"); } else if (x == 5) {
System.out.println(x + " equals 5"); } else {
System.out.println(x + " is < 5"); }
}
public static void main(String[] arguments) { test(6); test(5); test(4);
}
![Page 11: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/11.jpg)
Outline
Conditionals
Loops
Arrays
ArrayLists. Maps
![Page 12: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/12.jpg)
Loops
Loop operators allow to loop through a block of code.
There are several loop operators in Java.
![Page 13: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/13.jpg)
The while operator
while (condition) {
statements
}
![Page 14: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/14.jpg)
The while operator
int i = 0;
while (i < 3) {
System.out.println(“Rule #“ + i);
i = i+1;
}
Count carefully
Make sure that your loop has a chance to finish.
![Page 15: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/15.jpg)
The for operator
for (initialization;condition;update){
statements
}
![Page 16: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/16.jpg)
The for operator
for (int i = 0; i < 3; i=i+1) {
System.out.println(“Rule #“ + i);
}
Note: i = i+1 may be replaced by i++
![Page 17: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/17.jpg)
S
}
Branching Statements break terminates a for or while loop
break;
ystem.out.println(“Rule #” + i);
for (int i=0; i<100; i++) {
if(i == 50)
![Page 18: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/18.jpg)
r
Branching Statementscontinue skips the current iteration of a loop and proceeds directly to the next iteration
fo (int i=0; i<100; i++) {
if(i == 50)
continue;
System.out.println(“Rule #” + i);
}
![Page 19: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/19.jpg)
Embedded loops
for (int i = 0; i < 3; i++) {
for (int j = 2; j < 4; j++) {
System.out.println (i + “ “ + j);
}
}
Scope of the variable defined in the initialization: respective for block
![Page 20: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/20.jpg)
Outline
Conditionals
Loops
Arrays
ArrayLists. Maps
![Page 21: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/21.jpg)
Arrays
An array is an indexed list of values.
You can make an array of any type int, double, String, etc..
All elements of an array must have the same type.
![Page 22: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/22.jpg)
Arrays
..0 1 2 3 .. n-1
![Page 23: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/23.jpg)
Arrays
Example: double [ ]
5.0 2.44 9.01 1.0 -9.9 .. 0 1 2 3 .. n-1
![Page 24: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/24.jpg)
Arrays The index starts at zero and ends at length-1.
Example: int[] values = new int[5];
values[0] = 12; // CORRECT
values[4] = 12; // CORRECT
values[5] = 12; // WRONG!! compiles but
// throws an Exception
// at run-time
Have a demo with runtime exception
![Page 25: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/25.jpg)
Arrays
An array is defined using TYPE[].
Arrays are just another type.
int[] values; // array of int
int[][] values; // int[] is a type
![Page 26: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/26.jpg)
Arrays
To create an array of a given size, use the operator new :
int[] values = new int[5];
or you may use a variable to specify the size:
int size = 12;
int[] values = new int[size];
![Page 27: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/27.jpg)
Array Initialization
Curly braces can be used to initialize an array. It can ONLY be used when you declare the variable.
int[] values = { 12, 24, -23, 47 };
![Page 28: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/28.jpg)
Quiz time!
Is there an error in this code?
int[] values = {1, 2.5, 3, 3.5, 4};
![Page 29: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/29.jpg)
Accessing Arrays
To access the elements of an array, use the [] operator:
values[index]
Example: int[] values = { 12, 24, -23, 47 };
values[3] = 18; // {12,24,-23,18} int x = values[1] + 3; // {12,24,-23,18}
![Page 30: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/30.jpg)
The length variableEach array has a length variable built-in that contains the length of the array.
int[] values = new int[12];int size = values.length; // 12
int[] values2 = {1,2,3,4,5}int size2 = values2.length; // 5
![Page 31: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/31.jpg)
String arraysA side note
public static void main (String[] arguments){
System.out.println(arguments.length);
System.out.println(arguments[0]);
System.out.println(arguments[1]);
}
![Page 32: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/32.jpg)
Looping through an array
Example 1:
int[] values = new int[5];
for (int i=0; i<values.length; i++) {
values[i] = i;
int y = values[i] * values[i];
System.out.println(y);
}
![Page 33: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/33.jpg)
Looping through an array
Example 2:
int[] values = new int[5];
int i = 0;
while (i < values.length) {
values[i] = i;
int y = values[i] * values[i];
System.out.println(y); i++;
}
![Page 34: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/34.jpg)
Outline
Conditionals
Loops
Arrays
ArrayLists. Maps
![Page 35: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/35.jpg)
Arrays with items
Create the array bigger than you need
Track the next “available” slot
Book[] books = new Book[10];
int nextIndex = 0;
books[nextIndex] = b;
nextIndex = nextIndex + 1;
![Page 36: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/36.jpg)
Arrays with items
Create the array bigger than you need
Track the next “available” slot
Book[] books = new Book[10];
int nextIndex = 0;
books[nextIndex] = b;
nextIndex = nextIndex + 1;
����������������������������
![Page 37: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/37.jpg)
�
�
�
�
ArrayList
Modifiable list
Internally implemented with arrays
Features
Get/put items by index
Add items
Delete items
Loop over all items
![Page 38: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/38.jpg)
Array � ArrayList
Book[] books =
new Book[10];
int nextIndex = 0;
books[nextIndex] = b;
nextIndex += 1;
ArrayList<Book> books
= new ArrayList<Book>();
books.add(b);
![Page 39: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/39.jpg)
import java.util.ArrayList;
class ArrayListExample {
public static void main(String[] arguments) {
ArrayList<String> strings = new ArrayList<String>();
strings.add("Evan");
strings.add("Eugene");
strings.add("Adam");
System.out.println(strings.size());
System.out.println(strings.get(0));
System.out.println(strings.get(1));
strings.set(0, "Goodbye");
strings.remove(1);
������������������������������������������ �������� ����������������������������������� �����
for (String s : strings) {
System.out.println(s);
}
}
}
![Page 40: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/40.jpg)
Maps
Stores a (key, value) pair of objects
Look up the key, get back the value
Example: Address Book
� Map from names to email addresses
TreeMap: Sorted (lowest to highest)
HashMap: Unordered (pseudo-random)
![Page 41: Lecture 3: Loops, Arrays Lecture... · Arrays ArrayLists. Maps . Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements](https://reader030.vdocument.in/reader030/viewer/2022040606/5eb263e41f16424f3f66823c/html5/thumbnails/41.jpg)
public static void main(String[] arguments) {
HashMap<String, String> strings = new HashMap<String, String>();
strings.put("Evan", "[email protected]");
strings.put("Eugene", "[email protected]");
strings.put("Adam", "[email protected]”);
System.out.println(strings.size());
strings.remove("Evan");
System.out.println(strings.get("Eugene"));
for (String s : strings.keySet()) {
System.out.println(s);
}
for (String s : strings.values()) {
System.out.println(s);
}
for (Map.Entry<String, String> pairs : strings.entrySet()) {
System.out.println(pairs);
}
}