1 / 20

Arrays

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:

kelton
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays Yong Choi School of Business CSU, Bakersfield

  2. 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.

  3. 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]?

  4. 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 ];

  5. 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)

  6. 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.

  7. Practice of Arrays I • Here is a declaration of another array: int[] scores = new double[25]; • Which of the following are legal? • scores[ 0 ] • scores[1] • scores[ -1 ] • scores[ 10] • scores[ 35 ] • scores[ 24 ]

  8. 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] ); } }

  9. Using a Variable as an Index • See chapter 46 for details • http://www.csub.edu/~ychoi2/MIS%20260/NotesJava/chap46/ch46_8.html

  10. 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.

  11. 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.

  12. 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 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

  13. 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

  14. 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 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 gradeTable[ 5 ][ 3 ] • Java knows what slot you mean and goes there directly.

  15. 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 ); } }

  16. 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?

  17. 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++ )

  18. Reading in Each Element import 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 ] ); } } }

  19. 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 ] ); } } }

  20. 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] ); } }

More Related