Show Menu
Cheatography

Java Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Arrays - Initia­liz­ation

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 Initia­lizer List is used, then the new Keyword is unnece­ssary
The length of the array will be initia­lized as - final public instance length

Contents of an Array

Variables
Objects
Each variable must be set indivi­dually
Each object must be initia­lized indivi­dually
 
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 ArrayL­ist.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 restri­ctions

Can only contain object types
variables will be auto-boxed into a wrapper class before being inserted
to access, typeVa­lue() must be called, but arrayList already auto-u­nboxes the variables
a null variable will yield a nullPo­int­erE­xce­ption when auto-u­nboxing