1 / 7

Arrays

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.

iokina
Télécharger la présentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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”

More Related