• Tidak ada hasil yang ditemukan

ARRAYS (1)

N/A
N/A
Protected

Academic year: 2025

Membagikan "ARRAYS (1)"

Copied!
33
0
0

Teks penuh

(1)

ARRAYS

CT 1513

One-Dimensional

(2)

2

1. Introduction

2. Properties of an Array 3. Arrays Declaration

3.1 Examples

3.2 Using constants as sizes 3.3 Using expressions as sizes

3.4 Specifying sizes during execution 3.5 Initialization during declaration 4. Accessing Array Elements

5. length

6. Initialization of the array

6.1 Initialization during declaration 6.2 Using loops

6.3 User input 7. Programming Hint (1) 8. Programming Hint (2)

Outline

(3)

3

1. INTRODUCTION

 Simple data types (int, double, char, boolean) use a single memory cell to store a variable.

 For example, after the declaration of the following three variables (sum, choice and average), the memory layout may look as shown in the figure:

int sum = 0;

char choice = ‘x’;

double average = 0.0;

1 2 3

 Variables are dispersed in memory

 Names are translated into addresses

 The order of declaration is not necessarily the order of storing in the memory. In fact,

we don’t have any control on assigning addresses to variables.

Address Value

200 x

500 0

502 0.0

(4)

4

2. PROPERTIES OF AN ARRAY

 An array is a collection of two or more adjacent memory cells called array elements.

 Elements of the same array should be all of the same type: this represents the type of the array.

 Array elements are associated with a particular symbolic name (identifier name) that obeys all rules previously mentioned about identifiers.

 Therefore, to set up an array in memory, we must declare the following:

o The type of the array: which is the type of its elements o The name of the array

o The number of elements of the array: this is the number of adjacent memory cells associated with the array name

 However, it is sometimes more efficient to solve problems by grouping data items together for storage in main memory.

 For such types of problems, we use an array.

 Grouped data items are then processed using a loop.

(5)

One-Dimensional Arrays

Java Programming: From Problem Analysis to Program Design, 4e 5

(6)

One-Dimensional Arrays (continued)

Java Programming: From Problem Analysis to Program

Design, 4e 6

(7)

• Array num:

int[] num = new int[5];

7 Java Programming: From Problem Analysis to Program Design, 4e

Arrays initialization

When an array is instantiated, Java automatically initializes its elements to their default values.

numeric arrays are initialized to 0,

char arrays are initialized to the null character, which is '\u0000',

boolean arrays are initialized to false.

(8)

• Array num:

int[] num = new int[5];

Java Programming: From Problem Analysis to Program

Design, 4e 8

Arrays (continued)

To save space, we also draw an array, as shown in Figures 9-2(a) and 9-2(b).

(9)

One-Dimensional Arrays : access elements

Java Programming: From Problem Analysis to Program

Design, 4e 9

intExp = number of components in array >= 0

0 <= indexExp < intExp

(10)

ACCESSING ARRAY ELEMENTS

Java Programming: From Problem Analysis to Program

Design, 4e 10

(11)

ACCESSING ARRAY ELEMENTS

Java Programming: From Problem Analysis to Program

Design, 4e 11

(12)

ACCESSING ARRAY ELEMENTS

Java Programming: From Problem Analysis to Program

Design, 4e 12

(13)

13

ACCESSING ARRAY ELEMENTS

Example 2

 Consider the following declarations and their reflection in the memory:

double[] list = new double[10];

1

list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9]

list 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

int i = 2;

list[2*i-1] = 46.0; //list[3] = 46.0 2

3

list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9]

list 0.0 0.0 0.0 46.0 0.0 0.0 0.0 0.0 0.0 0.0

list[2*i+1] = 20.0; // i = 2 4

list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9]

list 0.0 0.0 0.0 46.0 0.0 20.0 0.0 0.0 0.0 0.0

list[7] = list[3] + list[5];//list[7] = 46.0 + 20.0 = 66.0 5

list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9]

list 0.0 0.0 0.0 46.0 0.0 20.0 0.0 66.0 0.0 0.0

(14)

14

ACCESSING ARRAY ELEMENTS

 Note the difference between the following two statements:

list[7] = list[3] + list[5];//list[7] = 46.0 + 20.0 = 66.0 list[3+5] = 10.0; //list[8] = 10.0

5 6

list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9]

list 0.0 0.0 0.0 46.0 0.0 20.0 0.0 66.0 10.0 0.0

(15)

Use constant to declare array size

15 Java Programming: From Problem Analysis to Program Design, 4e

(16)

