\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{Sara (lasago)} \pdfinfo{ /Title (java.pdf) /Creator (Cheatography) /Author (Sara (lasago)) /Subject (Java 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}{6692DE} \definecolor{LightBackground}{HTML}{F5F8FC} \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 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Sara (lasago)} via \textcolor{DarkBackground}{\uline{cheatography.com/70899/cs/18394/}}} \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}Sara (lasago) \\ \uline{cheatography.com/lasago} \\ \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 5th June, 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{tabularx}{17.67cm}{x{7.5988 cm} x{9.6712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Objetos}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Instancias}} & En los lenguajes de programación orientada a objetos un objeto es una instancia de una clase. Esto es, un miembro de una clase que tiene atributos en lugar de variables. \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} {\bf{Atributos}} & Los atributos son las caracterísiticas individuales que diferencian un objeto de otro y determinan su apariencia, estado u otras cualidades. Los atributos se guardan en variables denominadas de instancia, y cada objeto particular puede tener valores distintos para estas variables. Las variables de instancia también denominados miembros dato, son declaradas en la clase pero sus valores son fijados y cambiados en el objeto. Además de las variables de instancia hay variables de clase, las cuales se aplican a la clase y a todas sus instancias. Por ejemplo, el número de ruedas de un automóvil es el mismo cuatro, para todos los automóviles. \tn % Row Count 38 (+ 30) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.5988 cm} x{9.6712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Objetos (cont)}} \tn % Row 2 \SetRowColor{LightBackground} {\bf{Constructores}} & Un constructor inicializa un objeto al crearlo. Tiene el mismo nombre que su clase y se parece sintácticamente a un método, pero no tienen tipo de return.\{\{nl\}\}Normalmente se usan para dar valores iniciales a las variables de instancia definidas por la clase, o para llevar a cabo otros procedimientos de inicio para crear un objeto completamente formado.\{\{nl\}\}todas las clases tienen constructores, los definas o no, porque java ofrece un constructor por defecto que inicializa todas las variables de miembro a 0.\{\{nl\}\}`class ClassName \{ ClassName() \{ \} \}` \tn % Row Count 26 (+ 26) % Row 3 \SetRowColor{white} {\bf{-No argument Constructors}} & using these constructors the instance variables of a method will be initialized with fixed values for all objects.\{\{nl\}\}`Public class MyClass \{ Int num; MyClass() \{ num = 100; \} \}`\{\{nl\}\}You would call constructor to initialize objects as follows\{\{nl\}\}`public class ConsDemo \{ public static void main(String args{[}{]}) \{ MyClass t1 = new MyClass(); MyClass t2 = new MyClass(); \seqsplit{System.out.println(t1.num} + " " + t2.num); \} \}`\{\{nl\}\}\{\{nl\}\}Se pueden modificar los atributos de la clase creando una instancia, un nuevo objeto, y accediendo a ellos main\{ Humidade h = new Humidade(); h.num = 1000; \} \{\{nl\}\}Humidade int num = 10; Humidade()\{num = 100;\} El roden es 1000, 100, 10 \tn % Row Count 59 (+ 33) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.5988 cm} x{9.6712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Objetos (cont)}} \tn % Row 4 \SetRowColor{LightBackground} {\bf{-Parameterized Constructors}} & Most often, you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method. \{\{nl\}\}`// A simple constructor. class MyClass \{ int x; // Following is the constructor MyClass(int i ) \{ x = i; \} \}`\{\{nl\}\}You would call constructor to initialize objects as follows \{\{nl\}\} `public class ConsDemo \{ public static void main(String args{[}{]}) \{ MyClass t1 = new MyClass( 10 ); MyClass t2 = new MyClass( 20 ); \seqsplit{System.out.println(t1.x} + " " + t2.x); \} \}` \tn % Row Count 26 (+ 26) % Row 5 \SetRowColor{white} {\bf{"this" keyword}} & Keyword THIS is a reference variable in Java that refers to the current object. \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.5988 cm} x{9.6712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Objetos (cont)}} \tn % Row 6 \SetRowColor{LightBackground} {\bf{Variables de instancia}} & Una variable de instancia es un campo declarado dentro de la declaración de una clase sin usar la palabra reservada static. Si una clase T tiene un campo a que es una variable de instancia, entonces una nueva variable de instancia a es creada e inicializada a un valor por defecto como parde de cada nuevo objeto creado de la clase T o de cualquier clase que sea subclase de T La bariable de instancia deja de existir cuando el objeto del cual es campo deja de ser referenciado, después que cualquier finalización necesaria del objeto ha sido completada \tn % Row Count 26 (+ 26) % Row 7 \SetRowColor{white} {\bf{variables de clase}} & Una variable de clase es un campo declarado usando la palabra reservada static dentro de una declaración de clase o con o sin la palabra reservada static dentro de una declaración de una interfaz. Una variable de clase es creada cuando su clase o interfaz es preparada y es inicializada a un valor por defecto. La variable de clase deja de existir cuando su clase o interfaz es descargada. \tn % Row Count 44 (+ 18) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Métodos}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{A method in Java is a block of statements that has a name and can be executed by calling (also called invoking) it from some other place in your program. Along with fields, methods are one of the two elements that are considered members of a class. (Constructors and initializers are not considered class members.).} \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{Every program must have at least one method for the program to accomplish any work. And every program must have a method named main, which is the method first invoked when the program is run.} \tn % Row Count 11 (+ 4) % Row 2 \SetRowColor{LightBackground} & `visibility {[}static{]} return-type method-name (parameter-list) \{ statements... \}` \tn % Row Count 16 (+ 5) % Row 3 \SetRowColor{white} {\bf{visibility}}: The visibility of a method determines whether the method is available to other classes. The options are & -{\bf{public}}: Allows any other class to access the method\{\{nl\}\} -{\bf{private}}: Hides the method from other classes\{\{nl\}\}-{\bf{protected}}: Lets subclasses use the method but hides the method from other classes \tn % Row Count 27 (+ 11) % Row 4 \SetRowColor{LightBackground} {\bf{static}} & : This optional keyword declares that the method is a static method, which means that you can call it without first creating an instance of the class in which it's defined. The main method must always be static, and any other methods in the class that contains the main method usually should be static as well. A non-static method belongs to an object of the class and you have to create an instance of the class to access it. \tn % Row Count 49 (+ 22) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Métodos (cont)}} \tn % Row 5 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{return-type}}: After the word static comes the return type, which indicates whether the method returns a value when it is called — and if so, what type the value is. If the method doesn't return a value, specify void. String{[}{]}, Integer, int...} \tn % Row Count 5 (+ 5) % Row 6 \SetRowColor{white} {\bf{parameter list}}:En el momento de la creación se deb especificar el tipo de la variable y la variable.Al llamar al método se especifica el nombre. & public static int METODO( String{[}{]} nomes )\{ \tn % Row Count 13 (+ 8) % Row 7 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{Pasar por argumento:}}} \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{-} \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{-} \tn % Row Count 16 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{nl\}\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.3175 cm} x{12.9525 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Reference types}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Reference types hold references to objects and provide a means to access those objects stored somewhere in memory.} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{All reference types are a subclass of type java.lang.Object.} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{Annotation}}} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} {\bf{Array}} & Provides a fixed-size data structure that stores data elements of the same type. \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Class}} & Designed to provide inheritance, polymorphism, and encapsulation. Usually models something in the real world and consists of a set of values that holds data and a set of methods that operates on the data. \tn % Row Count 16 (+ 7) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{{\bf{Enumeration}}} \tn % Row Count 17 (+ 1) % Row 6 \SetRowColor{LightBackground} {\bf{Interface}} & Provides a public API and is "implemented" by Java classes. \tn % Row Count 20 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\seqsplit{https://www.oreilly.com/library/view/java-8-pocket/9781491901083/ch04.html}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Lists}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Arrays}} & {\emph{var-name = new type {[}size{]};}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} & int intArray{[}{]}; \{\{nl\}\}intArray = new int{[}20{]}; // alocar memoria al array \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} & int{[}{]} intArray = new int{[}20{]}; // combining both statements in one \tn % Row Count 10 (+ 4) % Row 3 \SetRowColor{white} Arrays bidimensionales & int{[}{]}{[}{]} multi = new int{[}5{]}{[}10{]}; \tn % Row Count 12 (+ 2) % Row 4 \SetRowColor{LightBackground} & int{[}{]}{[}{]} multi = new int{[}5{]}{[}{]}; multi{[}0{]} = new int{[}10{]}; multi{[}1{]} = new int{[}10{]}; multi{[}2{]} = new int{[}10{]}; multi{[}3{]} = new int{[}10{]}; multi{[}4{]} = new int{[}10{]}; \tn % Row Count 20 (+ 8) % Row 5 \SetRowColor{white} & int tiempo {[}{]}{[}{]} = new int{[}7{]}{[}2{]}; \tn % Row Count 22 (+ 2) % Row 6 \SetRowColor{LightBackground} & tiempo{[}0{]}{[}1{]} = 2; \tn % Row Count 23 (+ 1) % Row 7 \SetRowColor{white} & int{[}{]}{[}{]} multi = new int{[}{]}{[}{]}\{ \{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \}, \{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \}, \{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \}, \{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \}, \{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \} \}; \tn % Row Count 34 (+ 11) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Lists (cont)}} \tn % Row 8 \SetRowColor{LightBackground} & for( int i = 0; i \textless{} t.length; i++ )\{ \seqsplit{System.out.println("Día} "+i);//el día de la semana for( int j = 0; j \textless{} 2; j++ )\{ \seqsplit{System.out.println("a} las "+j+"h: "+t{[}i{]}{[}j{]});//j - la hora \} \} \tn % Row Count 13 (+ 13) % Row 9 \SetRowColor{white} {\bf{Arraylist class}} & implements List interface and it is based on an Array data structure \tn % Row Count 17 (+ 4) % Row 10 \SetRowColor{LightBackground} & ArrayList inherits AbstractList class and implements List interface. \tn % Row Count 21 (+ 4) % Row 11 \SetRowColor{white} & ArrayList is initialized by a size, however the size can increase if collection grows or shrunk if objects are removed from the collection. \tn % Row Count 28 (+ 7) % Row 12 \SetRowColor{LightBackground} & ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases \tn % Row Count 34 (+ 6) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Lists (cont)}} \tn % Row 13 \SetRowColor{LightBackground} & ArrayList(): This constructor is used to build an empty array list\{\{nl\}\}ArrayList(Collection c): This constructor is used to build an array list initialized with the elements from collection c\{\{nl\}\}ArrayList(int capacity): This constructor is used to build an array list with initial capacity being specified // Creating generic integer ArrayList ArrayList\textless{}Integer\textgreater{} arrli = new ArrayList\textless{}Integer\textgreater{}();\{\{nl\}\}arrli.add(i); \{\{nl\}\} // Remove element at index 3 arrli.remove(3); \{\{nl\}\}// Printing elements one by one for (int i=0; i\textless{}arrli.size(); i++) \seqsplit{System.out.print(arrli.get(i)+"} "); \tn % Row Count 31 (+ 31) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Lists (cont)}} \tn % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Los ArrayList funcionan con objetos y no con tipos primitivos, (Integer en vez de int, Character en vez de char...))} \tn % Row Count 3 (+ 3) % Row 15 \SetRowColor{white} Construye ArrayList con valor inicial & ArrayList(int initialCapacity) \tn % Row Count 5 (+ 2) % Row 16 \SetRowColor{LightBackground} Inserta el elemento en el index especificado, no se peude hacer si la posición no fue creada & add(int index, Object element) \tn % Row Count 10 (+ 5) % Row 17 \SetRowColor{white} Appends the specified element to the end of this ArrayList. & add(Object o) \tn % Row Count 13 (+ 3) % Row 18 \SetRowColor{LightBackground} & addAll(Collection c) Appends all of the elements in the specified Collection to the end of this this ArrayList, in the order that they are returned by the specified Collection's Iterator. \tn % Row Count 23 (+ 10) % Row 19 \SetRowColor{white} Borra todos los elementos & clear() \tn % Row Count 25 (+ 2) % Row 20 \SetRowColor{LightBackground} hace una copia superficial & clone() \tn % Row Count 27 (+ 2) % Row 21 \SetRowColor{white} Devuelve true si contiene el elemento & contains(Object elem) \tn % Row Count 29 (+ 2) % Row 22 \SetRowColor{LightBackground} Devuelve el elemento en la posición especificada & cars.get(0); \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Lists (cont)}} \tn % Row 23 \SetRowColor{LightBackground} Modificar elemento & cars.set(0, "Opel"); \tn % Row Count 2 (+ 2) % Row 24 \SetRowColor{white} Busca la ocurrencia del primer elemento & indexOf(Object elem) \tn % Row Count 4 (+ 2) % Row 25 \SetRowColor{LightBackground} & isEmpty() \tn % Row Count 5 (+ 1) % Row 26 \SetRowColor{white} devuelve el índice de la última ocurrencia & lastIndexOf(Object elem) \tn % Row Count 8 (+ 3) % Row 27 \SetRowColor{LightBackground} Quita el elemento del índice especificado & cars.remove(0) \tn % Row Count 11 (+ 3) % Row 28 \SetRowColor{white} Returns the number of components in this ArrayList. & size() \tn % Row Count 15 (+ 4) % Row 29 \SetRowColor{LightBackground} & toArray() ,{]} toArray(Object{[}{]} a) \tn % Row Count 17 (+ 2) % Row 30 \SetRowColor{white} Trims the capacity of this ArrayList to be the ArrayList's current size. & trimToSize() \tn % Row Count 22 (+ 5) % Row 31 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{} \tn % Row Count 22 (+ 0) % Row 32 \SetRowColor{white} Pasando por argumento & public void AnalyseArray(ArrayList\textless{}Integer\textgreater{} array) \{ // Do something \} ... ArrayList\textless{}Integer\textgreater{} A = new ArrayList\textless{}Integer\textgreater{}(); AnalyseArray(A); \tn % Row Count 30 (+ 8) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Lists (cont)}} \tn % Row 33 \SetRowColor{LightBackground} {\bf{Bidimensionales}} Crear: & ArrayList{[}{]}{[}{]} table = new ArrayList{[}10{]}{[}10{]}; table{[}0{]}{[}0{]} = new ArrayList(); // add another ArrayList object to {[}0,0{]} table{[}0{]}{[}0{]}.add(); // add object to that ArrayList \tn % Row Count 9 (+ 9) % Row 34 \SetRowColor{white} Sacar tamaño del ArrayList & int size = listOfBanks.size(); \tn % Row Count 11 (+ 2) % Row 35 \SetRowColor{LightBackground} Sacar tamaño de ArrayList bidimensional & list.get(0).get(0) \tn % Row Count 13 (+ 2) % Row 36 \SetRowColor{white} Añadir elementos & arrlist.add(15);\{\{nl\}\}// adding element 25 at third position arrlist.add(2,25); \tn % Row Count 18 (+ 5) % Row 37 \SetRowColor{LightBackground} Pasar arraylist bidimensional por método & ArrayList\textless{}ArrayList\textless{}Integer\textgreater{}\textgreater{} disponibilidade \tn % Row Count 21 (+ 3) % Row 38 \SetRowColor{white} Agregar valor en array bidimensional & ArrayList\textless{}ArrayList\textless{}Integer\textgreater{}\textgreater{} d = new ArrayList\textless{}ArrayList\textless{}Integer\textgreater{}\textgreater{}(); for( int i = 0; i \textless{} d.size(); i++ )\{ for( int j = 0; j \textless{} d.get(i).size(); j++ )\{ d.get(i).add(0); \} \tn % Row Count 32 (+ 11) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\{\{nl\}\} \newline https://www.geeksforgeeks.org/arraylist-in-java/ \newline https://courses.cs.washington.edu/courses/cse341/98au/java/jdk1.2beta4/docs/api/java/util/ArrayList.html \newline https://www.programiz.com/java-programming/multidimensional-array} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Arrays multidimensionales}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/lasago_1555077142_two-dimensional-array.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.727 cm} p{1.727 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Classes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.9359 cm} x{14.3341 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Variables}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Globales} & Java doesn't technically support global variables. As a pure object-oriented language, everything needs to be part of a class.\{\{nl\}\}The static modifier tells Java that the variable can be used across all instances of the class. \tn % Row Count 7 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.181 cm} x{12.089 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{If}} \tn % Row 0 \SetRowColor{LightBackground} If & if (a \textgreater{} b) \{ max = a; \} else \{ max = b; \} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Operador ternario & max = (a \textgreater{} b) ? a : b; \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Booleanos & if( !contiene )\{\} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.727 cm} p{1.727 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Switch case}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{int c; switch(c)\{ case 1: case 2: case 3: resultado = x; case 4: resultado = y; default: resultado = z;\}} \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.0083 cm} x{12.2617 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Bucles}} \tn % Row 0 \SetRowColor{LightBackground} for \seqsplit{simplificado} & for (String str : list) \{ \seqsplit{ System.out.println(str);} \}\{\{nl\}\}Object{[}{]} humidades = new Object{[}10{]}; for( Object h : humidades )\{ \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} for & for (int i = 0; i \textless{} list.size(); i++) \{ \seqsplit{ System.out.println(list.get(i));} \} \tn % Row Count 8 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.727 cm} p{1.727 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Packages}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc. A Package can be defined as a grouping of related types (classes, interfaces, enumerations and annotations ) providing access protection and namespace management.} \tn % Row Count 7 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.7715 cm} x{9.4985 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Clase Scanner}} \tn % Row 0 \SetRowColor{LightBackground} Para string & Scanner sc = new Scanner(System.in); \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} & String a = sc.nextLine(); \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Para char & c = sc.next().charAt(0); \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Después de un scan.nextInt() & sc.nextLine(); \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.1169 cm} x{9.1531 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Tipos primitivos}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{En Java hayq ue especificar lso tipos primitivos antes que usarlos.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} String & String.length(); String usa " " \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} char & char usa ' ' \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} Para forzar tipos (explicit casting) & int randomNumber = \seqsplit{(int)(Math.random());} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Interfaces}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Un interfaz es una lista de acciones que puede llevar a cabo un determinado objeto. En una clase además de aparecer los métodos aparecía el código para dichos métodos, en cambio en un interfaz sólo existe el prototipo de una función, no su código.\{\{nl\}\}En java un interfaz define la lista de métodos, pero para que una clase posea un interfaz hay que indicar explícitamente que lo implementa mediante la claúsula implements. \{\{nl\}\}} \tn % Row Count 9 (+ 9) % Row 1 \SetRowColor{white} Donde modif.visibilidad puede ser public o bien sin especificar, es decir visibilidad pública (desde cualquier clase se puede emplear el interfaz) o de paquete (sólo se puede emplear desde clases del mismo paquete).\{\{nl\}\}nombreInterfaz por convenio sigue las mismas reglas de nomenclatura que las clases, y en muchos casos acaba en able (que podíamos traducir como: ser capaz de). \{\{nl\}\}-La claúsula opcional extends se emplea para conseguir que un interfaz hereda las funciones de otro/s interfaces, simplemente listaInterfaces es una lista separaada por coma de interfaces de los que se desea heredar.\{\{nl\}\}- & `{[}modif.visibilidad{]} interface nombreInterfaz {[}extends listaInterfaces{]} \{ prototipo método1; .. prototipo método1; \} ` \tn % Row Count 40 (+ 31) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Interfaces (cont)}} \tn % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{En muchas ocasiones un interfaz es empleado para definir un comportamiento, que posteriormente será implementado por diversas clases, que podrían no tener nada que ver entre ellas, pero que todas se comportarán igual de cara al interfaz. Es decir, todas tendrán las funciones indicadas por el interfaz.} \tn % Row Count 7 (+ 7) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{Cuando varios objetos de distintas clases pueden responder al mismo mensaje (función), aún realizando cosas distintas se denomina {\bf{polimorfismo}}.} \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements. Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.} \tn % Row Count 24 (+ 14) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{An interface can contain any number of methods.} \tn % Row Count 25 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.} \tn % Row Count 28 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{The byte code of an interface appears in a .class file.} \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Interfaces (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.} \tn % Row Count 3 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{However, an interface is different from a class in several ways, including −} \tn % Row Count 5 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{You cannot instantiate an interface.} \tn % Row Count 6 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{An interface does not contain any constructors.} \tn % Row Count 7 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{All of the methods in an interface are abstract.} \tn % Row Count 8 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.} \tn % Row Count 11 (+ 3) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{An interface is not extended by a class; it is implemented by a class.} \tn % Row Count 13 (+ 2) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{An interface can extend multiple interfaces.} \tn % Row Count 14 (+ 1) % Row 16 \SetRowColor{LightBackground} & contrato entre as clases concretas que a implementen, xa que a clase que o faga atópase obrigada a definir os métodos abstractos que a compoñen. \tn % Row Count 22 (+ 8) % Row 17 \SetRowColor{white} & Podemos dicir que as interfaces simulan a herdanza múltiple xa que unha clase pode implementar calquera numero de interfaces, ademais as interfaces poden herdar un ou mais números de interfaces mediante a palabra extends \tn % Row Count 34 (+ 12) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Interfaces (cont)}} \tn % Row 18 \SetRowColor{LightBackground} & Todos os métodos dunha interfaz son implicitamente public abstract, non é necesario especificalo na declaración do mesmo. \tn % Row Count 7 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\seqsplit{http://www.mundojava.net/interfaces.html?Pg=java\_inicial\_4\_5.html}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Ficheros}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{Crear ficheros}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Escribir fichero & File f = new File(rutayarchivo); \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{Escribir objetos a fichero}}} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} Primero hay que implementar la clase Serializable desde la clase del objeto & `public class Student implements Serializable \{\}` \tn % Row Count 8 (+ 4) % Row 4 \SetRowColor{LightBackground} Luego, abrir o crear un nuevo fichero con FileOutputStream & `FileOutputStream fileOut = new \seqsplit{FileOutputStream(filepath);`} \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} Crear un ObjectOutputStream dándole el anterior FileOutputStream como argumento al constructor. & `ObjectOutputStream objectOut = new \seqsplit{ObjectOutputStream(fileOut);`} \tn % Row Count 16 (+ 5) % Row 6 \SetRowColor{LightBackground} Usar el método \seqsplit{ObjectOutputStream.writeObject} para escribir el objeto que quieras al fichero. & \seqsplit{`objectOut.writeObject(serObj);`} \tn % Row Count 21 (+ 5) % Row 7 \SetRowColor{white} & for(int i=0;i\textless{}arrayper.size();i++) \{ \seqsplit{oos.writeObject(arrayper.get(i));} \} \tn % Row Count 27 (+ 6) % Row 8 \SetRowColor{LightBackground} Cerrar el flujo & objectOut.close(); \tn % Row Count 29 (+ 2) % Row 9 \SetRowColor{white} & `package \seqsplit{com.javacodegeeks.java.core;} import \seqsplit{java.io.FileOutputStream;} import \seqsplit{java.io.ObjectOutputStream;} public class ObjectIOExample \{ private static final String filepath="C:\textbackslash{}\textbackslash{}Users\textbackslash{}\textbackslash{}nikos7\textbackslash{}\textbackslash{}Desktop\textbackslash{}\textbackslash{}obj"; public static void main(String args{[}{]}) \{ ObjectIOExample objectIO = new ObjectIOExample(); Student student = new Student("John","Frost",22); \seqsplit{objectIO.WriteObjectToFile(student);} \} public void \seqsplit{WriteObjectToFile(Object} serObj) \{ try \{ FileOutputStream fileOut = new \seqsplit{FileOutputStream(filepath);} ObjectOutputStream objectOut = new \seqsplit{ObjectOutputStream(fileOut);} \seqsplit{objectOut.writeObject(serObj);} objectOut.close(); \seqsplit{System.out.println("The} Object was succesfully written to a file"); \} catch (Exception ex) \{ \seqsplit{ex.printStackTrace();} \} \} \}` \tn % Row Count 74 (+ 45) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Ficheros (cont)}} \tn % Row 10 \SetRowColor{LightBackground} Recuperar los atributos del objeto & ` public static Object crearObjeto( float altitud, int valor )\{ Humidade h = new Humidade( altitud, valor ); \seqsplit{System.out.println("} "+h+""+h.altitude+" "+h.valor); return h; \}` \tn % Row Count 11 (+ 11) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{Recuerda guardar los métodos de fichero en un try catch} \tn % Row Count 13 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{Escribir tetxo a ficheros}}} \tn % Row Count 14 (+ 1) % Row 13 \SetRowColor{white} Creando fichero al abrir el flujo & `FileOutputStream foos = new \seqsplit{FileOutputStream(new} \seqsplit{File("texto.txt"));`} \tn % Row Count 18 (+ 4) % Row 14 \SetRowColor{LightBackground} Usando fichero existente/cerar uno nuevo & FileOutputStream foos = new \seqsplit{FileOutputStream("texto2}.txt"); \tn % Row Count 21 (+ 3) % Row 15 \SetRowColor{white} Escribiendo la cadena en el fichero & \seqsplit{foos.write(cadena.getBytes("utf-8")}, 0, cadena.length()); \tn % Row Count 24 (+ 3) % Row 16 \SetRowColor{LightBackground} & `Primer parámetro: cadena a escribir, {\bf{getBytes()}}: This method encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.\{\{nl\}\}Segundo parámetro: lugar desde donde se dejó de escribir\{\{nl\}\}Tercer parámetro: número de carácteres a escribir` \tn % Row Count 40 (+ 16) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Ficheros (cont)}} \tn % Row 17 \SetRowColor{LightBackground} {\bf{Escribir float, etc a fichero (en binario)}} & \seqsplit{http://www.java2s.com/Tutorial/Java/0180\_\_File/WritefloattoafileusingDataOutputStream.htm} \tn % Row Count 5 (+ 5) % Row 18 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{{\bf{Leer objeto de fichero}}} \tn % Row Count 6 (+ 1) % Row 19 \SetRowColor{LightBackground} Abrir flujo & FileInputStream fis = new \seqsplit{FileInputStream("meteo}.dat"); \tn % Row Count 9 (+ 3) % Row 20 \SetRowColor{white} Abrir input de objeto & ObjectInputStream ois = new \seqsplit{ObjectInputStream(fis);} \tn % Row Count 12 (+ 3) % Row 21 \SetRowColor{LightBackground} & Object obj = ois.readObject();\{\{nl\}\}Persoa l = (Persoa) ois.readObject(); \tn % Row Count 16 (+ 4) % Row 22 \SetRowColor{white} & ObjectInputStream ois = new \seqsplit{ObjectInputStream(fis);} for ( int i = 0; i \textless{}persoas.size(); i++ )\{ Persoa per = (Persoa) ois.readObject(); \seqsplit{System.out.println(""+per.apelido+"} y "+per.cp); \seqsplit{persoasLeidas.add(per);} \} \tn % Row Count 31 (+ 15) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Ficheros (cont)}} \tn % Row 23 \SetRowColor{LightBackground} {\bf{Leer varios objetos de un fichero}} & catch \seqsplit{(FileNotFoundException} ex) \{ \seqsplit{Logger.getLogger(E3E1E2.class.getName()).log(Level.SEVERE}, null, ex); \}` \tn % Row Count 7 (+ 7) % Row 24 \SetRowColor{white} & try \{\{\{nl\}\}boolean engadir = true;\{\{nl\}\}FileInputStream fis = new \seqsplit{FileInputStream("meteo}.dat"); ObjectInputStream ois = new ObjectInputStream(fis);\{\{nl\}\}while(engadir)\{\{\{nl\}\}Object obj = ois.readObject();\{\{nl\}\}if(obj != null)\{objetosLeidos.add(obj); \{\{nl\}\}else\{engadir = false; \}\{\{nl\}\}for( int i = 0; i \textless{} \seqsplit{objetosLeidos.size();} i++ )\{\{\{nl\}\} \seqsplit{System.out.println("Objeto} leído "+objetosLeidos.get(i));\}\} \tn % Row Count 27 (+ 20) % Row 25 \SetRowColor{LightBackground} Hacer casting de un tipo objeto con el objeto que queremos & Humidade h = (Humidade) humidades.get(i); \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Ficheros (cont)}} \tn % Row 26 \SetRowColor{LightBackground} & public static ArrayList LER() throws \seqsplit{FileNotFoundException}, IOException, ClassNotFoundException\{ FileInputStream fis = new \seqsplit{FileInputStream("casas}.dat"); ObjectInputStream ois = new \seqsplit{ObjectInputStream(fis);} boolean saidaDatos = true; ArrayList\textless{}Casa\textgreater{} casasleidas = new ArrayList\textless{}\textgreater{}(); try\{ while( saidaDatos )\{ Casa casa = (Casa) ois.readObject(); \seqsplit{System.out.println("Casa} leida: "+casa+" ID "+casa.id+" NH "+casa.hab+" M "+casa.m2); \seqsplit{casasleidas.add(casa);} \} \}catch(EOFException ex)\{ \seqsplit{System.out.println("no} hay más que leer"); \} return casasleidas; \} \tn % Row Count 37 (+ 37) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Ficheros (cont)}} \tn % Row 27 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{\bf{Ficheros de acceso aleatorio}}} \tn % Row Count 1 (+ 1) % Row 28 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{} \tn % Row Count 1 (+ 0) % Row 29 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Random access files permit nonsequential, or random, access to a file's contents. To access a file randomly, you open the file, seek a particular location, and read from or write to that file.} \tn % Row Count 5 (+ 4) % Row 30 \SetRowColor{white} Crea un flujo de fichero de acceso aleatorio desde el que leer y, opcionalmente, al que escribir el archivo especificado por el argumento de Fichero/el archivo con el nombre especificado & \seqsplit{RandomAccessFile(File} file, String mode) \tn % Row Count 15 (+ 10) % Row 31 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{} \tn % Row Count 15 (+ 0) % Row 32 \SetRowColor{white} The \seqsplit{java.io.RandomAccessFile.seek(long} pos) method sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. & raf.seek(0); \tn % Row Count 24 (+ 9) % Row 33 \SetRowColor{LightBackground} & \seqsplit{System.out.println("Elemento} a visualizar"); int x = sc.nextInt(); RandomAccessFile raf = new \seqsplit{RandomAccessFile("indices}.ind", "r"); raf.seek(x); int posicion = raf.readInt(); RandomAccessFile rafO = new \seqsplit{RandomAccessFile("datos}.dat", "r"); \seqsplit{rafO.seek(posicion);} \seqsplit{System.out.println(""+rafO.readUTF());} \tn % Row Count 43 (+ 19) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\seqsplit{https://examples.javacodegeeks.com/core-java/io/fileoutputstream/how-to-write-an-object-to-file-in-java/}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.727 cm} p{1.727 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Superclase}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.727 cm} p{1.727 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Subclases}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.727 cm} p{1.727 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Determinar la clase de un objeto}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.7353 cm} x{10.5347 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Herencia}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{This allows you to establish a hierarchy for your classes. A class that inherits from some other class (referred to as a superclass) is called a subclass. While a subclass inherits methods and behaviors from a superclass, it can also declare new fields and methods (as well as override superclass methods).} \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{The subclass inherits state and behavior in the form of variables and methods from its superclass. The subclass can use just the items inherited from its superclass as is, or the subclass can modify or override it.} \tn % Row Count 12 (+ 5) % Row 2 \SetRowColor{LightBackground} Subclases & A subclass is defined with the extends keyword. For example, the syntax ClassB extends ClassA establishes ClassB as a subclass of of ClassA. Java only supports single inheritance, meaning a subclass cannot extend more than one superclass. \tn % Row Count 22 (+ 10) % Row 3 \SetRowColor{white} Constructores de subclases & Because a constructor initializes an instance of a class, they are never inherited; however, the subclass must call a superclass constructor as it is an extension of a superclass object. This can be done in either of the two ways shown below.\{\{nl\}\}class MySuperclass\{ // superclass instance variable: String myString; // superclass default (empty) constructor: MySuperclass()\{\} // superclass parameterized constructor: MySuperclass(String myString)\{ // initialize instance variable this.myString = myString; \} \}\{\{nl\}\}1) The subclass makes an explicit call to the superclass' parameterized constructor (i.e.: it calls super(...);): class MySubclass extends MySuperclass\{ // subclass constructor: MySubclass(String myString)\{ // explicit call to superclass constructor: super(myString); \} \}\{\{nl\}\}2) The subclass makes an implicit call to the superclass' default constructor (i.e.: a behind-the-scenes call to super(); happens automatically): class MySubclass extends MySuperclass\{ MySubclass(String myString)\{ // behind-the-scenes implicit call to superclass' default constructor happens // subclass can now initialize superclass instance variable: this.myString = myString; \} \}\{\{nl\}\}In the second example above, observe that we are initializing a field (myString) that isn't even declared in that class; the reason why this works is because it's inherited from MySuperclass and therefore can be accessed with the this keyword. \tn % Row Count 87 (+ 65) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.7353 cm} x{10.5347 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Herencia (cont)}} \tn % Row 4 \SetRowColor{LightBackground} Overriding & If a class inherits a method from its superclass, then there is a chance to override the method provided that it is not marked final.\{\{nl\}\}class Animal \{ public void move() \{ \seqsplit{System.out.println("Animals} can move"); \} \} class Dog extends Animal \{ public void move() \{ \seqsplit{System.out.println("Dogs} can walk and run"); \} \} public class TestDog \{ public static void main(String args{[}{]}) \{ Animal a = new Animal(); // Animal reference and object Animal b = new Dog(); // Animal reference but Dog object a.move(); // runs the method in Animal class b.move(); // runs the method in Dog class \} \} This program will throw a compile time error since b's reference type Animal doesn't have a method by the name of \seqsplit{bark.https://www.tutorialspoint.com/java/java\_overriding.htm} \tn % Row Count 35 (+ 35) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.7353 cm} x{10.5347 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Herencia (cont)}} \tn % Row 5 \SetRowColor{LightBackground} & As a subclass, your class inherits member variables and methods from its superclass. Your class can choose to hide variables or override methods inherited from its superclass. \tn % Row Count 8 (+ 8) % Row 6 \SetRowColor{white} & las subclases no heredan métodos privados \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{LightBackground} & se pueden usar métodos protected \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{white} & public class Oral extends Examen\{\{\{nl\}\} int temporesp; \{\{nl\}\} @Override\{\{nl\}\} public void ALTA()\{ \{\{nl\}\} super.ALTA(); \{\{nl\}\} Scanner sc = new Scanner(System.in); \{\{nl\}\} \seqsplit{System.out.println("tempo} de resposta"); \{\{nl\}\} this.temporesp = sc.nextInt();\{\{nl\}\} \} \tn % Row Count 25 (+ 13) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Abstracción}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Defien modelo de objeto} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{Las clases abstractas pueden o no tener métodos abstractos, métodos sin cuerpo ( public void get(); )} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} Las clases abstractas pueden tener también constructores & class TimesTwo extends Product \{ public TimesTwo() \{ super(2); \} \} \tn % Row Count 9 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{pero si tiene al menos un método abstracto deben ser clases abstractas} \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{No se pueden instanciar} \tn % Row Count 12 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{para suar una clase abstracta tienes que heredarla desde otra clase, y ofrecer implementaciones para sus métodos abstractos} \tn % Row Count 15 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Los métodos abstractos no tienen cuerpo y demandan que sus subclases incluyan ese método} \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{Todas las clases que hereden de la clase deben o Override el método abstracto o declararse ellas como abstractas} \tn % Row Count 20 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{public abstract class Animal \{ \{\{nl\}\} String name; \{\{nl\}\} abstract String sound(); //all classes that implement Animal must have a sound method \} \{\{nl\}\}\{\{nl\}\} public class Cat extends Animal \{ \{\{nl\}\} public Cat() \{ \{\{nl\}\} this.name = "Garfield"; \{\{nl\}\} \} \{\{nl\}\} @Override\{\{nl\}\} public String sound()\{ //implemented sound method from the abstract class \& method \{\{nl\}\} return "Meow!"; \{\{nl\}\} \} \{\{nl\}\}\}\{\{nl\}\}} \tn % Row Count 30 (+ 10) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Abstracción (cont)}} \tn % Row 9 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{A diferencia de las interfaces, no tienes por qué implementar todos los métodos} \tn % Row Count 2 (+ 2) % Row 10 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{If the subclass has a method with the same name as the parent's method that the subclass extends, the subclass' method overwrites the parent. If you want to use the parent class's method instead, you use the super keyword, like this: super.startRobot();} \tn % Row Count 8 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.727 cm} x{15.543 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Métodos de cadena}} \tn % Row 0 \SetRowColor{LightBackground} & X2 = sc.nextLine(); X2 = X2.toUpperCase(); \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.727 cm} x{15.543 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Polimorfismo}} \tn % Row 0 \SetRowColor{LightBackground} & Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} & class Animal \{ public void animalSound() \{ System.out.println("The animal makes a sound"); \} \} class Pig extends Animal \{ public void animalSound() \{ System.out.println("The pig says: wee wee"); \} \} class Dog extends Animal \{ public void animalSound() \{ System.out.println("The dog says: bow wow"); \} \} \tn % Row Count 14 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.727 cm} x{15.543 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Abstracción}} \tn % Row 0 \SetRowColor{LightBackground} & Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} & Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from). \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} & An abstract class can have both abstract and regular methods \tn % Row Count 10 (+ 2) % Row 3 \SetRowColor{white} & // Abstract class abstract class Animal \{ // Abstract method (does not have a body) public abstract void animalSound(); // Regular method public void sleep() \{ \seqsplit{System.out.println("Zzz");} \} \} // Subclass (inherit from Animal) class Pig extends Animal \{ public void animalSound() \{ // The body of animalSound() is provided here System.out.println("The pig says: wee wee"); \} \} \tn % Row Count 22 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.6991 cm} x{11.5709 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Interface}} \tn % Row 0 \SetRowColor{LightBackground} & An interface is a completely "abstract class" that is used to group related methods with empty bodies: \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} & // interface interface Animal \{ public void animalSound(); // interface method (does not have a body) public void run(); // interface method (does not have a body) \} \tn % Row Count 11 (+ 7) % Row 2 \SetRowColor{LightBackground} & To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends). The body of the interface method is provided by the "implement" class: \tn % Row Count 20 (+ 9) % Row 3 \SetRowColor{white} multiple interfaces & // DemoClass "implements" FirstInterface and SecondInterface class DemoClass implements FirstInterface, SecondInterface \{ \tn % Row Count 25 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.727 cm} x{15.543 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Java Enums}} \tn % Row 0 \SetRowColor{LightBackground} & An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} & enum Level \{ LOW, MEDIUM, HIGH \} \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} & public class MyClass \{ enum Level \{ \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{You can also have an enum inside a class:} \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \end{document}