This document provides a summary of Java arrays and ArrayList. It outlines how to initialize and access elements in arrays, as well as built-in array utilities like sort and binary search. ArrayList is introduced as a dynamic alternative to arrays, with methods to add, remove, and access elements. The key differences between arrays and ArrayList are summarized, such as fixed vs dynamic size and primitive vs reference types.
This document provides a summary of Java arrays and ArrayList. It outlines how to initialize and access elements in arrays, as well as built-in array utilities like sort and binary search. ArrayList is introduced as a dynamic alternative to arrays, with methods to add, remove, and access elements. The key differences between arrays and ArrayList are summarized, such as fixed vs dynamic size and primitive vs reference types.
This document provides a summary of Java arrays and ArrayList. It outlines how to initialize and access elements in arrays, as well as built-in array utilities like sort and binary search. ArrayList is introduced as a dynamic alternative to arrays, with methods to add, remove, and access elements. The key differences between arrays and ArrayList are summarized, such as fixed vs dynamic size and primitive vs reference types.
by Jianmin Feng (taotao) via cheatography.com/79308/cs/19274/
Array Initialization Arrays utilities ArrayList
int [] a1; int a2[]; declarations import java.util.Arrays; import java.util.ArrayList;
int [] a = new int[3]; a={0,0,0} Arrays.sort(arr); Size dynamically changed at runtime with int [] a = {1,2,3}; initializer list Arrays.binarySearch(arr,item); autoshift //if found, return index last element indexed by size()-1 int [] a = new int[] {1,2,3}; initializer list //if not return -(potential 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 ArrayList List list=Arrays.asList(arr); System.out.print(a.length); //3 Arrays.copyOfRange(Object[] length predefined size() variable
src,from,to) manual shift Auto shift
Compare:length for Array, length() for String and size() for ArrayList faster slower toString() method primitive+Ref type ref type only Loop through Array not overloaded, inherited from Object class ArrayIndexOutOfBou‐ IndexOutOfBound‐ for (int i=0; i<arr.length; i++) directly ndaryException aryException {} //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() Collections.sort() Array as method parameter public static void main Arrays.binarySearch() Collections.binary‐ passing an array is passing the memory (String... args){} Search() address, the change on array content will be //0 or more arguments persistent through caller and callee. //similar to method overloading, 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 [] doubleArr=new double[3] ArrayList aL3 = new ArrayList‐ String [] objArr = new (aL1); String[3]; //object type //initialization, first index ArrayList<String> aL4,aL5; required aL4=new ArrayList<String>(); Array Modification int [][] arr = new int[4][]; //Java5 int [] arr[]= new int[5][]; Array is fixed in length once initialized, aL5 = new ArrayList<>(); //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.asLis‐ be computationally expensive. //3D t(arr); arr={{1,2,3},[4,5,6}}; //loop through IllegArgumentException // when primary
for (int i=0;i<arr.length;i++}{ type value assigned
for (int j=0;j<arr[i];j++)
{... } } for (int innerArr:arr){ for (int num:innerArr){...} }
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) listObj.toArray(); //convert to obj arr remove Boolean remove(Object obj) listObj.toArray(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 contains(Object obj) //get a list with fixed size
list =Arrays.asList(arrayObj) equals boolean equals(Object obj) list.remove(1): //UnsupportedOp‐ eration Wrraper class List<String> args=Arrays.asList‐ To put primitive type to ArrayList, using ("one","two");//varargs wrapper class // get a new arrayList obj, Boolean,Byte,Short,Integer,Long,Float,D‐ ArrayList<String> aL=new ArrayL‐ ouble,Character ist(Arrays.asList("one","two")); Xxx.parseXxx(String) //convnert string to a toArray() is list object method, return new primitive array object Xxx.valueOf(String) //convert string to a asList() is Arrays class static method, return wrapper class(Xxx) the memory location as a list (same NumberFormatException 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 --> NullPointexc‐ eption Integer List is interest: int as index is precedent autoboxing. 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