1 / 14

Lecture 14

Lecture 14. Today’s topic. Arrays Reading for this Lecture: Chaper 11. Introduction to Arrays. What is Arrays Arrays will contain a list of values of same type It is very useful to have an array when a program manages a large number of information

zoey
Download Presentation

Lecture 14

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. Lecture 14

  2. Today’s topic • Arrays • Reading for this Lecture: • Chaper 11

  3. Introduction to Arrays • What is Arrays • Arrays will contain a list of values of same type • It is very useful to have an array • when a program manages a large number of information • when a set of values will be processed in a loop

  4. Introduction to Arrays • For example, when a program manages information of 100 students (e.g. student ID) in a loop • For each loop, we want to access the data for each student • But we don’t want to declare them as individual variables, • e.g. 100 integer variables like: int stdID1,stdID2,stdID3,stdID4,stdID5,…; inefficient

  5. Introduction to Arrays • Without arrays we would need to do something like this (NOTE: Don’t do it this way!): int stdID0, stdID1, stdID2, stdID3, stdID4, … ; for (int i = 0; i < 100; i++) { if(i == 0) { System.out.println( stdId0 ); } else if(i == 1) { System.out.println( stdId1 ); } else if(i == 2) { System.out.println( stdId2 ); } } We have to repeatedly write same statementsfor all variables (i.e. all student IDs) …

  6. Introduction to Arrays • In Java, an array is an object • We can declare an array as follows: e.g. int [] nums = new int [5]; • Specify data type for an variable values • [ ] indicates that following variable is an array • Name of array variable • Use new operator to create an object (instantiation) • Specify how many values will be contained in this array

  7. Introduction to Arrays int [] nums = new int [5]; • This statement declares an array named nums, which contains 5 values of type int • A single integer value can be selected using an integer (usually called index) inside the [ ] • In this example, the valid array index values are0 ~ 4 (not 1 ~ 5) nums 0 1 2 3 4 e.g. nums[0] nums[3]

  8. 2 4 6 8 10 Initialization of Arrays (1) • An array should be initialized so that each element contains a specific value: // array declaration with initialization int [] nums = {2, 4, 6, 8, 10}; nums 0 1 2 3 4 index For example, nums[0] is 2 nums[1] is 4

  9. 1 3 5 7 9 Initialization of Arrays (2) • An array should be initialized so that each element contains a specific value: // array declaration without initialization int [] nums = new int [5]; nums[0] = 1; nums[1] = 3; nums[2] = 5; nums[3] = 7; nums[4] = 9; nums 0 1 2 3 4 index

  10. Arrays and Loops • // Example in the previous slide • int stdID0, stdID1, stdID2, stdID3, stdID4; • for (int i = 0; i < 5; i++) • { • if( i == 0 ) { • System.out.println(“Student0 ID is ” + stdID0); • } • else if( i == 1 ) { • System.out.println(“Student1 ID is ” + stdID1); • } • else if( i == 2 ) { • System.out.println(“Student2 ID is ” + stdID2); • } • // two more students here • ….. • } • Array will be efficiently used in loops

  11. Arrays and Loops int [ ] stdID = new int [5]; stdID[0] = 32; stdID[1] = 42; stdID[2] = 90; stdID[3] = 25; stdID[4] = 10; • Now, we can coordinate the processing of one value with the execution of one pass through a loop using an index variable int MAX = 5; int [ ] stdID = {32, 42, 90, 25, 10}; for (int i = 0; i < MAX; i++) { // use i as array index variable System.out.println(“Student” + i + “ ID is ” + stdID[i]); }

  12. Alternative Loop Control Condition • Arrays are objects • Each array has an attribute “length” • we can get a value equal to the length of that array e.g. nums.length is equal to MAX: int MAX = 5; // symbolic constant int [ ] nums = new int [MAX]; for (i = 0; i < nums.length; i++) { // use i as array index variable in Java statements using nums[i]; }

  13. Exercise!!!

  14. Declare an array of integer • Store (assign) seven multiply of 3 in the array • Print out them 3, 6, 9, 12, 15, 18, 21  array • Print out the array elements in reverse order

More Related