Top Banner
1 Why arrays are cool Why arrays are cool By mr. Hanley By mr. Hanley 4/25/2007 4/25/2007
13

Why arrays are cool

Jan 03, 2016

Download

Documents

aurelia-vaughn

Why arrays are cool. By mr. Hanley 4/25/2007. Contiguous memory structures. An array is a contiguous region of memory dedicated to storing a group of items Lots of times in computer programming, we need to keep track of a group of items. Here is an array of 9 integers called golfScores. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Why arrays are cool

11

Why arrays are coolWhy arrays are cool

By mr. HanleyBy mr. Hanley

4/25/20074/25/2007

Page 2: Why arrays are cool

22

Contiguous memory structuresContiguous memory structures

An array is a contiguous region of memory An array is a contiguous region of memory dedicated to storing a group of itemsdedicated to storing a group of items

Lots of times in computer programming, Lots of times in computer programming, we need to keep track of a group of itemswe need to keep track of a group of items

Page 3: Why arrays are cool

33

Here is an Here is an array of 9 array of 9 integers integers called called golfScoresgolfScores

golfScoresgolfScores

AddressAddress indexindex valuevalue

1200012000 00 44

1200412004 11 55

1200812008 22 44

1201212012 33 77

1201612016 44 55

1202012020 55 33

1202412024 66 55

1202812028 77 66

1203212032 88 22

Page 4: Why arrays are cool

44

But how do I create and use an But how do I create and use an array?array?

int[] golfScores = new int[9];int[] golfScores = new int[9];This command creates a new array of 9 This command creates a new array of 9

variablesvariablesgolfScores[0] = 5; //this sets the first golfScores[0] = 5; //this sets the first

element in the array to 5element in the array to 5

Page 5: Why arrays are cool

55

The array The array starts off with starts off with all values all values initialized to 0initialized to 0

golfScoresgolfScores

AddressAddress indexindex valuevalue

1000010000 00 00

1000410004 11 00

1000810008 22 00

1001210012 33 00

1001610016 44 00

1002010020 55 00

1002410024 66 00

1002810028 77 00

1003210032 88 00

Page 6: Why arrays are cool

66

Now, we will Now, we will overwrite the overwrite the first element first element with a 5.with a 5.

golfScoresgolfScores

AddressAddress indexindex valuevalue

1000010000 00 55

1000410004 11 00

1000810008 22 00

1001210012 33 00

1001610016 44 00

1002010020 55 00

1002410024 66 00

1002810028 77 00

1003210032 88 00

Page 7: Why arrays are cool

77

Can’t we use loops with arrays?Can’t we use loops with arrays?

Of courseOf courseLooping and arrays are a powerful Looping and arrays are a powerful

combinationcombinationBetter than Batman and Robin!Better than Batman and Robin! int i=0;int i=0;

while(i<9) {while(i<9) { golfScores[i] = -1; golfScores[i] = -1; i=i+1; i=i+1;}}

Page 8: Why arrays are cool

88

Now, we will Now, we will overwrite all overwrite all elements with elements with -1-1

golfScoresgolfScores

AddressAddress indexindex valuevalue

1000010000 00 -1-1

1000410004 11 -1-1

1000810008 22 -1-1

1001210012 33 -1-1

1001610016 44 -1-1

1002010020 55 -1-1

1002410024 66 -1-1

1002810028 77 -1-1

1003210032 88 -1-1

Page 9: Why arrays are cool

99

This is sick, can I create an array of This is sick, can I create an array of anything else?anything else?

SureSure Here is an array of 10 appointments:Here is an array of 10 appointments: String [ ] appts = new String[ 10 ];String [ ] appts = new String[ 10 ];

Page 10: Why arrays are cool

1010

apptsappts

AddressAddress indexindex valuevalue

71087108 00 “”“”

71127112 11 ””””

71167116 22 “”“”

71207120 33 “”“”

71247124 44 “”“”

71287128 55 “”“”

71327132 66 “”“”

71367136 77 “”“”

71407140 88 “”“”

71447144 99 “”“”

Page 11: Why arrays are cool

1111

How do we access any element?How do we access any element?

sc.println(“Which appointment to sc.println(“Which appointment to change?”);change?”);

int which = sc.input.readInt();int which = sc.input.readInt();appts[which] = sc.input.readLine();appts[which] = sc.input.readLine();

Page 12: Why arrays are cool

1212

apptsappts

AddressAddress indexindex valuevalue

71087108 00 “”“”

71127112 11 ””””

71167116 22 Java classJava class

71207120 33 “”“”

71247124 44 “”“”

71287128 55 “”“”

71327132 66 “”“”

71367136 77 “”“”

71407140 88 “”“”

71447144 99 “”“”

Page 13: Why arrays are cool

1313

But I want to print all the But I want to print all the appointments to the screenappointments to the screen

NPNP for(int i=0; i < appts.length; i++)for(int i=0; i < appts.length; i++)

{{ sc.println(i + “ “ + appts[i]); sc.println(i + “ “ + appts[i]);}}

That’s it, you’re kidding!That’s it, you’re kidding!No, I’m notNo, I’m notTruly Sick!!Truly Sick!!