A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro...

25
A Java array is kind of like a credit card holder Sleeves to put credit cards in Outer case of holder Intro Arrays Thursday, February 16, 2012 11:59 AM Intro Arrays Page 1

Transcript of A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro...

Page 1: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

A Java array is kind of like a credit card holder

Sleeves to put credit cards in

Outer case of holder

Intro ArraysThursday, February 16, 2012

11:59 AM

Intro Arrays Page 1

Page 2: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

Sleeve #0Sleeve #1

Sleeve #2

However, the "sleeves" of a Java array are numbered in permanent ink on the outside corners.

The numbering goes 0 , 1, 2 , 3 , …INSTEAD of 1, 2, 3, 4, ...

Numbered SleevesThursday, February 16, 2012

12:02 PM

Intro Arrays Page 2

Page 3: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

1

2

0

23

-8

369

Sleeve #0

Sleeve #1

Sleeve #2

Data item IN sleeve #0

Data item IN sleeve #1

Data item IN sleeve #2

Sleeve numbers are called SUBSCRIPTS

Sleeves are called subscripted ELEMENTS

Numbers IN sleeves are called subscripted VALUES

Wallet Torn ApartThursday, February 16, 2012

12:04 PM

Intro Arrays Page 3

Page 4: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

Using the "Easy" library, an array can be input from the user's console as ONE THING.

Here is an input command with the usual four parts:

int[] mystuff = cin.nextIntArray() ;

MAKE NAME VALUE

PUT

Easy Array InputFriday, February 17, 2012

9:48 AM

Intro Arrays Page 4

Page 5: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

int[] mystuff = cin.nextIntArray() ;

MAKE NAME VALUE

PUT

Make a variable inside of which ONE integer array can be kept -remember that an array is like a wallet with a number in each sleeve.

The name for theONE THING which is an array of integers-mystuff is the ONE name of the WHOLE wallet.

The "Easy"operation to read a SINGLE array of integers from the user's console -The input must be in brackets notation.(see next page)

As usual PUT is done last.The VALUE on the right is to stored into the variable on the left.

NOTES: Array InputFriday, February 17, 2012

9:52 AM

Intro Arrays Page 5

Page 6: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

Brackets Notation is used by the Easy library to allow the user to print and input an ENTIRE array to and from the console.

Here is an example of brackets notation:

[ 5 , -17 , 6 , 5 , 2 ]

Represents outer "case" of the "wallet"

Commas represent the spaces between the "sleeves" in the "wallet"

Integer in sleeve #0

Integer in sleeve #1

Integer in sleeve #2

Integer in sleeve #3

Integer in sleeve #4

Brackets NotationFriday, February 17, 20129:58 AM

Intro Arrays Page 6

Page 7: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

0

5

1

-17

2

63

5

24

ONEarray

Five "sleeves"Hold five integers

TWO NOTES:

Order of numbers -first to last is IMPORTANT

(1)

Numbers CAN be repeated.

(2)

Sleeve subscripts

REMEMBER:"sleeve" = array element"sleeve index" = subscript

Exploded View Previous ArrayFriday, February 17, 2012

10:16 AM

Intro Arrays Page 7

Page 8: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

out.println( "an array=" + content( mystuff ) ) ;

Using the "Easy" library, an array can be printed onto the user's console as ONE THING.

Here is an output command with the usual four parts:

The usual print to the console command

A legend of some kind

Name of the variable wherein the array has been PUT.-See page named "Easy Array Input"

Special "Easy" operation to put array into brackets notation

Easy Array OutputFriday, February 17, 201210:24 AM

Intro Arrays Page 8

Page 9: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

The simple program below (just the main procedure) prompts the user to enter a single array of integers (in bracket notation) and then copies that same array back to the console (again in bracket notation).

out.print("Enter brackets array: ");int[] listx = cin.nextIntArray();out.println("You entered this array = " + content(listx) );

AGAIN NOTE: The ENTIRE array is input as ONE THING and is printed out as ONE THING.

