SlideServe
Menu
Préférences
© SlideServe · v2026

Arrays

iokina avatar
7 diapositives · 313 Views · Aug 08, 2014

Arrays. Contents. Declaration and Definition of an array Initialization of an array Accessing the elements of an array Traversing an array. Declaration of an array variable . Allocation of memory. Data type. Name of array variable. Indicates more than one integer. Arrays.

Arrays - slide 1
01 / 7Arrays
1 / 17

Télécharger la présentation

Arrays
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Arrays Contents • Declaration and Definition of an array • Initialization of an array • Accessing the elements of an array • Traversing an array

  2. Declaration of an array variable Allocation of memory Data type Name of array variable Indicates more than one integer Arrays Declaration and Definition An array is a fixed amount of contiguous memory that is allocated to hold data of a specified type. An array, for example, may hold a sequence of integers. To declare an array and allocate memory, the programmer must specify: The type of data that will be contained in the array A name for the array The length of the array – (The number of integers in the sequence) Example – Construct an array to contain a sequence of 10 integers int [ ] intArray; intArray = newint [10]; intArray 0

  3. intArray 0 An array variable is allocated an amount of memory necessary for holding a handle (address) regardless of the type data that the array will eventually hold. 0 0 0 0 0 0 0 0 0 0 When allocating memory to hold the sequence of integers, the length of the array must be specified. Arrays An array object (such as intArray) holds a handle (we avoid using the word pointer in java) to the first byte of allocated storage. In the example on the previous slide, no memory was allocated during the declaration, so a null value was assigned to the variable intArray. int [ ] intArray; Default initial array values are all 0 intArray = newint [10]; An amount of memory equal to 10 * sizeof (int) = 40 bytes is allocated with the address of the first byte of this allotment stored in the array variable intArray.

  4. composers Bach Mozart Beethoven Chopin Liszt The array holds handles to the five string objects Arrays Initialization of arrays An array may be initialized during declaration. String [ ] composers = {“Bach”, “Mozart”, “Beethoven”, “Chopin”, “Liszt”}; List elements match the declared type The size of the array is determined by the number of items in the sequence

  5. composers Bach Mozart Beethoven Chopin Liszt Arrays Accessing values stored in an array An array is an indexed sequence of a single kind of object. Elements of the array are located by their position (index value) in the sequence. 0 1 2 3 4 The numbering of the elements in a java array begins with 0

  6. 0 1 2 3 4 composers Bach Mozart Beethoven Chopin Liszt Hadyn Arrays To access one of the elements of an array you specify the name of the array and the index of the element. examples System.out.println(composers[1]); Mozart composers[3] = “Hadyn”; Assignment to the 4th position

  7. System.out.println(composers[i]); composers[i] = “Bach”; Arrays Traversing an array Every array has a public length attribute that can be accessed using dot notation. composers.length //has a value of 5 The array has a length of 5 elements which are contained in the range of 0 … composers.length - 1 Example of array traversal int size = composers.length; for (int i = 0; i < size; i++) { //do something } Print the list of composers Initialize all elements to “Bach”