This lecture covers Java arrays, including one-dimensional and two-dimensional arrays, and explains their declaration and initialization. It highlights the use of loops for processing arrays and introduces the enhanced for loop for easier traversal. Additionally, the lecture discusses passing arrays to methods with an example of displaying array elements.
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views
Object Oriented Programming I - Lecture 9
This lecture covers Java arrays, including one-dimensional and two-dimensional arrays, and explains their declaration and initialization. It highlights the use of loops for processing arrays and introduces the enhanced for loop for easier traversal. Additionally, the lecture discusses passing arrays to methods with an example of displaying array elements.
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35
Object Oriented
Programming I Lecture 9
Prepared by. Nisreen Salih
In this lecture we will cover Java – Arrays One Dimensional Array Two Dimensional Array Java -Arrays Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. Declaring Array variables To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable: Array Declaration Array Declaration Examples Following statement declares an array variable, myList, creates an array of 10 elements of double type and assigns its reference to myList:
double[] myList = new double[10];
Following picture represents array myList. Here, myList holds ten double values and the indices are from 0 to 9. Array Declaration
The size of the array must be
of type int Array Declaration Array Declaration Example Array Initialization Arrays Processing Arrays When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known. Example Foreach loop JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable. Example The following code displays all the elements in the array myList: Example Write a program that finds the sum and the average of positive integers stored in an array of integers. This will produce the following result Passing Arrays to Methods Justas you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array: public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } Passing Arrays to methods This will produce the following result Any Questions