Show Menu

Java Array and ArrayList (OCA) Cheat Sheet (DRAFT) by

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

Array Initia­liz­ation

int [] a1; int a2[];
int [] a = new int[3];
int [] a = {1,2,3};
initia­lizer list
int [] a = new int[] {1,2,3};
initia­lizer list
int a[] = new int[];
compile error
int [] a = new int[3] {1,2,3};
compile error

Array length

System.out.print(a.length); //3
Compar­e:l­ength for Array, length() for String and size() for ArrayList

Loop through Array

for (int i=0; i<arr.length; i++) {} //modify
for ( int a : arr) {...} //access only

Array as method parameter

passing an array is passing the memory address, the change on array content will be persistent through caller and callee.

Array contents

int [] intArr={1,2,3} 
int [] doubleArr=new double[3]
String [] objArr = new String[3]; //object type

Array Modifi­cation

Array is fixed in length once initia­lized, remove an element will resize the array and move all the element on its right. This could be comput­ati­onally expensive.

Arrays utilities

import java.util.Arrays;
//if found, return index
//if not return -(potential index -1)
//if array not sorted,result is undefined
List list=Arrays.asList(arr);
Arrays.copyOfRange(Object[] src,from,to)

toString() method

not overlo­aded, inherited from Object class directly

Variable arguments varargs

public static void main (String [] args){}
public static void main (String... args){}
//0 or more arguments
//similar to method overloading, or arrays
//args are treated as args[]

Multiple dimension array

//initialization, first index required
int [][] arr = new int[4][];
int [] arr[]= new int[5][];
int arr[][]= new int[6][]
int [] arr[][]= new int[5][][]; //3D
//loop through
for (int i=0;i<arr.length;i++}{
   for (int j=0;j<arr[i];j++) {... }
for (int innerArr:arr){
  for (int num:innerArr){...}


import java.u­til.Ar­ray­List;
Size dynami­cally changed at runtime with autoshift
last element indexed by size()-1
only contains reference type

Array vs ArrayList

length predefined
size() variable
manual shift
Auto shift
primit­ive+Ref type
ref type only
equals() not overriden
overriden equals()
toString() not overriden
overriden toString()

Create ArrayList

ArrayList aL1 = new ArrayList();
ArrayList aL2 = new ArrayList(5);
ArrayList aL3 = new ArrayList(aL1);

ArrayList<String> aL4,aL5;
aL4=new ArrayList<String>(); //Java5
aL5 = new ArrayList<>(); //Java 7

ArrayList aL6 = Arrays.asList(arr);
IllegA­rgu­men­tEx­ception // when primary type value assigned

Using ArrayList

Boolean add(E element)
void add (int index, E element)
Boolean remove­(Object obj)
E remove(int index)
E set(int index, E element)
int size()
boolean isEmpty()
void clear()
boolean contai­ns(­Object obj)
boolean equals­(Object obj)

Wrraper class

To put primitive type to ArrayList, using wrapper class
Boolea­n,B­yte­,Sh­ort­,In­teg­er,­Lon­g,F­loa­t,D­oub­le,­Cha­racter­rse­Xxx­(St­ring) //convnert string to a primitive­lue­Of(­String) //convert string to a wrapper class(Xxx)


Auto primitive value > wrapper class obj
null allowed boxing into list, print ok,but unboxing is not allowed --> NullPo­int­exc­eption
Integer List is interest: int as index is precedent autobo­xing. remove(1) will remove second element, not Integer(1)

Convert Between Array and List

1. toArray()
listObj.toArray();  //convert to obj arr
listObj.toArray(new String[0]);
//index= 0 will return a new string array, 
//index >0 will reuse if fit, else return new one

2. AsList()
//get a list with fixed size
list =Arrays.asList(arrayObj)
list.remove(1): //UnsupportedOperation
List<String> args=Arrays.asList("one","two");//varargs
// get a new arrayList obj, 
ArrayList<String> aL=new ArrayList(Arrays.asList("one","two"));
toArray() is list object method, return new array object
asList() is Arrays class static method, return the memory location as a list (same structure shared by array and list, content can be changed by both array and list,size still fixed)