Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of...

21
Arrays--data structure
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    0

Transcript of Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of...

Arrays--data structure

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;

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.

Array initialization 6

Arrays come in various dimensions. Before an array can be used, it must

be declared.int[] numbers;int[] numbers= new int[5] .--------------- -0 0 0 0 0numbers0 1 2 3 4

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.

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".

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 int[5];

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.

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.

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 0numbers0 1 2 3 4 Index

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 numbers[0] is 54; the contents of numbers[1] is 26, etc.

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.

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 int[SIZE];// input the numbers into the array System.out.println("Input " + SIZE + "integers,

one per line\n");

Programming example:

for (int index = 0; index !=SIZE; index++) {

System.out.print("cell" + index + " "); numbers[index] =

Console.readInt(); }// find and output the largest number in

the array largest = numbers[0];

Programming example: 3

for (int index=1; index != SIZE; index++)

if (numbers[index] > largest) largest = numbers[index];

System.out.println("\nLargest number in the array is " + largest);}}

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.

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.

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.

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)

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

Specification 6

Design ideas int index--number of elements in array double sum--the sum of the array

elements double number--entries from user double[]data--array of user entered

numbers int i--loop control variable