1 / 19

Arrays

Arrays. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type. c[ 0 ]. Name of array (Note that all elements of this array have the same name, c ). -45 6 0 72 1543 -89 0 62 -3 1 6453 78. c[ 1 ]. c[ 2 ]. c[ 3 ].

rasul
Download Presentation

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 • Array • Group of contiguous memory locations • Each memory location has same name • Each memory location has same type

  2. c[ 0 ] Name of array (Notethat all elements ofthis array have thesame name, c) -45 6 0 72 1543 -89 0 62 -3 1 6453 78 c[ 1 ] c[ 2 ] c[ 3 ] c[ 4 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] Position number (indexof subscript) of the element within array c c[ 9 ] c[ 10 ] c[ 11 ] A 12-element array.

  3. Arrays (cont.) • Examine array c • c is the array name • c.length accesses array c’s length • c has 12 elements ( c[0], c[1], … c[11] ) • The value of c[0] is –45 • The brackets ([]) are in highest level of precedence in Java

  4. Declaring and Allocating Arrays • Declaring and Allocating arrays • Arrays are objects that occupy memory • Allocated dynamically with operator new int c[] = newint[ 12 ]; • Equivalent toint c[]; // declare array c = newint[ 12 ]; // allocate array • We can allocate arrays of objects too String b[] = new String[ 100 ];

  5. Allocating an Array and Initializing Its Elements • Show how to • Declare array • Allocate array • Initialize array elements

  6. Declare array as an array of ints Allocate 10ints for array; each int is initialized to 0 by default array.length returns length of array array[counter] returns int associated with index in array

  7. Each int is initialized to 0 by default

  8. Using an Initializer List to Initialize Elements of an Array • Initialize array elements • Use initializer list • Items enclosed in braces ({}) • Items in list separated by commas int n[] = { 10, 20, 30, 40, 50 }; • Creates a five-element array • Subscripts of 0, 1, 2, 3, 4 • Do not need operator new

  9. Each array element corresponds to element in initializer list

  10. Examples

  11. Calculatingthe Value to Store in Each Array Element • Calculate value stored in each array element • Initialize elements of 10-element array to even integers

  12. Summing the Elements of an Array • Array elements • Can represent a series of values • We can sum these values

  13. Using Histograms to Display Array Data Graphically • Present array values graphically • Histogram • Plot each numeric value as bar of asterisks (*)

More Related