10 Arrays and ArrayLists
Oracle Academy Section
Section 8: Arrays and Exceptions
Lesson 1: One-dimensional Arrays
Lesson 2: ArrayLists
Java Foundations Certification Exam Topics
Arrays and ArrayLists
Use a one-dimensional array
Create and manipulate an ArrayList
Traverse the elements of an ArrayList by using iterators and loops including the enhanced for loop
Compare an array and an ArrayList
Key Resources
Java Tutorials:
tutorialspoint:
Udemy
Multi-Dimensional Arrays (13:06) creating 2d int array grid, array of arrays, rows can have different lengths, 2d String array texts, iterating through using nested for, printing with \t, declaring with only rows
ArrayList (9:58) specifying type, can't use primative type, must use corresponding class, .add, iterate over using for and size or using for each, .remove using index, from end fast, from beginning slow
Bucky's Room
Supplemental Resources
Logic Ch 8 Arrays
Sequentially searching an array
Passing an array as an argument
Programmr: What is array?
Most Important Concepts / Code
An array is a container object that holds a fixed number of values of a single type.
The length of an array is established when the array is created. After creation, its length is fixed.
Values stored in an array are called elements.
Elements are accessed by using the array name and then the subscript (also called index) in square brackets [ ].
You can use the array name and the subscript just like a variable.
Each array has a length attribute that tells you the number of elements.
Declare and create array:
All on one line, like:
dataType[] arrayRefVar = new dataType[arraySize];
Or on two lines, like:
dataType[] arrayRefVar; // this line names the array and declares the data type
arrayRefVar = new dataType[arraySize]; // this line actually creates the space in memory for the array
Example:
int[] anArray = new int[3];
Or
int[] anArray;
anArray = new int[3];
When you know the values you want to populate the array at the time of declaration you could use this syntax instead to declare, create, and initialize:
dataType[] arrayRefVar = {value0, value1, ..., valuek}; //
Example:
int[] anArray = { 100, 200, 300 };
Use a for loop and the length attribute to loop through an array.
for (int i = 0; i < anArray.length; i++) {
System.out.println(anArray[i]);
}
A string is an array of characters. length is a method of the String class.
System.out.print(testWord.toCharArray()[letterPosition]);
You cannot assign an array to another. To achieve this you can use .clone()
destinationArray = sourceArry.clone();
A two-dimensional array is like a table.
Access elements with arrayName[row][col]
int[][] boxscore = new int[2][9]; // 2D integer array with 2 rows and 9 columns
// how to initialize two dimensional array in Java
// using for loop
int[][] board = new int[3][3];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = i + j;
}
}
Array vs. List vs. ArrayList
With generics, the data type becomes like an argument.
ArrayList<String> list = new ArrayList<String>(); // creates ArrayList
list.add("Item1"); // adds item
int pos = list.indexOf("Item1"); // gets the position that matches argument
int size = list.size(); // returns the size
boolean element = list.contains("Item5"); // finds
String item = list.get(0); // returns element at index location
list.remove(0); // removes element at index location
list.remove("Item3"); // removes match
for (int i = 0; i < list.size(); i++) {
System.out.println("Index: " + i + " - Item: " + list.get(i));
}
for (String str : list) {
System.out.println("Item is: " + str);
}
Hour 1
Quiz and Concepts Review
for each loop
Algorithmic thinking to get largest and smallest
Efficiency
Coding examples
Fixed size
Single type
Out of bounds
Get sum and average
2d examples
Tic Tac Toe / Chess
Platform games like Super Mario Bros.
Jagged array
A 2 dimensional array where each sub array is a different length.
String[][] squares = new String[3][];
squares[0] = new String[10];
squares[1] = new String[20];
squares[2] = new String[30];
Programming joke of the week
{"hip", "hip"}
Tournament Challenge (Spring semester)
Hour 2
Practice
What are the contents of arr after the for loop?
int j = 0;
for (i = 0; i < 5; i++)
{
arr[i] = i + j;
j = j + 1;
}
What are the contents of arr after the for loop?
int qty = 1;
for (index = 0; index < 5; index++)
{
arr[index] = index * qty;
qty = qty + 1;
}
Parallel arrays