Why arrays are cool 4252007 1 Contiguous memory

  • Slides: 13
Download presentation
Why arrays are cool 4/25/2007 1

Why arrays are cool 4/25/2007 1

Contiguous memory structures An array is a contiguous region of memory dedicated to storing

Contiguous memory structures An array is a contiguous region of memory dedicated to storing a group of items l Lots of times in computer programming, we need to keep track of a group of items l 2

l Here is an array of 9 integers called golf. Scores Address 12000 12004

l Here is an array of 9 integers called golf. Scores Address 12000 12004 12008 12012 12016 12020 12024 12028 12032 index 0 1 2 3 4 5 6 7 8 value 4 5 4 7 5 3 5 6 2 3

But how do I create and use an array? int[] golf. Scores = new

But how do I create and use an array? int[] golf. Scores = new int[9]; l This command creates a new array of 9 variables l golf. Scores[0] = 5; //this sets the first element in the array to 5 l 4

l The array starts off with all values initialized to 0 golf. Scores Address

l The array starts off with all values initialized to 0 golf. Scores Address 10000 10004 10008 10012 10016 10020 10024 10028 10032 index 0 1 2 3 4 5 6 7 8 value 0 0 0 0 0 5

l Now, we will overwrite the first element with a 5. golf. Scores Address

l Now, we will overwrite the first element with a 5. golf. Scores Address 10000 10004 10008 10012 10016 10020 10024 10028 10032 index 0 1 2 3 4 5 6 7 8 value 5 0 0 0 0 6

Can’t we use loops with arrays? Of course l Looping and arrays are a

Can’t we use loops with arrays? Of course l Looping and arrays are a powerful combination l Better than Batman and Robin! l int i=0; while(i<9) { golf. Scores[i] = -1; i=i+1; } l 7

l Now, we will overwrite all elements with -1 golf. Scores Address 10000 10004

l Now, we will overwrite all elements with -1 golf. Scores Address 10000 10004 10008 10012 10016 10020 10024 10028 10032 index 0 1 2 3 4 5 6 7 8 value -1 -1 -1 8

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

This is sick, can I create an array of anything else? l l l Sure Here is an array of 10 appointments: String [ ] appts = new String[ 10 ]; 9

appts Address index value 7108 7112 7116 7120 7124 7128 7132 7136 7140 7144

appts Address index value 7108 7112 7116 7120 7124 7128 7132 7136 7140 7144 0 1 2 3 4 5 6 7 8 9 “” ”” “” “” 10

How do we access any element? System. out. println(“Which appointment to change? ”); l

How do we access any element? System. out. println(“Which appointment to change? ”); l int which = input. next. Int(); l appts[which] = input. next. Line(); l 11

appts Address index value 7108 7112 7116 7120 7124 7128 7132 7136 7140 7144

appts Address index value 7108 7112 7116 7120 7124 7128 7132 7136 7140 7144 0 1 2 3 4 5 6 7 8 9 “” ”” “” “” Java class “” “” “” 12

But I want to print all the appointments to the screen NP l for(int

But I want to print all the appointments to the screen NP l for(int i=0; i < appts. length; i++) { System. out. println(i + “ “ + appts[i]); } l That’s it, you’re kidding! l No, I’m not l Truly Sick!! l 13