Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of the computer. zOne way to do this is to create five.

Similar presentations


Presentation on theme: "Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of the computer. zOne way to do this is to create five."— Presentation transcript:

1 Arrays--data structure

2 Arrays 7 zConsider how you would store five integer values in the memory of the computer. zOne way to do this is to create five variable names and assign value to each: zint number1 = 54; zint number2 = 26; zint number3= 99; z int number4 = -25; z int number5 = 13;

3 Array definition 3 zIf the same approach was used for storing 50 values, coding would be tedious. zNeed a better data structure for storage that reduces coding to a minimum. zAn 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 zArrays come in various dimensions. zBefore an array can be used, it must be declared. zint[] numbers; zint[] numbers= new int[5] z.----------------  00000 znumbers01234

5 Explanation 3 z int[] numbers has created only one memory location to represent the identifier numbers. z This location is automatically initialized to the value null zIn 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 zIn 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 zThe syntax for the declaration of a 1- dimensional array follows: ztype-specifier [] array-name = z new type-specifier [number of cells] zFor example, the array depicted above can be declared as: zint[] numbers = new int[5];

8 Storing numbers in the Array 2 zThis 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. zThere 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 zModification of the declaration of the one- dimensional array: ztype-specifier [] array-name = {list of numbers} zi.e., int[] numbers = {45,36,99,-25,13} zSince 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 zSyntax: z One dimensional array declaration: z type specifier [] array-name = z new type-specifier [ number of cells]; zi.e. int [] numbers = new int [5]; z.----------------  00000 znumbers01234 z Index

11 Accessing elements 2 zAccess 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. zFor example, the contents of numbers[0] is 54; the contents of numbers[1] is 26, etc.

12 Declaring Size 2 zTip: a good programming practice is to replace the numeric literal that defines the number of cells in the array with a constant. zThus, 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: z/*Program to input to input numbers into a one-dimensional array and display the contents of the array*/ z final int SIZE = 5; //size of the array z int[] numbers = new int[SIZE]; z// input the numbers into the array z System.out.println("Input " + SIZE + "integers, one per line\n"); z

14 Programming example: zfor (int index = 0; index !=SIZE; index++) { z System.out.print("cell" + index + " "); z numbers[index] = Console.readInt(); } z// find and output the largest number in the array z largest = numbers[0];

15 Programming example: 3 zfor (int index=1; index != SIZE; index++) z if (numbers[index] > largest) largest = numbers[index]; zSystem.out.println("\nLargest number in the array is " + largest);}}

16 Dynamic Structure 1 zIn 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 zThe 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. zEach 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. z z

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. zInitArrayA.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 yint index--number of elements in array y double sum--the sum of the array elements y double number--entries from user ydouble[]data--array of user entered numbers yint i--loop control variable


Download ppt "Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of the computer. zOne way to do this is to create five."

Similar presentations


Ads by Google