Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 14. 2 Today’s topic Arrays Reading for this Lecture: –Chaper 11.

Similar presentations


Presentation on theme: "1 Lecture 14. 2 Today’s topic Arrays Reading for this Lecture: –Chaper 11."— Presentation transcript:

1 1 Lecture 14

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

3 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 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 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 statements for all variables (i.e. all student IDs) …

6 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 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 are 0 ~ 4 (not 1 ~ 5) nums 0 1 2 3 4 e.g. nums[0]nums[3]

8 8 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}; 246810 0 1 2 3 4 nums For example, nums[0] is 2 nums[1] is 4 index

9 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; 13579 0 1 2 3 4 nums index

10 10 Arrays and Loops Array will be efficiently used in 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 ….. }

11 11 Arrays and Loops 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]); } int [ ] stdID = new int [5]; stdID[0] = 32; stdID[1] = 42; stdID[2] = 90; stdID[3] = 25; stdID[4] = 10;

12 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 13 Exercise!!!

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


Download ppt "1 Lecture 14. 2 Today’s topic Arrays Reading for this Lecture: –Chaper 11."

Similar presentations


Ads by Google