arrays

20
Arrays Yong Choi School of Business CSU, Bakersfield

Upload: kelton

Post on 07-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Arrays. Yong Choi School of Business CSU, Bakersfield. Why Arrays?. Most real-world programs handle vast amounts of data. When data is organized into arrays and processed with loops a (relatively) small program can handle any amount of data. Important Facts: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Arrays

Arrays

Yong ChoiSchool of BusinessCSU, Bakersfield

Page 2: Arrays

Why Arrays?

• Most real-world programs handle vast amounts of data.

• When data is organized into arrays and processed with loops a (relatively) small program can handle any amount of data.

• Important Facts: – Indexes always start at zero, and count up by one's

until the last slot of the array. – If there are N slots in an array, the indexes will be 0

through N-1.

Page 3: Arrays

Picture of an Array

• The name of this array is data.• The slots are indexed 0

through 9. • Each slot can be accessed by

using its index number– data[0] is the slot which is

indexed by zero (which contains the value 23).

– data[5] is the slot which is indexed by 5 (which contains the value 14).

– What about data[7]?

Page 4: Arrays

Arrays are Objects

• Array declarations look like this: type [ ] arrayName;

• An array is an object: new type [ length]

• Often an array is declared and constructed in one statetment like this:

type []arrayName = new type [ length ];

Page 5: Arrays

Bounds Checking I

• Assume that an array were declared: int[] data = new int[10];

• It is not legal to refer to a slot that does not exist: – data[ -1 ] always illegal– data[ 10 ] illegal   (given the above declaration)– data[ 1.5 ] always illegal– data[ 0 ] always OK– data[ 9 ] OK   (given the above declaration)

Page 6: Arrays

Bounds Checking II

• If you have one of the illegal expressions in your program, your program will not compile.

• since the array is constructed as the program is running, its length does not need to be known to the compiler.

• If your running program tries to refer to a slot that does not exist, an exception will be thrown, and your program will be terminated.

Page 7: Arrays

Practice of Arrays I

• Here is a declaration of another array: int[] scores = new double[25];

• Which of the following are legal? 1) scores[ 0 ]2) scores[1]3) scores[ -1 ]4) scores[ 10]5) scores[ 35 ]6) scores[ 24 ]

Page 8: Arrays

First Array Program

• Default value of each slot is zero. public class ArrayEg1 { public static void main ( String[] args ) {

int[] stuff = new int[5]; stuff[0] = 23; stuff[1] = 38; stuff[2] = 7*2; System.out.println("stuff[0] has " + stuff[0] ); System.out.println("stuff[1] has " + stuff[1] ); System.out.println("stuff[2] has " + stuff[2] ); System.out.println("stuff[3] has " + stuff[3] ); System.out.println("stuff[4] has " + stuff[4] ); } }

Page 9: Arrays

Using a Variable as an Index

• See chapter 46 for details

• http://www.csub.edu/~ychoi2/MIS%20260/NotesJava/chap46/ch46_8.html

Page 10: Arrays

Initializer Lists

• You can declare, construct, and initialize the array all in one statement:

int[] data = {23, 38, 14, -3, 0, 14, 9, 103, 0, -56 };

• An int array of 10 slots (indexed 0..9). • The first value in the initializer list

corresponds to index 0, the second value coresponds to index 1, and so on.

Page 11: Arrays

Type of Arrays

• One dimensional array. – The slots of an array are often called elements. – The elements are accessed using a single index.

• Two dimensional array– The elements are accessed using two indexes.

• Three dimensional arrays, and higher dimensional arrays also exist.

Page 12: Arrays

Two-Dimensional Arrays I

• The instructor records the grades in a table. A particular cell of the table is designated by student number and week number.

• For example:

– The grade for student 0 week 1 is 42

– The grade for student 3 week 4 is 93

– The grade for student 6 week 2 is 78

Student Week

  0 1 2 3 4

0 99 42 74 83 100

1 90 91 72 88 95

2 88 61 74 89 96

