FUNDAMENTALS OF THE JAVA PROGRAMMING LANGUAGE (SL-110) CAPÍTULO 11: CREACIÓN Y USO DE ARREGLOS Ing. Ronald Criollo
AGENDA Arreglos. Declaración de Arreglos. Inicialización de Arreglos. Uso de Arreglos. Arreglos Multidimensionales.
ARREGLOS Es una colección de OBJETOS o variables PRIMITIVAS. Es útil cuando el numero de variables de referencia es muy grande.
ARREGLOS Todos los miembros de un arreglo son del mismo tipo y tamaño. El acceso a los miembros es rápido y eficiente.
ARREGLOS
ARREGLOS
DECLARACIÓN Sintaxis Ejemplo type [] array_identifier; char [] status; int [] ages; Shirt [] shirts; String [] names;
INSTANCIACION Sintaxis Ejemplo array_identifier = new type [length]; status = new char [20]; ages = new int [5]; names = new String [7]; shirts = new Shirt [3];
INICIALIZACION Sintaxis Ejemplo array_identifier[index] = value; ages[0] = 19; ages[1] = 42; ages[2] = 92; ages[3] = 33; ages[4] = 46; shirts[0] = new Shirt();
INSTANCIACION
INDICE Para el acceso a los MIEMBROS de un ARREGLO es usado un INDICE. Indice es un tipo de dato int. Para acceder al primer miembro es 0. status[0] = ’3’; names[1] = "Fred Smith"; ages[1] = 19; prices[2] = 9.99F;
DECLARACION, INSTANCIACION E INICIALIZACION DE UN ARREGLO UNIDIMENSIONAL Sintaxis type [] array_identifier = {comma-separated list of values or expressions}; Ejemplo
INICIALIZACION DE ARREGLO DE OBJETOS La inicialización de arreglos de objetos requiere que los objetos esten instanciados primero. Account[] accountList; accountList = new Account[2]; accountList[0] = new Account(203.50); accountList[1] = new Account(4500.00);
INICIALIZACION DE ARREGLO DE OBJETOS
ACCESANDO A ELEMENTOS DE UN ARREGLO El tipo de dato del Indice debe ser int. El valor del Indice puede ser resultado de un calculo. loopValue = arrayOfInts[index – 1]; System.arraycopy() puede ser usado para copiar el contenido de un arreglo a otro System.arraycopy(arrayOfInts, 0, anotherArrayOfInts, 0, arrayOfInts.length); Account[] accountList; accountList = new Account[2]; accountList[0] = new Account(203.50); accountList[1] = new Account(4500.00);
RECORRIENDO UN ARREGLO UNIDIMENSIONAL int [] myArray; myArray = new int[100]; for (int count = 0; count < myArray.length; count++) { myArray[count] = count; } Account[] accountList; accountList = new Account[2]; accountList[0] = new Account(203.50); accountList[1] = new Account(4500.00);
Los miembros de un arreglo pueden ser buscados y ordenados. USO DE ARREGLOS Un arreglo puede ser una forma de pasar un largo numero de argumentos a un metodo. Arreglos paralelos pueden ser usados para almacenar informacion relacionada. Los miembros de un arreglo pueden ser buscados y ordenados.
ARREGLOS MULTIDIMENSIONALES Los arreglos multidimensionales pueden ser usados en vez de los arreglos paralelos. Son declarados con multiples pares de []. int[][] multiArray = new int[20][30]; Usa un Indice independiente para cada dimension. loopValue = multiArray[3][6];
ARREGLOS MULTIDIMENSIONALES
RECORRIENDO UN ARREGLO MULTIDIMENSIONAL Lazos FOR anidados pueden ser usador para recorrer los elementos de un arreglo multidimensional. public int[][] myArray = new int[4][5]; for(x = 0; x < 4; x++) { for(y = 0; y < 5; y++) { myArray[x][y] = x * y; }