Below is a snapshot of the user's console for running the above program

Enter brackets array: [ 3 , 2, -5,6,2,1]You entered this array = [3, 2, -5, 6, 2, 1]

Note user input is in blue.

Array IO ExampleFriday, February 17, 201210:35 AM

Intro Arrays Page 9

Page 10: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

It can be necessary to process the content of each individual "sleeve" of an array one at a time.

One way to do this is to use a for-in loop:

// process the ONE sleeve content named itemfor ( int item : mydata ) {

}

This command block is exploded on the next page

The colon : means "in"

Array LoopsFriday, February 17, 2012

10:42 AM

Intro Arrays Page 10

Page 11: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

for ( int item : mydata ) {

// java steps that will// process just ONE sleeve content named item

}

The array of ALL those "sleeves" that are going to be processed - one at a time.

A loop variable name to hold just ONE sleeve value at a time

Type of each item to be found INSIDE each sleeve of the array

Keywords and punctuation for a for-in command

for-in command

Exploded for-inFriday, February 17, 2012

10:45 AM

Intro Arrays Page 11

Page 12: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

Suppose we have an integer array named mystuff and want to print out each "sleeve" element (to be named x) one at a time - each on a line by itself - namely all the elements will appear in one column.Here is one way to do that:

out.println(x);for( int x : mystuff ) {

}

NOTES:(1) The loop variable name is x(2) x will hold one integer at a time(3) The array name is mystuff(4) Each "sleeve" of mystuff will hold just one integer(5) Each x will be printed (one at a time) by the command: out.println(x);

For-in ExampleFriday, February 17, 2012

10:53 AM

Intro Arrays Page 12

Page 13: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

The processing put inside a for-in loop can be any of the models you have already learned - or any models you learn in the future - or any models you invent yourself.

This java code adds up and prints out the sum of the realnumber elements in the sleeves of the the array named grades - which is read in from the user.

out.print("Enter grades in [] :");double[] grades = cin.nextDoubleArray();double sum = 0;

sum = sum + g;for(double g : grades) {

}out.println("Total was "+sum);

// prompt

// input WHOLE array

// start total at 0

// for-in loop: g is each grade in array - one at a time

// add grade g to total - one at a time

// marks end of for-in loop

// tell user the total of all the grades

For-in Example 2Friday, February 17, 2012

10:58 AM

Intro Arrays Page 13

Page 14: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

One way to get an array is to input one from the user by a command like:

int[] mylist = cin.nextIntArray();

Another way is to use an array initializer in place of the user input operation:

int[] mylist = { 5 , 6 , 1 , 3 };

This has the usual parts: MAKE, NAME, PUT, VALUEExcept that the VALUE is a list of the array element values in braces.YES - sometimes Java uses braces for arrays and sometimes it uses brackets - SORRY! ! !So far: brackets are for input/output and braces are for writing java program commands.

Initializing an ArrayFriday, February 17, 2012

11:11 AM

Intro Arrays Page 14

Page 15: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

Given an array - you can refer to parts INSIDE the array.

.length

First, you can refer to the number of "sleeves" in the array by using the notation

[0][1][2][3][4]...

Then you can refer to each number INSIDE a sleeve of the array using the notations:

The number xx inside the brackets [ xx ] is called a subscript - it is just the index number permanently written on the outside of each sleeve - see the page named "Wallet Torn Apart".

All of these notations FOLLOW the name of the array

Parts of an ArrayFriday, February 17, 201211:07 AM

Intro Arrays Page 15

Page 16: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

For example, if we had an array named theStuff and wanted to print out the product of the first number and second number INSIDE that array, we could write:

out.println("product=" + theStuff[0] * theStuff[1] );

Refers to the number in sleeve #0 of the wallet (i.e. array) named theStuff

Refers to the number in sleeve #1 of the wallet (i.e. array) named theStuff

Recall that the sleeves are numbered starting from 0 rather than 1.

Example of PartsFriday, February 17, 2012

11:20 AM

Intro Arrays Page 16

Page 17: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

