• No results found

A Java array is kind of like a credit card holder Sleeves to put credit cards in

N/A
N/A
Protected

Academic year: 2021

Share "A Java array is kind of like a credit card holder Sleeves to put credit cards in"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)

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

(2)

Sleeve #0 Sleeve #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 Sleeves

Thursday, February 16, 2012 12:02 PM

(3)

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 Apart Thursday, February 16, 2012 12:04 PM

(4)

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 Input

Friday, February 17, 2012 9:48 AM

(5)

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 the ONE 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 Input Friday, February 17, 2012 9:52 AM

(6)

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 Notation Friday, February 17, 2012 9:58 AM

(7)

0

5

1

-17

2

6

3

5

2

4 ONE array 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 Array

Friday, February 17, 2012 10:16 AM

(8)

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 Output Friday, February 17, 2012 10:24 AM

(9)

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 Example

Friday, February 17, 2012 10:35 AM

(10)

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 item for ( int item : mydata ) {

}

This command block is exploded on the next page

The colon : means "in" Array Loops

Friday, February 17, 2012 10:42 AM

(11)

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-in

Friday, February 17, 2012 10:45 AM

(12)

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)xwill hold one integer at a time

(3) The array name is mystuff

(4) Each "sleeve" of mystuffwill hold just one integer

(5) Each xwill be printed (one at a time) by the command:

out.println(x);

For-in Example

Friday, February 17, 2012 10:53 AM

(13)

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 real

number 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 2 Friday, February 17, 2012 10:58 AM

(14)

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, VALUE Except 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 Array Friday, February 17, 2012 11:11 AM

(15)

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 xxinside 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 Array Friday, February 17, 2012 11:07 AM

(16)

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 Parts

Friday, February 17, 2012 11:20 AM

(17)

This program code reads a series of integer arraysuntil 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 Loop Friday, February 17, 2012

11:25 AM

(18)

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 Loop

Friday, February 17, 2012 11:30 AM

(19)

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 Input

Friday, February 17, 2012 11:30 AM

(20)

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 Break

Friday, February 17, 2012 11:30 AM

(21)

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 Element

Friday, February 17, 2012 11:30 AM

(22)

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 Element

Friday, February 17, 2012 11:30 AM

(23)

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 Product

Friday, February 17, 2012 11:30 AM

(24)

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 Array

Friday, February 17, 2012 11:30 AM

(25)

Enter two nos in []: [4,3]

product=12

Enter two nos in[]: [12,7]

product=84

Enter two nos in []: [7,3,5,9]

product=21

Enter two nos in []: [20,11]

product=220

Enter 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 Program

Friday, February 17, 2012 11:39 AM

References

Related documents

The study is helpful for many players in Home Appliances to make following strategies related to advertising for any particular company: -..  Advertising

One of the tools of performance measurement that has been used in the past is performance appraisal that has been reintroduced in a new format and design and implementation within the

While there is ample theoretical and empirical research on firm and industry determinants of internal R&D, the literature deals less with the choice between

• AutoGOJA™ online job analysis system • TVAP™ test validation & analysis program • CritiCall™ pre-employment testing for 911 operators • OPAC™ pre-employment testing

These specific tracts were reconstructed with the hypothesis that changes in white matter microstructure may be observed in the PCOS group as it is a heterogeneous disorder

scheme, there are several types of entities: a cloud provider, cloud users, a blacklisting controller and verifiers. The cloud provider and blacklisting controller could be

The aim of this study was to evaluate the current vac- cination status of the HCWs in all of the Departments different from the Department for the Health of Women and Children of one

Trauma  Rehabilita>on  Database   — Microsoft  Access  designed  by  Consultant  in  RM   — Simple  data  input  for  administrative  staff   — Daily