\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Jianmin Feng (taotao)} \pdfinfo{ /Title (java-array-and-arraylist-oca.pdf) /Creator (Cheatography) /Author (Jianmin Feng (taotao)) /Subject (Java Array and ArrayList (OCA) Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{075707} \definecolor{LightBackground}{HTML}{F7F9F7} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Java Array and ArrayList (OCA) Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Jianmin Feng (taotao)} via \textcolor{DarkBackground}{\uline{cheatography.com/79308/cs/19274/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Jianmin Feng (taotao) \\ \uline{cheatography.com/taotao} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 10th April, 2019.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{3.23505 cm} x{1.74195 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Array Initialization}} \tn % Row 0 \SetRowColor{LightBackground} int {[}{]} a1; int a2{[}{]}; & declarations \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} int {[}{]} a = new int{[}3{]}; & a=\{0,0,0\} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} int {[}{]} a = \{1,2,3\}; & initializer list \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} int {[}{]} a = new int{[}{]} \{1,2,3\}; & initializer list \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} int a{[}{]} = new int{[}{]}; & compile error \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} int {[}{]} a = new int{[}3{]} \{1,2,3\}; & compile error \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Array length}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{System.out.print(a.length);} //3} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Compare:length for Array, length() for String and size() for ArrayList} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Loop through Array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{for (int i=0; i\textless{}arr.length; i++) \{\} //modify \newline for ( int a : arr) \{...\} //access only} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Array as method parameter}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{passing an array is passing the memory address, the change on array content will be persistent through caller and callee.% Row Count 3 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Array contents}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int {[}{]} intArr=\{1,2,3\} \newline int {[}{]} doubleArr=new double{[}3{]} \newline String {[}{]} objArr = new String{[}3{]}; //object type} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Array Modification}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Array is fixed in length once initialized, remove an element will resize the array and move all the element on its right. This could be computationally expensive.% Row Count 4 (+ 4) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Arrays utilities}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import java.util.Arrays; \newline Arrays.sort(arr); \newline Arrays.binarySearch(arr,item); \newline //if found, return index \newline //if not return -(potential index -1) \newline //if array not sorted,result is undefined \newline List list=Arrays.asList(arr); \newline Arrays.copyOfRange(Object{[}{]} src,from,to)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{toString() method}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{not overloaded, inherited from Object class directly% Row Count 2 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Variable arguments varargs}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{public static void main (String {[}{]} args)\{\} \newline public static void main (String... args)\{\} \newline //0 or more arguments \newline //similar to method overloading, or arrays \newline //args are treated as args{[}{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Multiple dimension array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{//initialization, first index required \newline int {[}{]}{[}{]} arr = new int{[}4{]}{[}{]}; \newline int {[}{]} arr{[}{]}= new int{[}5{]}{[}{]}; \newline int arr{[}{]}{[}{]}= new int{[}6{]}{[}{]} \newline int {[}{]} arr{[}{]}{[}{]}= new int{[}5{]}{[}{]}{[}{]}; //3D \newline arr=\{\{1,2,3\},{[}4,5,6\}\}; \newline //loop through \newline for (int i=0;i\textless{}arr.length;i++\}\{ \newline for (int j=0;j\textless{}arr{[}i{]};j++) \{... \} \newline \} \newline for (int innerArr:arr)\{ \newline for (int num:innerArr)\{...\} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{ArrayList}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import java.util.ArrayList;} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Size dynamically changed at runtime with autoshift} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{last element indexed by size()-1} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{only contains reference type} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.68758 cm} x{2.28942 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Array vs ArrayList}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Array}} & {\bf{ArrayList}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} length predefined & size() variable \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} manual shift & Auto shift \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} faster & slower \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} primitive+Ref type & ref type only \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \seqsplit{ArrayIndexOutOfBoundaryException} & \seqsplit{IndexOutOfBoundaryException} \tn % Row Count 7 (+ 2) % Row 6 \SetRowColor{LightBackground} equals() not overriden & overriden equals() \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} toString() not overriden & overriden toString() \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} Arrays.sort() & \seqsplit{Collections.sort()} \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} \seqsplit{Arrays.binarySearch()} & \seqsplit{Collections.binarySearch()} \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Create ArrayList}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{ArrayList aL1 = new ArrayList(); \newline ArrayList aL2 = new ArrayList(5); \newline ArrayList aL3 = new ArrayList(aL1); \newline \newline ArrayList\textless{}String\textgreater{} aL4,aL5; \newline aL4=new ArrayList\textless{}String\textgreater{}(); //Java5 \newline aL5 = new ArrayList\textless{}\textgreater{}(); //Java 7 \newline \newline ArrayList aL6 = Arrays.asList(arr);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{IllegArgumentException // when primary type value assigned} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.04517 cm} x{3.93183 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Using ArrayList}} \tn % Row 0 \SetRowColor{LightBackground} add & Boolean add(E element) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} & void add (int index, E element) \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} remove & Boolean remove(Object obj) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} & E remove(int index) \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} set & E set(int index, E element) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} size & int size() \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} & boolean isEmpty() \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} clear & void clear() \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} \seqsplit{contains} & boolean contains(Object obj) \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} equals & boolean equals(Object obj) \tn % Row Count 10 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Wrraper class}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{To put primitive type to ArrayList, using wrapper class} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Boolean,Byte,Short,Integer,Long,Float,Double,Character} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Xxx.parseXxx(String) //convnert string to a primitive} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Xxx.valueOf(String) //convert string to a wrapper class(Xxx)} \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{NumberFormatException} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Autoboxing}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Auto primitive value \textgreater{} wrapper class obj} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{null allowed boxing into list, print ok,but unboxing is not allowed -{}-\textgreater{} NullPointexception} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Integer List is interest: int as index is precedent autoboxing. remove(1) will remove second element, not Integer(1)} \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Convert Between Array and List}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1. toArray() \newline listObj.toArray(); //convert to obj arr \newline listObj.toArray(new String{[}0{]}); \newline //index= 0 will return a new string array, \newline //index \textgreater{}0 will reuse if fit, else return new one \newline \newline 2. AsList() \newline //get a list with fixed size \newline list =Arrays.asList(arrayObj) \newline list.remove(1): //UnsupportedOperation \newline List\textless{}String\textgreater{} args=Arrays.asList("one","two");//varargs \newline // get a new arrayList obj, \newline ArrayList\textless{}String\textgreater{} aL=new \seqsplit{ArrayList(Arrays.asList("one"},"two"));} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{toArray() is list object method, return new array object \newline 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)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}