Content Analysis

profileAnEsh
OODP101_Week5.pptx

Object Oriented Design and Programming

Week 5

A B Emran Salahuddin, Anchal Shrestha, Chaitali Samani (Sydney)

Hanspreet kaur, Allan NG (Melbourne)

Kent Institute Australia Pty. Ltd.

ABN 49 003 577 302 CRICOS Code: 00161E RTO Code: 90458 TEQSA Provider Number: PRV12051

Version 2 – 18th December 2015

1

SLIDE TITLE

Farrell, J. (2017) Programming Logic and Design, Comprehensive (9th ed.) Cengage Learning

2

2

Programming Logic and Design Ninth Edition

Arrays and basic Array operations

3

Objectives

Arrays- one dimensional and two dimensional.

Declaration, Creation and Initialization.

Some basic operations

4

Introduction to Arrays

In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification

A data structure is a way to organize data in a computer (usually to save time or space).

Data structures play an essential role in computer programming—indeed.

In this lecture, we introduce you to the idea of a data structure and to your first data structure, the array.

5

Introduction to Arrays(continued-1)

The primary purpose of an array :-

facilitate storing large quantities of data of same type.

manipulating large quantities of data of same type.

Have a large amount of data of same type to process?

First put all of the data into one or more arrays.

use indexing to refer to individual elements and to process the data.

6

Introduction to Arrays(continued-2)

You can use array in following scenarios:

To store exam scores of five exams.

To store stock prices.

To store various departing time of your train in a day from your nearby station.

And many more.

7

Array in Java

Making an array in a Java program involves three distinct steps:

-Declare the array.

-Create the array.

-Initialize the array elements.

8

Array in Java(continued-2)

To declare an array:-

- You need to specify a name and the type of data it will contain.

To create it :-

- You need to specify its length (the number of elements).

To initialize it :-

- You need to assign a value to each of its elements.

9

Array in Java(continued-3)

Following code makes an array of n(any number) elements, each of type double and initialized to 0.0:

10

Array in Java(continued-4)

The first statement was the array declaration.

It is just like a declaration of a variable of the corresponding primitive type

Except for the square brackets following the type name, which specify that we are declaring an array.

11

Array in Java(continued-5)

The second statement creates the array.

It uses the keyword new to allocate memory to store the specified number of elements. It has to be some number.

12

Array in Java(continued-6)

The for loop assigns the value 0.0 to each of the n array elements.

We refer to an array element by putting its index in square brackets after the array name.

The code a[i] refers to element i of array a[]

13

Array Initialization

Arrays can be initialized in different ways:

Using for loop- discussed in previous slide

Standard approach

double a[]={2.5,56,3.5,6.5,3.4,6.8}

Simpler way

a[i]= 2.5 where i can be position of element of array

14

Array in Java(continued-7)

Zero-based indexing

- The first element of an array a[] is a[0] not a[1].

- The second element is a[1] not a[2], and so forth.

Consider the array:

String[] SUITS = { "Clubs", "Diamonds", "Hearts", "Spades" };

What will be value of SUITS[0], SUITS[3]

15

Array in Java(continued-8)

Array length

Once you create an array in Java, its length is fixed.

Use .length to get a length of array.

For example:

double scores[]={2.5,56,3.5,6.5,3.4,6.8}

scores.length will be 6

16

Array in Java(continued-9)

Memory Representation

Arrays are fundamental data structures.

Direct correspondence with memory systems.

The elements of an array are stored consecutively in memory, so that it is easy to quickly access any array value.

17

Array in Java(continued-10)

Memory allocation

-When you use the keyword new to create an array, Java reserves sufficient space in memory to store the specified number of elements.

This process is called memory allocation.

18

The same process is required for all variables that you use in a program (but you do not use the keyword new with variables of primitive types because Java knows how much memory to allocate). We call attention to it now because it is your responsibility to create an array before accessing any of its elements. If you fail to adhere to this rule, you will get an uninitialized variable error at compile time.

18

Array in Java(continued-11)

19

Bounds checking

use valid indices when referring to an array element.

If you have created an array of length n and use an index whose value is less than 0 or greater than n-1.

program will terminate with an ArrayIndexOutOfBoundsException at run time

19

Array in Java(continued-12)

20

Exchanging two values in an array

Use the temporary variable to exchange the values

double temp= scores[1];

scores[1]=scores[2];

scores[2]=temp;

20

Array in Java(continued-13)

21

Typical array processing codes

21

Array in Java(continued-14)

22

Two Dimensional Array

What if teacher want to store student number and marks obtained?

Use two dimensional array

Similar to matrix of rows and columns.

Declaration and creation of two dimensional array

datatype [][] arrayname= new datatype[rows][columns]

int[][] arrayname= new int[4][2]

22

Array in Java(continued-15)

23

Two Dimensional Array Initialisation

int[][] anIntegerArray = new int[5][3]; anIntegerArray[0][0] = 10; anIntegerArray[0][1] = 20; anIntegerArray[0][2] = 30;

Another approach:

int[][] Employees = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} };

23

Array in Java(continued-16)

24

Two Dimensional Array: accessing the array elements

- Standard Approach

24

Array in Java(continued-17)

25

Two Dimensional Array: accessing the array elements

- Using for loop

25

Summary

Arrays are fourth basic element after assignments, conditionals and loops.

Basic data structure

One dimensional array to store sequence of values of same type.

We use indexing to refer to array elements.

Two dimensional array to store values of same type in form of matrix of various rows and columns.

26

26

kent.edu.au Kent Institute Australia Pty. Ltd. ABN 49 003 577 302 ● CRICOS Code: 00161E ● RTO Code: 90458 ● TEQSA Provider Number: PRV12051

27

27