This program code reads a series of integer arrays until the user enters an empty array. For each such array (except the empty one) it prints out the product of the first two numbers in that array. It is assumed that the user will not try to mess-up the program by entering an array with only one element.

out.print("Enter two nos in []: ");int[] thedata = cin.nextIntArray();if(thedata.length==0) break;int product = thedata[0] * thedata[1] ;out.println("product="+product);

while(true) {

}

Detailed explanation appears in the following pages

Array IO INSIDE LoopFriday, February 17, 2012

11:25 AM

Intro Arrays Page 17

Page 18: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

out.print("Enter two nos in []: ");int[] thedata = cin.nextIntArray();if(thedata.length==0) break;int product = thedata[0] * thedata[1] ;out.println("product="+product);

while(true) {

}

Sentinel value "infinite" loop

Sentinel Value LoopFriday, February 17, 2012

11:30 AM

Intro Arrays Page 18

Page 19: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

out.print("Enter two nos in []: ");int[] thedata = cin.nextIntArray();if(thedata.length==0) break;int product = thedata[0] * thedata[1] ;out.println("product="+product);

while(true) {

}

Sentinel value Prompt and Input

NOTE: input gets A WHOLE ARRAY(but which usually has only two elements)

Sentinel Value Prompt and InputFriday, February 17, 201211:30 AM

Intro Arrays Page 19

Page 20: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

out.print("Enter two nos in []: ");int[] thedata = cin.nextIntArray();if( thedata.length == 0 ) break;int product = thedata[0] * thedata[1] ;out.println("product="+product);

while(true) {

}

Sentinel value break - note use of .length notation to see how many sleeves are in array. Empty array will have a length of 0.

Breaks out of loop when user enters an array with NO elements in it - i.e. user types:

[ ]

Sentinel Value BreakFriday, February 17, 201211:30 AM

Intro Arrays Page 20

Page 21: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

out.print("Enter two nos in []: ");int[] thedata = cin.nextIntArray();if(thedata.length==0) break;int product = thedata[0] * thedata[1] ;out.println("product="+product);

while(true) {

}

First array element is in sleeve number 0.

First Array ElementFriday, February 17, 201211:30 AM

Intro Arrays Page 21

Page 22: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

out.print("Enter two nos in []: ");int[] thedata = cin.nextIntArray();if(thedata.length==0) break;int product = thedata[0] * thedata[1] ;out.println("product="+product);

while(true) {

}

Second array element is in sleeve number 1.

Second Array ElementFriday, February 17, 201211:30 AM

Intro Arrays Page 22

Page 23: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

out.print("Enter two nos in []: ");int[] thedata = cin.nextIntArray();if(thedata.length==0) break;int product = thedata[0] * thedata[1] ;out.println("product="+product);

while(true) {

}

Product of first and second elements of array most recently entered by user.

Compute ProductFriday, February 17, 201211:30 AM

Intro Arrays Page 23

Page 24: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

out.print("Enter two nos in []: ");int[] thedata = cin.nextIntArray();if(thedata.length==0) break;int product = thedata[0] * thedata[1] ;out.println("product="+product);

while(true) {

}

Show user the product for most recently entered array.

Thus there will be an output line in the console for each one of the input arrays that the user types - except for the last empty input array typed as [ ]

Answer for Current ArrayFriday, February 17, 201211:30 AM

Intro Arrays Page 24

Page 25: A Java array is kind of like a credit card holder Sleeves ...core.ecu.edu/csci/wirthj/Lessons/Intro Arrays.pdf · A Java array is kind of like a credit card holder Sleeves to put

Enter two nos in []: [4,3]product=12Enter two nos in[]: [12,7]product=84Enter two nos in []: [7,3,5,9]product=21Enter two nos in []: [20,11]product=220Enter two nos in []: []

User input is in blue

Program ends after last input - namely the []

User CAN enter MORE than two numbers if they want.

Sample Console for Previous ProgramFriday, February 17, 2012

11:39 AM

Intro Arrays Page 25