140 likes | 269 Views
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
E N D
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 • when a set of values will be processed in a loop
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
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) …
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
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]
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
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
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
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]); }
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]; }
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