Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Java Array and ArrayList (OCA) Cheat Sheet

by Jianmin Feng (taotao) via cheatography.com/79308/cs/19274/

Array Initia​liz​ation Arrays utilities ArrayList

int [] a1; int a2[]; declar​ations import java.util.Arrays; import java.u​til.Ar​ray​List;


int [] a = new int[3]; a={0,0,0} Arrays.so​rt(​arr); Size dynami​cally changed at runtime with
int [] a = {1,2,3}; initia​lizer list
Arrays.bi​nar​ySe​arc​h(a​rr,​item); autoshift
//if found, return index last element indexed by size()-1
int [] a = new int[] {1,2,3}; initia​lizer list
//if not return -(pote​ntial
int a[] = new int[]; compile error only contains reference type
index -1)
int [] a = new int[3] {1,2,3}; compile error //if array not sorted​,result is
Array vs ArrayList
undefined
Array length Array Arra​yList
List list=A​rra​ys.a​sL​ist​(arr);
System.out.print(a.length); //3 Arrays.co​pyO​fRa​nge​(Ob​ject[] length predefined size() variable

src,fr​om,to) manual shift Auto shift


Compar​e:l​ength for Array, length() for String
and size() for ArrayList faster slower
toString() method primit​ive+Ref type ref type only
Loop through Array not overlo​aded, inherited from Object class ArrayI​nde​xOu​tOf​Bou​‐ IndexO​utO​fBo​und​‐
for (int i=0; i<arr.length; i++) directly nda​ryE​xce​ption ary​Exc​eption
{} //modify equals() not overriden overriden equals()
Variable arguments varargs
for ( int a : arr) {...} toString() not overriden
//access only public static void main (String overriden toString()
[] args){}
Arrays.sort() Collec​tio​ns.s​ort()
Array as method parameter public static void main
Arrays.bi​nar​ySe​arch() Collec​tio​ns.b​in​ary​‐
passing an array is passing the memory (String... args){}
Sea​rch()
address, the change on array content will be //0 or more arguments
persistent through caller and callee. //similar to method overlo​ading,
Create ArrayList
or arrays
ArrayList aL1 = new ArrayList();
Array contents //args are treated as args[]
ArrayList aL2 = new ArrayL​‐
int [] intArr={1,2,3}
Multiple dimension array ist(5);
int [] double​Arr=new double[3]
ArrayList aL3 = new ArrayL​ist​‐
String [] objArr = new
(aL1);
String[3]; //object type //init​ial​iza​tion, first index
ArrayL​ist​<St​rin​g> aL4,aL5;
required
aL4=new ArrayL​ist​<St​rin​g>();
Array Modifi​cation int [][] arr = new int[4][];
//Java5
int [] arr[]= new int[5][];
Array is fixed in length once initia​lized, aL5 = new ArrayL​ist​<>(); //Java
remove an element will resize the array and int arr[][]= new int[6][]
7
move all the element on its right. This could int [] arr[][]= new int[5]​[][];
ArrayList aL6 = Arrays.as​Lis​‐
be comput​ati​onally expensive. //3D
t(arr);
arr={{1,2,3},[4,5,6}};
//loop through IllegA​rgu​men​tEx​ception // when primary

for (int i=0;i<​arr.le​ngt​h;i++}{ type value assigned

​ ​ for (int j=0;j<​arr​[i]​;j++)


{... }
}
for (int innerA​rr:​arr){
​ for (int num:in​ner​Arr​){...}
}

By Jianmin Feng (taotao) Not published yet. Sponsored by Readable.com


cheatography.com/taotao/ Last updated 10th April, 2019. Measure your website readability!
Page 1 of 2. https://readable.com
Java Array and ArrayList (OCA) Cheat Sheet
by Jianmin Feng (taotao) via cheatography.com/79308/cs/19274/

Using ArrayList Convert Between Array and List

add Boolean add(E element) 1. toArray()


void add (int index, E element) listOb​j.t​oAr​ray(); //convert to
obj arr
remove Boolean remove​(Object obj)
listOb​j.t​oAr​ray(new String​[0]);
E remove(int index)
//index= 0 will return a new
set E set(int index, E element)
string array,
size int size() //index >0 will reuse if fit,
boolean isEmpty() else return new one
clear void clear() 2. AsList()

contains boolean contai​ns(​Object obj) //get a list with fixed size


list =Array​s.a​sLi​st(​arr​ayObj)
equals boolean equals​(Object obj)
list.r​emo​ve(1): //Unsu​ppo​rte​dOp​‐
eration
Wrraper class
List<S​tri​ng> args=A​rra​ys.a​sL​ist​‐
To put primitive type to ArrayList, using
("on​e","t​wo")​;//​varargs
wrapper class
// get a new arrayList obj,
Boolea​n,B​yte​,Sh​ort​,In​teg​er,​Lon​g,F​loa​t,D​‐
ArrayL​ist​<St​rin​g> aL=new ArrayL​‐
oub​le,​Cha​racter
ist​(Ar​ray​s.a​sLi​st(​"​one​"​,"tw​o"));
Xxx.pa​rse​Xxx​(St​ring) //convnert string to a
toArray() is list object method, return new
primitive
array object
Xxx.va​lue​Of(​String) //convert string to a
asList() is Arrays class static method, return
wrapper class(Xxx)
the memory location as a list (same
Number​For​mat​Exc​eption structure shared by array and list, content
can be changed by both array and list,size
Autoboxing still fixed)

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)

By Jianmin Feng (taotao) Not published yet. Sponsored by Readable.com


cheatography.com/taotao/ Last updated 10th April, 2019. Measure your website readability!
Page 2 of 2. https://readable.com

You might also like