Array Information:
-
Allows us to store multiple values under a single name
- Multiple values, one variable
-
Each value in an array has its own index
- Starts from 0
-
Array lengths are fixed
-
Primitive vs object arrays:
-
Primitive arrays store actual primitive value
-
object arrays store refrences to the actual value of the object
-
-
Default values:
-
Numeric arrays == 0
-
Boolean arrays == false
-
Object/String arrays == null
- null is a keyword meaning no address
-
-
ArrayIndexOutOfBounds errors don’t show up at compile time
Declaring and Creating Arrays:
- Declaring an array:
//Array template
elementType[] identifier;
elementType identifier[];
//Examples
double[] weeks;
double weeks[];
- Creating an array:
//Array template
new elementType[length];
//Example
double[] weeks = new double[5] //Creates an array with length 5
Specifying Values:
//Assign values while declaring:
elementType[] identifier = {values}; //template
double[] test = {5.3, 8.1, 0.1, 9};
//Assign values one at a time:
identifier[index] = value; //template
double[] test = new double[3];
test[0] = 5.3;
test[1] = 8.1;
test[2] = 0.1;
for-each Statement:
-
Use for loop without using indicies/indexes
- Avoids ArrayIndexOutOfBounds errors
-
To edit a value inside the array, use created variable instead of index
//Template
for (arrayType variable : array) {
bodystatement;
}
//Example (sum all values in array)
double test[] = {5.3, 8.1, 0.1, 9};
double sum;
for (double grade : test) {
sum += grade;
}
Searching Arrays:
- Can use for loops
Sparce Arrays and Null Checking:
-
A null value in an array can cause an error when an object is called on it
- Can happen when searching through arrays in a for loop
-
Can check if a value in an array is null:
if (value != null) {
statement;
}
CLI Arguments
-
Can take CLI arguments from terminal when starting the JVM
-
Java creates the array containing the arguments and makes a refrence to it in args
-
Arguments go into the main ‘args’ parameter
-
public static void main(String[] args) //String[] args is cli input
Wrapper Classes:
-
There is a class for each primitive var that enhances features
- Ex. Conversion
-
Each wrapper class has its own sets of parameters
2D Arrays:
-
Arrays of Arrays
- A “table” of values
-
2 Index values instead of 1
- First index is row, seccond is column
-
Creating 2D Arrays:
//Structure
elementType[][] identifier;
elementType identifier[][];
//Examples
double[][] array2d = {{80, 70, 75}, {69, 72, 74}};
double[][] array2d = {{80, 70, 75},
{69, 72, 74}};
double[][] array2d = new double[2][3];
- Assigning Elements:
//Structure
identifier[row][col] = value;
//Examples
array2d[0][1] = 50;
array2d[1][0] = 25;
2D Array Processing (for loops):
-
Needs two for loops to go through every value
-
Row Major:
- Row by Row
for (int row = 0; row < array2d.length; row++) {
for (int col = 0; col < array2d[row].length; col++) {
bodystatement;
}
}
-
Column Major:
- Column by Column
for (int col = 0; col < array2d[0].length; col++) {
for (int row = 0; row < array2d.length; row++) {
bodystatement;
}
}
Ragged Arrays:
-
2D Arrays with rows of varying lengths
-
Created just like normal arrays
-
Important to use .length when traversing instead of a constant valuye
More Dimentions:
-
Can create arrays of multiple dimentions:
-
3D
-
4D
-
Etc…
-