Sometimes we find problems in which they have a need to handle large volumes of information of the same type. For example, suppose you want to manipulate the payroll of a company which is 150 employees in production. It want to control the payment of individual workers based on hours worked and should be considered deductions for the savings fund and insurance.
Although there are many ways to solve this type of poblem a good solution is to use data structures that allow us to manipulate information quickly and organized. This time I will discuss the use of arrangements, the first form of data structures, perhaps the simplest.
A under a structure or collection of items of the same type of fixed length, that is, a memory segment of our computer that we can store a fixed number of elements in a row, for example: a list of phone numbers of employees of a company or a list of dates of birth of the companions of classroom. Arrangement is represented graphically as follows:
data are stored consecutively one after the other from the position 0 (zero) to array length minus 1. If you have created an array of length 10 will begin to store the data from position 0 to position 9, as shown in the picture above. In Java, arrays can be data structures of primitive or composite, for example arrays of integers, floating point numbers or strings and arrays of objects and GUI components.
How arrangements are made in Java?
To create an array of any type in Java can use the following forms:
In line 2 we declare an array called ages of integer (int ). For syntax, all elements which are declared as arrays will be placed the corchetes ( [ ] ) en su declaración. En Java los corchetes de los arreglos pueden ir antes del identificador o después del mismo, tal como se ve en la línea 8 de la imagen. El operador new hace que se reserve el espacio de memoria suficiente para la manipulación del arreglo. En la línea 3 se inicializa el arreglo de enteros con una longitud de 10 posiciones de memoria. Mientras que en la línea 5 declaramos e inicializamos en la misma línea un arreglo de calificaciones de tipo doble.
Si deseamos asignarle valores al arreglo en su inicialización, tal como lo hacemos con las variables o constantes, entonces no se especifica la longitud del arreglo, aunque en su declaración seguiremos placing the brackets after the type of data, after this post the initial values \u200b\u200bof the array inside braces ({} ) and separating each array element with a comma. In the example of line 8 are declaring an array of strings, so the values \u200b\u200bare placed in quotation marks.
By using the operator new virtual machine automatically initialize the array elements. Some examples:
The following code declares an array of integers and print its contents:
The following code declares an array of integers and assign a random value to each position of the later prints the values \u200b\u200bcontained in the array:
In line 2 we declare an array called ages of integer (int ). For syntax, all elements which are declared as arrays will be placed the corchetes ( [ ] ) en su declaración. En Java los corchetes de los arreglos pueden ir antes del identificador o después del mismo, tal como se ve en la línea 8 de la imagen. El operador new hace que se reserve el espacio de memoria suficiente para la manipulación del arreglo. En la línea 3 se inicializa el arreglo de enteros con una longitud de 10 posiciones de memoria. Mientras que en la línea 5 declaramos e inicializamos en la misma línea un arreglo de calificaciones de tipo doble.
Si deseamos asignarle valores al arreglo en su inicialización, tal como lo hacemos con las variables o constantes, entonces no se especifica la longitud del arreglo, aunque en su declaración seguiremos placing the brackets after the type of data, after this post the initial values \u200b\u200bof the array inside braces ({} ) and separating each array element with a comma. In the example of line 8 are declaring an array of strings, so the values \u200b\u200bare placed in quotation marks.
By using the operator new virtual machine automatically initialize the array elements. Some examples:
- integer elements are initialized to 0.
- floating elements and double is initialized with 0.0
- string elements are initialized to null .
The following code declares an array of integers and print its contents:
The following code declares an array of integers and assign a random value to each position of the later prints the values \u200b\u200bcontained in the array:
0 comments:
Post a Comment