• Tidak ada hasil yang ditemukan

Lesson 4 Chap (9) - Array.pptx (699Kb)

N/A
N/A
Protected

Academic year: 2018

Membagikan "Lesson 4 Chap (9) - Array.pptx (699Kb)"

Copied!
46
0
0

Teks penuh

(1)

Lesson 4 - Array

(2)

Objectives

 Whats is an array?  Declaring an array?  Initialization

 Array operations – using “for” loop with

an array

(3)

Array, why do we care?

 The car example from the previous

chapter on object-oriented

programming.

 You may remember we developing a

program that contained multiple instances of a class, that is, two object.

(4)

Array, why do we care?

 How could I take this further and write a program with 100 car objects?

 We could try some ways, perhaps, with some

clever copying and pasting, you might write a program with the following begining:

Car myCar1; Car myCar 2; Car myCar 3; ....

(5)

Array, why do we care?

 An array will allow us to take these 100 lines of code and put them into one line.

 Instead of having 100 variables, an array is one thing that contains a list of variables.

(6)

What is an array?

 From the Chapter 4, you may recall that a variable is a named pointer to a location in memory where data is stored.

 In other words, variables allow programs to keep track of information over a period of time.

 An array is exactly the same, only instead of pointing to one singular piece of information, an array points to multiple pieces.

(7)

What is an array?

(8)

What is an array?

 You can think of an array as a list of variables. A list, it should be noted, is useful for two important reasons.

a. Number one, the list keeps track of the elements in the list themselves.

b. Number two, the list keeps track of the order of those elements (which element is the frst in the list, the second, the third, etc).

(9)

What is an array?

 In an array, each element of the list has a unique index, an integer value that designates its position in the list (element #1, element #2, etc).

 In all cases, the name of the array refers to the list as a whole, while each element is accessed via its position.

(10)

What is an array?

(11)

Declaring and Creating

Array

 In Chapter Variables, we learned that all

variables must have a name and a data type.

 Arrays are no diferent.

 The declaration statement, however,

does look diferent.

 We denote the use of an array by placing

(12)

Declaring and Creating

Array

 For example, we started with an array of

primitive values, for example, integers.

(13)

Declaring and Creating

Array

 The declaration in Figure 9.3 indicates that

“arrayOfInts” will store a list of integers.

 The array name “arrayOfInts” can be

absolutely anything you want it to be (we only include the word “array” here to illustrate what we are learning).

 One fundamental property of arrays,

however, is that they are of fxed.

 One we defne the size for an array it can

(14)

Declaring and Creating

Array

(15)

Declaring and Creating

Array

 The Array declaration in Figure 9.4 allow us to specify the array size: how many elements we want the array to hold (or, technically, how much memory in the computer we are asking for store our beloved data).

(16)

Declaring and Creating

Array

Example 9-1: Additional array

declaration and creation examples!

a. Float [] scores = new foat [4];

b. Human [] people = new Human [100];

c. Int num = 50;

Car [] cars = new Car[num];

d. Spaceship[] ships = new

(17)

Declaring and Creating

Array

Exercise 9-1: Write the declaration

(18)

Declaring and Creating

Array

Answer of Exercise 9-1:

 Int [] a = new int [30];

(19)

Declaring and Creating

Array

Exercise 9-2: Which of the following

(20)

Declaring and Creating

Array

Answer of Exercise 9-2:

VALID

No VALID

VALID

VALID

(21)

Initializing an Array

 One way to fll an array is to hard-code

the values stored in each spot the array.

Example 9-2: Initializing the

(22)

Initializing an Array

 As you can see, we refere to each

element of the array individually by specifying an index, starting at 0.

The syntax for this is the name of

(23)

Initializing an Array

 A second option for initializing an array

is to manually type out a list of values enclosed in curly braces and separated by commas.

Example 9-3: initializing the

(24)

Array Operations

 Consider, for a moment, the following

problem:

(A) Create an array of 1000 foattng

potnt numbers.

(B) Intttaltze every element of that

(25)

Array Operations

 Part A we already know how to do.

 what we want to avoid is having to do

(26)

Array Operations

 Let’s describe in English what we want to

program:

 For every number n from 0 to 99,

(27)

Array Operations

(28)

Array Operations

 Unfortunately, the situation has not

improved.

 We have, nonetheles, taken a big leap

(29)

Array Operations

Example 9-4: Using a while loop to

(30)

Array Operations

 A for loop allows us to be event more concise,

as example 9-5 shows.

Example 9-5: Using a for loop to initialize

all elements of an array

What was once 1000 lines of code is now

(31)

Array Operations

 We can exploit the same technique for

(32)

Array Operations

For example, we could the array and

(33)

Array Operations

There is one problem with Example 9-6:

the use of the hard-coded value 1000. Striving to be better programmers, we should always question the existence of a hard-coded number.

In this case, what if we wanted to change

the array to have 2000 elements?

If our program was very long with many

(34)

Array Operations

Fortunately for us, Processtng gives

us a nice means for accessing the size of an array dynamically, using the dot syntax we learned for Objects in Objects.

Lengthis a property of every array and

(35)

Array Operations

Let’s use length while clearing an

(36)

Array Operations

Exercise 9-6: Assuming an array of

10 integers, thats is

Write code to perform the following

(37)

Array Operations

Exercise 9-6: Assuming an array of

10 integers, thats is

 For (int i = 0; i < nums.length; i++){

 Nums[i] = nums[i] * nums[i];

(38)

Array Operations

Exercise 9-6: Assuming an array of

10 integers, thats is

 For (int i = 0; i < nums.length; i++){

 Nums[i] += int(random(0,10));

(39)

Array Operations

Exercise 9-6: Assuming an array of

10 integers, thats is

 For (int i = 0; i < nums.length-1; i++){

 Nums[i] += nums[i];

(40)

Array Operations

 Int total = 0;

 For (int i = 0; i < nums.length; i++){

 total+= nums[i];

(41)

Simple Array Example: The

Snake

Example 9-8: A snake following the

(42)

Simple Array Example: The

Snake

Example 9-8: A snake following the

(43)

Simple Array Example: The

Snake

Example 9-8: A snake following the

(44)

Simple Array Example: The

Snake

Example 9-8: A snake following the

(45)

Simple Array Example: The

Snake

Example 9-8: A snake following the

(46)

Referensi

Dokumen terkait

The results of logistic regression showed that there was a relationship between cigarette advertising illustrated the dangers of smoking and smoking behavior

SIKLUS BELAJAR EMPIRIS-INDUKTIF UNTUK MENINGKATKAN KETERAMPILAN BERPIKIR KRITIS SISWA PADA TOPIK LAJU REAKSI.. Universitas Pendidikan Indonesia | repository.upi.edu

Dengan ini peneliti meyatakan bahwa tesis yang berjudul “Analisis Faktor-Faktor Yang Mempengaruhi Kinerja Auditor Dengan Stres Kerja Sebagai Variabel Moderating

Bila dibandingkan antar konsentrasi alginat pada CaCl 2 0,75 % aktivitas antioksidan saling tidak berbeda nyata, sedangkan antar konsentrasi alginat pada CaCl 2 1 %

[r]

[r]

diatas memperlihatkan bahwa pada sampel air minum depot B, positif adanya bakteri Coliform karena terjadi pembentukan gas pada tabung Durham dan terjadi perubahan

Khawarita Siregar, MT, selaku Ketua Departemen Teknik Industri dan yang telah memberi motivasi sehingga penulis dapat menyelesaikan laporan Tugas Sarjana ini.. Ukurta Tarigan,