Professor: Kevin Molloy (adapted from slides originally developed by - - PowerPoint PPT Presentation

professor kevin molloy adapted from slides originally
SMART_READER_LITE
LIVE PREVIEW

Professor: Kevin Molloy (adapted from slides originally developed by - - PowerPoint PPT Presentation

Professor: Kevin Molloy (adapted from slides originally developed by Alvin Chao) The real power of arrays is the ability to process them using loops, i.e., performing the same task for multiple elements. The standard form of iteration is as


slide-1
SLIDE 1

Professor: Kevin Molloy (adapted from slides originally developed by Alvin Chao)

slide-2
SLIDE 2

The real power of arrays is the ability to process them using loops, i.e., performing the same task for multiple elements. The standard form of iteration is as follows: for (int i = 0; i < array.length; i++) { ... process array[i] ... } For example: // set all of the elements of x to -1.0 double[] x = new double[100]; for (int i = 0; i < x.length; i++) { x[i] = -1.0; }

// sum the elements of scores

int sum = 0; for (int i = 0; i < scores.length; i++) { sum += scores[i]; }

slide-3
SLIDE 3

What is the value of array and accumulator after the following iteration? Trace the loop by hand.

int[] array = {5, 26, 13, 12, 37, 15, 16, 4, 1, 3}; int accumulator = 0; for (int i = 0; i < array.length; i++) { if (array[i] % 2 == 1 && i + 1 < array.length) { array[i] *= -1; accumulator += array[i+1]; } }

slide-4
SLIDE 4

Looping Over the Contents of an Array

l We often use a for loop to access each element in

an array:

for (int i = 0; i < names.length; i++) { System.out.println("Hello " + names[i]); }

l If only there were a better way…

for (String name : names) { System.out.println("Hello " + name); }

This variable will be assigned the elements from this array

slide-5
SLIDE 5

When To Use an Enhanced For Loop

l Always, unless:

  • Need to modify the array
  • Need to know the element index for some reason
  • Need to process the elements out of order
  • ...
slide-6
SLIDE 6

Exercise #1

1)What will be printed by the following code? 2)Where is the style problem in this code?

String[] summer = {"June", "July", "August"}; String letters = ""; for (String i : summer) { letters += i.charAt(0); } System.out.println(letters);

slide-7
SLIDE 7

Exercise #1

1)What will be printed by the following code?

1)JJA

2)Where is the style problem in this code?

This is not an index variable, it requires a meaningful name (like “month”).

String[] summer = {"June", "July", "August"}; String letters = ""; for (String i : summer) { letters += i.charAt(0); } System.out.println(letters);

slide-8
SLIDE 8

Exercise #2

l Complete the following method using an enhanced for

loop (reminder: use .equals to compare strings.)

/** * This method counts the number of times a target word occurs in * an array of words. Comparisons are case-sensitive. * * @param words - The array to search * @param target - The word to search for * @return The word count */ public static int countWord(String[] words, String target) { }

slide-9
SLIDE 9

Reference Arrays

Die[] dice; dice = new Die[4]; dice[0] = new Die(6); dice[2] = new Die(5); for (Die curDie : dice) { if (curDie != null) { curDie.roll(); } } dice

slide-10
SLIDE 10

Reference Arrays

Die[] dice; dice = new Die[4]; dice[0] = new Die(6); dice[2] = new Die(5); for (Die curDie : dice) { if (curDie != null) { curDie.roll(); } } dice null null null null

slide-11
SLIDE 11

Reference Arrays

Die face: 6 Die[] dice; dice = new Die[4]; dice[0] = new Die(6); dice[2] = new Die(5); for (Die curDie : dice) { if (curDie != null) { curDie.roll(); } } dice null null null

slide-12
SLIDE 12

Reference Arrays

Die face: 5 Die[] dice; dice = new Die[4]; dice[0] = new Die(6); dice[2] = new Die(5); for (Die curDie : dice) { if (curDie != null) { curDie.roll(); } } Die face: 6 dice null null

slide-13
SLIDE 13

Exercise #3

l Draw the memory diagram and determine output.

Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); }

slide-14
SLIDE 14

Exercise #3

single Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); }

slide-15
SLIDE 15

Exercise #3

dice Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } single

slide-16
SLIDE 16

Exercise #3

null Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } dice single null null null

slide-17
SLIDE 17

Exercise #3

Die face: 1 Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } null dice single null null null

slide-18
SLIDE 18

Exercise #3

i Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 1 null dice single null null null

slide-19
SLIDE 19

Exercise #3

i Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 1 dice single null null null

slide-20
SLIDE 20

Exercise #3

1 i Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 1 dice single null null null

slide-21
SLIDE 21

Exercise #3

1 i Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 1 dice single null null

slide-22
SLIDE 22

Exercise #3

2 i Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 1 dice single null null

slide-23
SLIDE 23

Exercise #3

2 i Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 1 dice single null

slide-24
SLIDE 24

Exercise #3

3 i Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 1 dice single null

slide-25
SLIDE 25

Exercise #3

3 i Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 1 dice single

slide-26
SLIDE 26

Exercise #3

Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 1 dice single

slide-27
SLIDE 27

Exercise #3

Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 3 dice single

slide-28
SLIDE 28

Exercise #3

Die single; Die[] dice; Dice = new Die[4]; single = new Die(1); for (int i = 0; i < dice.length; i++) { dice[i] = single; } dice[0].setFace(3); for (Die curDie : dice) { System.out.println(curDie.getFace()); } Die face: 3 dice single Output: 3 3 3 3

slide-29
SLIDE 29

Exercise #4

Complete the following method

/** * This method creates a Die array, and populates it with * Die objects. Each Die object will be initialized with * a random face value (using the zero argument constructor). * * @param numDice - The number of Die objects in the new array * @return The array of Die objects */ public static Die[] createDice(int numDice) { }

slide-30
SLIDE 30
  • Acknowledgements

Parts of this activity are based on materials developed by Chris Mayfield and Nathan Sprague. </end>