3 61 89 82 98 93

4 93 73 75 78 99

5 50 65 92 87 94

6 43 98 78 56 99

Page 13: Arrays

Two-Dimensional Arrays II

• A compact notation uses the row and column number like this:

gradeTable[ row ][ col ]

• For example: – gradeTable[ 0 ][ 1 ] is 42

– gradeTable[ 3 ][ 4 ] is 93

– gradeTable[ 6 ][ 2 ] is 78

Page 14: Arrays

Headings are not part of the Array

• The row and column numbers are not part of the array. When you ask for

gradeTable[ 5 ][ 3 ]

• Java knows what slot you mean and goes there directly.

99 42 74 83 100

90 91 72 88 95

88 61 74 89 96

61 89 82 98 93

93 73 75 78 99

50 65 92 87 94

43 98 78 56 99

Actual grade table in computer memory

Page 15: Arrays

Example Program of 2D Arrays• Try this program!public class GradeExample {

public static void main( String[] arg ) { // declare and construct a 2D array int[][] gradeTable =

{ {99, 42, 74, 83, 100}, {90, 91, 72, 88, 95},

{88, 61, 74, 89, 96}, {61, 89, 82, 98, 93},

{93, 73, 75, 78, 99}, {50, 65, 92, 87, 94}, {43, 98, 78, 56, 99} };

System.out.println("grade 0,0: " + gradeTable[0][0]); System.out.println("grade 2,4: " + gradeTable[2][4]); gradeTable[5][3] = 99 ; int sum = gradeTable[0][1] + gradeTable[0][2] ; System.out.println( "sum: " + sum ); } }

Page 16: Arrays

Counting Loops and Arrays

• The index of an array starts at 0 and counts up to one less than the number of elements in the array public class countArray { public static void main ( String[] args ) { int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }; for ( ______________________________ ) { System.out.println(__________________ ); } } }

• Complete this program• Is there any problem with this program?

Page 17: Arrays

The length of an Array

• The length of the array depends on data. – Usually, you need to write programs that can deal with

arrays whose sizes are not known until the program is running.

– Do not need to worry about the size of Arrays by coding like below…

for ( int index= 0 ; index < egArray.length; index++ )

Page 18: Arrays

Reading in Each Elementimport java.io.* ; public class inputArray { public static void main ( String[] args ) throws IOException { int[] array = new int[5]; int data; BufferedReader inData = new BufferedReader ( new InputStreamReader( System.in ) ); // input the data for ( ___________ ; ________________ ; _____________ ) { System.out.println( "enter an integer: " ); data = Integer.parseInt( inData.readLine() ); array[ index ] = data ; } // write out the data for ( ___________ ; ________________ ; _____________ ) { System.out.println( "array[ " + index + " ] = " + array[ index ] ); } } }

Page 19: Arrays

• In below program, the user picks the array size when the program runs. import java.io.* ; public class inputArray { public static void main ( String[] args ) throws IOException { BufferedReader inData = new BufferedReader ( new

InputStreamReader( System.in ) ); int[] array; // determine the array size and construct the array System.out.println( "What length is the array?" ); int size = Integer.parseInt( inData.readLine() ); array = new int[ _____________ ]; for ( int index=0; index < array.length; index++) { System.out.println( "enter an integer: " ); data = Integer.parseInt( inData.readLine() ); array[ index ] = data ; } for ( int index=0; index < array.length; index++ ) { System.out.println( "array[ " + index + " ] = " + array[ index ] ); } } }

Page 20: Arrays

Arrays of Strings

• You can create an array of Strings– For example:

String[] deptName = {“Accounting”, “HR”, “Sales”,};

public class StringArray { public static void main ( String[] args ) {

String[] strArray = new String[4] ; strArray[0] = "Hello" ; strArray[1] = "World" ; strArray[2] = "Greetings" ; strArray[3] = "Jupiter" ;

for (int j=0; j < strArray.length; j++ ) System.out.println( "Slot " + j + ": " +

strArray[j] );} }