Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2011 Introduction to Programming I Arrays (I)

Similar presentations


Presentation on theme: "CS2011 Introduction to Programming I Arrays (I)"— Presentation transcript:

1 CS2011 Introduction to Programming I Arrays (I)
Chengyu Sun California State University, Los Angeles

2 Example: Grades Find the average of the grades, and display the ones that are above average 3 students: grade1, grade2, grade3 10 students: grade1, grade2, …, grade10 100 students??

3 Create An Array Declare an array variable Allocate an empty array
Populate the array

4 Declare An Array Variable
Type of the elements in the array Indicate that the variable is an array Name of the variable int[] grades; or int grades[]; All elements in an array must be of the same type

5 Allocate An Empty Array
Create a new array of given size Size of the array grades = new int[30]; Type of the elements in the array (must match the type in array declaration) The size of an array is fixed and cannot be changed At this point the array is empty

6 Populate An Array Array index grades[0] = 85; grades[1] = 100;
Array index starts from 0 and goes up to size – 1 IndexOutOfBound error array[index] is just like a variable Assign it a value Access its value

7 What Happens in Each Step
Memory Memory Memory grades grades grades 85 100 Declare a variable Allocate space for the array Point the variable to the array Put values into the array

8 Array Variable vs. The Actual Array
An array variable only holds a reference to array, e.g. grades is a variable that contains a reference to an array of 30 int elements

9 Array Initializer No need to specify array size
A list of values int[] numbers = { 10, 20, 25 }; String[] words = { "hi", "you" }; No need to specify array size Must be in the same statement where the array is declared

10 Iterate Through An Array …
for( int i=0 ; i < grades.length ; ++i ) System.out.println( grades[i] ); The size of the array is stored in a variable in the array called length for loop is a natural fit for arrays as array size is fixed and known

11 … Iterate Through An Array
A variation of the for loop is call foreach loop, as in "for each element in the array, do something" for( int grade : grades ) System.out.println( grade );

12 Grades Example Create an array of size 10
Populate the array with 10 random numbers between 80 and 100 Use a method to print the array Pass an array as a parameter Calculate the average grade Display the grades that are above average

13 Command-Line Arguments
What's the args in the main() method? Prints out args in main() and try running the program like the following: java <Program> hi you 100 Specify command line arguments in Eclipse: Run Configurations

14 More Array Examples Find the largest/smallest element
Find the index of the largest/smallest element Find if a value exists in an array Sequential search Binary search a sorted array


Download ppt "CS2011 Introduction to Programming I Arrays (I)"

Similar presentations


Ads by Google