This is a draft cheat sheet. It is a work in progress and is not finished yet.
Arrays - Initialization
Array is a object, so the new Keyword is used |
Array sizes must remain fixed after creation |
Array References may be reassigned to a different array of different size |
If an Initializer List is used, then the new Keyword is unnecessary |
The length of the array will be initialized as - final public instance length |
Contents of an Array
Variables |
Objects |
Each variable must be set individually |
Each object must be initialized individually |
|
mutator methods can be called with each object |
|
|
Traversing an Array
For-each loop: Use if you need to access every element but dont need to replace or remove |
You can also use it to access mutator methods to access objects if you have an array of objects |
For loop: Use for all other cases |
ArrayList Advantages
size is not fixed |
last slot in use is always ArrayList.size() - 1 |
insertion and deletion of objects is a single statement, shifting is done for you |
printing an arraylist prints the list formatted in square brackets, whereas arrays will print their hashcode |
|
|
Array as parameter
Arrays are treated as objects, so only their reference gets passed in a parameter |
This allows the actual array to be accessed and modified from a method from said parameter |
ArrayList restrictions
Can only contain object types |
variables will be auto-boxed into a wrapper class before being inserted |
to access, typeValue() must be called, but arrayList already auto-unboxes the variables |
a null variable will yield a nullPointerException when auto-unboxing |
|