Java Programming: From Problem Analysis to Program

Design, 4e 16

Specifying Array Size During Program

Execution

(17)

Java Programming: From Problem Analysis to Program

Design, 4e 17

The initializer list contains values, called initial values, that are placed between braces and separated by commas

sales[0]= 12.25 , sales[1]= 32.50 ,

sales[2]= 16.90 , sales[3]= 23.00 , and sales[4]= 45.68

Array Initialization During Declaration

(18)

Array Initialization During Declaration (continued)

Java Programming: From Problem Analysis to Program

Design, 4e 18

int[] list = {10, 20, 30, 40, 50, 60};

When declaring and initializing arrays, the size of the array is determined by the number of initial values within the

braces.

If an array is declared and initialized simultaneously, we

don’t use the operator new to instantiate the array object

(19)

Arrays and the Instance Variable length

Java Programming: From Problem Analysis to Program

Design, 4e 19

Associated with each array that has been instantiated, there is a public ( final ) instance variable length

The variable length contains the size of the array

The variable length can be directly accessed in a program

using the array name and the dot operator

(20)

Arrays and the Instance Variable length (continued)

Java Programming: From Problem Analysis to Program

Design, 4e 20

int[] list = {10, 20, 30, 40, 50, 60};

This statement creates the array list of six components and initializes the components using the values given

 Here list.length is 6

int[] numList = new int[10];

This statement creates the array numList of 10 components

and initializes each component to 0

(21)

Arrays and the Instance Variable length (continued)

Java Programming: From Problem Analysis to Program

Design, 4e 21

The value of numList.length is 10

numList[0] = 5;

numList[1] = 10;

numList[2] = 15;

numList[3] = 20;

These statements store 5 , 10 , 15 , and 20 , respectively, in the first four components of numList

You can store the number of filled elements, that is, the actual number of elements, in the array in a variable, say numOfElement

It is a common practice for a program to keep track of the

number of filled elements in an array

(22)

Array Index Out of Bounds Exception

double[] num = double[10];

int i;

The element num[i] is valid, that is, i is a valid index if i = 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.

The index—say, index—of an array is in bounds if index >= 0 and index <= arraySize - 1.

If either index < 0 or index > arraySize - 1, then we say that the index is out of bounds.

Java Programming: From Problem

Analysis to Program Design, 4e 22

(23)

Note on Array Declaration

Q: What is the difference between the following declaration ?

int alpha[], beta;

int[ ] gamma, delta;

Java Programming: From Problem

Analysis to Program Design, 4e 23

(24)

24

PROGRAMMING HINT

 The size of the array specified during instantiation cannot be changed.

 Sometimes, the programmer does not know how much elements will be needed.

 Therefore, it is common to specify a large size and use only the elements which were actually filled by the user.

 Assume that numOfElements is a variable that represents the number of actually filled elements within the array (numOfElements <= length)

 The value of numOfElements is used as a counter: it is initialized to zero, and then incremented with each new element filled in the array.

(25)

25

Self-Check Exercises

 Write a complete Java program that does the following:

o Initializes all array elements with the value ‘*’

o Accepts a set of Strings, takes the first character of the String and fills it in the array in sequence.

o Prints the array.

W8.1 Arrays (1)

 Write a complete Java program that stores the user input of integers into an array, and then initializes another array of the same size. The program then adds the corresponding elements of both arrays and puts the result in a third one.

 Write a complete Java program that stores the user input of integers into an array, and then prints it in the reverse order.

(26)
(27)

27

Arrays of Objects

(28)

Arrays of Objects

28

Can use arrays to manipulate objects

Example: create array named array1 with N objects of type T

T[] array1 = new T[N]

Can instantiate array1 as follows:

for ( int j = 0; j <array1.length; j++)

array1[j] = new T();

(29)

Array of String Objects

29

String[] nameList = new String[5];

nameList[0] = "Amanda Green";

nameList[1] = "Vijay Arora";

nameList[2] = "Sheila Mann";

nameList[3] = "Rohit Sharma";

nameList[4] = "Mandy Johnson";

(30)

Array of String

Objects(continued)

30

(31)

Arrays of Objects (continued)

31

Clock[] arrivalTimeEmp = new Clock[100];

(32)

Instantiating Array Objects

32

for (int j = 0; j < arrivalTimeEmp.length; j++) arrivalTimeEmp[j] = new Clock();

(33)

Instantiating Array Objects (continued)

33

arrivalTimeEmp[49].setTime(8, 5, 10);

Referensi

Dokumen terkait