Arrays Yong Choi School of Business CSU Bakersfield
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: – 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.
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]?
Arrays are Objects • Array declarations look like this: type [ ] array. Name; • An array is an object: new type [ length] • Often an array is declared and constructed in one statetment like this: type []array. Name = new type [ length ];
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[ data[ -1 ] always illegal 10 ] illegal (given the above declaration) 1. 5 ] always illegal 0 ] always OK 9 ] OK (given the above declaration)
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.
Practice of Arrays I • Here is a declaration of another array: int[] scores = new double[25]; • Which of the following are legal? 1) 2) 3) 4) 5) 6) scores[ 0 ] scores[1] scores[ -1 ] scores[ 10] scores[ 35 ] scores[ 24 ]
First Array Program • Default value of each slot is zero. public class Array. Eg 1 { 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] ); ); ); } }
Using a Variable as an Index • See chapter 46 for details • http: //www. csub. edu/~ychoi 2/MIS%20260/Notes. Java/cha p 46/ch 46_8. html
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.
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.
Two-Dimensional Arrays I Student Week • The instructor records the grades in a table. A particular cell of the table is designated by student number and week number. 0 1 2 3 4 0 99 42 74 83 100 1 90 91 72 88 95 2 88 61 74 89 96 – The grade for student 3 week 4 is 93 3 61 89 82 98 93 – The grade for student 6 week 2 is 78 4 93 73 75 78 99 5 50 65 92 87 94 6 43 98 78 56 99 • For example: – The grade for student 0 week 1 is 42
Two-Dimensional Arrays II • A compact notation uses the row and column number like this: grade. Table[ row ][ col ] • For example: – grade. Table[ 0 ][ 1 ] is 42 – grade. Table[ 3 ][ 4 ] is 93 – grade. Table[ 6 ][ 2 ] is 78
Headings are not part of the Array Actual grade table in computer memory • The row and column numbers are not part of the array. When you ask for grade. Table[ 5 ][ 3 ] 99 42 74 83 100 90 91 72 88 95 88 61 74 89 96 61 89 82 98 93 • Java knows what slot you mean and goes there directly. 93 73 75 78 99 50 65 92 87 94 43 98 78 56 99
Example Program of 2 D Arrays • Try this program! public class Grade. Example { public static void main( String[] arg ) { // declare and construct a 2 D array int[][] grade. Table = { {99, 42, 74, 83, 100}, {90, 91, 72, 88, 95}, {88, 61, 74, 89, 96}, {61, 89, 82, 98, 93}, {93, 75, 78, 99}, {50, 65, 92, 87, 94}, {43, 98, 78, 56, 99} }; System. out. println("grade 0, 0: " + grade. Table[0][0]); System. out. println("grade 2, 4: " + grade. Table[2][4]); grade. Table[5][3] = 99 ; int sum = grade. Table[0][1] + grade. Table[0][2] ; System. out. println( "sum: " + sum ); } }
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 count. Array { public static void main ( String[] args ) { int[] eg. Array = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }; for ( _______________ ) { System. out. println(_________ ); }}} • • Complete this program Is there any problem with this program?
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 < eg. Array. length; index++ )
Reading in Each Element import java. io. * ; public class input. Array { public static void main ( String[] args ) throws IOException { int[] array = new int[5]; int data; Buffered. Reader in. Data = new Buffered. Reader ( new Input. Stream. Reader( System. in ) ); // input the data for ( ___________ ; _________ ) { System. out. println( "enter an integer: " ); data = Integer. parse. Int( in. Data. read. Line() ); array[ index ] = data ; } // write out the data for ( ___________ ; _________ ) { System. out. println( "array[ " + index + " ] = " + array[ index ] ); } } }
• In below program, the user picks the array size when the program runs. } import java. io. * ; public class input. Array { public static void main ( String[] args ) throws IOException { Buffered. Reader in. Data = new Buffered. Reader ( new Input. Stream. Reader( System. in ) ); int[] array; // determine the array size and construct the array System. out. println( "What length is the array? " ); int size = Integer. parse. Int( in. Data. read. Line() ); array = new int[ _______ ]; for ( int index=0; index < array. length; index++) { System. out. println( "enter an integer: " ); data = Integer. parse. Int( in. Data. read. Line() ); array[ index ] = data ; } for ( int index=0; index < array. length; index++ ) { System. out. println( "array[ " + index + " ] = " + array[ index ] ); } }
Arrays of Strings • You can create an array of Strings – For example: String[] dept. Name = {“Accounting”, “HR”, “Sales”, }; public class String. Array { public static void main ( String[] args ) { String[] str. Array = new String[4] ; str. Array[0] = "Hello" ; str. Array[1] = "World" ; str. Array[2] = "Greetings" ; str. Array[3] = "Jupiter" ; for (int j=0; j < str. Array. length; j++ ) System. out. println( "Slot " + j + ": " + str. Array[j] ); }}
- Slides: 20