Arrays--data structure - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays--data structure

Description:

Consider how you would store five integer values in the memory of the computer. ... Notice that the five cells are pointed at or referenced to by identifier 'numbers' ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 22
Provided by: University482
Learn more at: http://pirate.shu.edu
Category:
Tags: arrays | data | five | structure

less

Transcript and Presenter's Notes

Title: Arrays--data structure


1
Arrays--data structure
2
Arrays 7
  • Consider how you would store five integer values
    in the memory of the computer.
  • One way to do this is to create five variable
    names and assign value to each
  • int number1 54
  • int number2 26
  • int number3 99
  • int number4 -25
  • int number5 13

3
Array definition 3
  • If the same approach was used for storing 50
    values, coding would be tedious.
  • Need a better data structure for storage that
    reduces coding to a minimum.
  • An array is a named collection of one or more
    items of the same data type. Each individual
    element of data can be accessed by the name of
    the array and a numbered index indicating its
    position within the array.

4
Array initialization 6
  • Arrays come in various dimensions.
  • Before an array can be used, it must be declared.
  • int numbers
  • int numbers new int5
  • .--------------- -?0 0 0 0 0
  • numbers 0 1 2 3 4

5
Explanation 3
  • int numbers has created only one memory
    location to represent the identifier numbers.
  • This location is automatically initialized to
    the value null
  • In order to allocate space for storing integers,
    it is necessary to use the keyword new, and
    specify an amount of memory to allocate for the
    storage of, in this example, five integers.

6
Memory allocation 1
  • In the example, each allocated memory cell is
    indexed with a value from 0 to 4, and the
    contents of the five cells have automatically
    been initialized to zero. Notice that the five
    cells are pointed at or referenced to by
    identifier "numbers".

7
Memory allocation 5
  • The syntax for the declaration of a 1-dimensional
    array follows
  • type-specifier array-name
  • new type-specifier number of cells
  • For example, the array depicted above can be
    declared as
  • int numbers new int5

8
Storing numbers in the Array
2
  • This states that the name of the array is
    "numbers." It is an array containing 5 cells,
    having subscripts numbered 0 to 4, respectively.
    The contents of this array are of type int.
  • There are two techniques for storing numbers in
    an array. The first is by initialization at the
    point of declaration, and the second by assigning
    the numbers to cells directly.

9
Declaration of the Array 4
  • Modification of the declaration of the
    one-dimensional array
  • type-specifier array-name list of numbers
  • i.e., int numbers 45,36,99,-25,13
  • Since the compiler can count the number of items
    of data in the list, there is no need to
    explicitly state how much memory to allocate in
    order to store the numbers.

10
Declaration of the Array 8
  • Syntax
  • One dimensional array declaration
  • type specifier array-name
  • new type-specifier number of cells
  • i.e. int numbers new int 5
  • .--------------- -?0 0 0 0 0
  • numbers 0 1 2 3 4
  • Index

11
Accessing elements 2
  • Access to numbers in the array is through the
    name of the array, followed by the index, in
    square brackets, of where the number is stored.
  • For example, the contents of numbers0 is 54
    the contents of numbers1 is 26, etc.

12
Declaring Size 2
  • Tip a good programming practice is to replace
    the numeric literal that defines the number of
    cells in the array with a constant.
  • Thus, if the size of the array changes, the only
    statement in the program that needs to be
    modified is the declaration of the constant.

13
Programming example
  • /Program to input to input numbers into a
    one-dimensional array and display the contents of
    the array/
  • final int SIZE 5 //size of the array
  • int numbers new intSIZE
  • // input the numbers into the array
  • System.out.println("Input " SIZE "integers,
    one per line\n")

14
Programming example
  • for (int index 0 index !SIZE index)
  • System.out.print("cell" index " ")
  • numbersindex Console.readInt()
  • // find and output the largest number in the
    array
  • largest numbers0

15
Programming example 3
  • for (int index1 index ! SIZE index)
  • if (numbersindex gt largest) largest
    numbersindex
  • System.out.println("\nLargest number in the array
    is " largest)

16
Dynamic Structure 1
  • In Java, it is possible to postpone assigning a
    value for the size of the array until program
    execution. In doing this, you create a dynamic
    array, and hence tailor the storage requirements
    to the amount of data available.

17
Summary of the main points
2
  • The contents of the array must be of the same
    data type. In other words, an array can contain
    all integers or all reals or all characters or
    all strings, but not a mixture of types.
  • Each item in the array is stored in a separate
    cell. If an array contained five integers, then
    each integer would occupy a single cell.

18
Summary of the main points
3
  • Each cell has a unique location value that shows
    the cell's position within the array. This
    location is known as an index and starts at value
    0.
  • The array is given one name, no matter how many
    items it contains
  • Before an index can be used, it must be declared
    like any other variable.

19
Summary of the main points
2
  • An item of data within a cell is accessed by
    using the name of the array followed by the
    position, index or subscript, within square
    brackets.
  • InitArrayA.java (cubes)

20
Specification 4
  • average_float.java
  • pre a preordained number of numbers
  • post the average of the numbers
  • design idea
  • get numbers via console
  • put into array
  • sum up elements of array
  • divide by number of numbers
  • report results

21
Specification 6
  • Design ideas
  • int index--number of elements in array
  • double sum--the sum of the array elements
  • double number--entries from user
  • doubledata--array of user entered numbers
  • int i--loop control variable
Write a Comment
User Comments (0)
About PowerShow.com