FUNDAMENTALS OF THE JAVA PROGRAMMING LANGUAGE (SL-110) CAPÍTULO 11: CREACIÓN Y USO DE ARREGLOS...
-
Upload
hilario-ocanas -
Category
Documents
-
view
2 -
download
0
Transcript of FUNDAMENTALS OF THE JAVA PROGRAMMING LANGUAGE (SL-110) CAPÍTULO 11: CREACIÓN Y USO DE ARREGLOS...
FUNDAMENTALS OF THE JAVA
PROGRAMMING LANGUAGE(SL-110)CAPÍTULO 11: CREACIÓN Y USO DE ARREGLOS
Ing. Ronald Criollo
Arreglos. Declaración de Arreglos. Inicialización de Arreglos. Uso de Arreglos. Arreglos Multidimensionales.
Es una colección de OBJETOS o variables PRIMITIVAS.
Es útil cuando el numero de variables de referencia es muy grande.
Todos los miembros de un arreglo son del mismo tipo y tamaño.
El acceso a los miembros es rápido y eficiente.
Sintaxistype [] array_identifier;type array_identifier [];
Ejemplochar [] status;int [] ages;Shirt [] shirts;String [] names;
Sintaxisarray_identifier = new type [length];
Ejemplostatus = new char [20];ages = new int [5];names = new String [7];shirts = new Shirt [3];
Sintaxisarray_identifier[index] = value;
Ejemploages[0] = 19;ages[1] = 42;ages[2] = 92;ages[3] = 33;ages[4] = 46;shirts[0] = new Shirt();
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;
Sintaxistype [] array_identifier = {comma-separated
list of values or expressions}; Ejemplo
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);
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);
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);
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.
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];
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;
}
}