Week 2-3 Intermediate Programming PDF

Document Details

Uploaded by Deleted User

NU Dasmariñas

Tags

java programming arrays recursion programming

Summary

This document is a set of lecture notes on intermediate programming, specifically focusing on arrays, ArrayLists, and recursion in Java. It covers topics like declaring and using arrays, traversing arrays, and methods that process arrays of data. The notes also include practical examples, conceptual explanations, and methods.

Full Transcript

Week 2 – 3 Array, ArrayList and Recursion CCPRGG2L – Intermediate Programming LEARNING OBJECTIVES At the end of lesson students should be able to: o Recognize Arrays, ArrayList and Recursion used in Java programming. o Differentiate single –dimensional array and multi - dimensional array....

Week 2 – 3 Array, ArrayList and Recursion CCPRGG2L – Intermediate Programming LEARNING OBJECTIVES At the end of lesson students should be able to: o Recognize Arrays, ArrayList and Recursion used in Java programming. o Differentiate single –dimensional array and multi - dimensional array. o Create program using array and recursion. PROBLEM: Often you will have to store a large number of values during the execution of a program. Suppose, for instance, that you need to read 100 numbers, compute their average, and find out how many numbers are above the average. Your program first reads the numbers and computes their average, then compares each number with the average to determine whether it is above the average. In order to accomplish this task, the numbers must all be stored in variables. You have to declare 100 variables and repeatedly write almost identical code 100 times. Writing a program this way would be impractical. So, how do you solve this problem? SOLUTION: An efficient, organized approach is needed. Java and most other high-level languages pro- vide a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. In the present case, you can store all 100 numbers into an array and access them through a single array variable. SOLUTION: To use an array in a program, you must declare a variable to reference the array and specify the array’s element type. Here is the syntax for declaring an array variable: Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below: NOTE!!! An array variable that appears to hold an array actually contains a reference to that array. Strictly speaking, an array variable and an array are different, but most of the time the distinction can be ignored. NOTE!!! When space for an array is allocated, the array size must be given, specifying the number of elements that can be stored in it. The size of an array cannot be changed after the array is created. NOTE!!! The array elements are accessed through the index. Array indices are 0 based; that is, they range from 0 to arrayRefVar.length-1. Here are some examples of processing arrays: double[] myList = new double; (Initializing arrays with input values) The following loop initializes the array myList with user input values. Here are some examples of processing arrays: double[] myList = new double; (Initializing arrays with random values) The following loop initializes the array myList with random values between 0.0 and 100.0, but less than 100.0. Here are some examples of processing arrays: double[] myList = new double; (Displaying arrays) To print an array, you have to print each element in the array using a loop like the following: Here are some examples of processing arrays: double[] myList = new double; (Summing all elements) Use a variable named total to store the sum. Initially total is 0. Add each element in the array to total using a loop like this: Here are some examples of processing arrays: double[] myList = new double; (Finding the largest element) Use a variable named max to store the largest element. Ini- tially max is myList. To find the largest element in the array myList, compare each element with max, and update max if the element is greater than max. Here are some examples of processing arrays: double[] myList = new double; (Finding the smallest index of the largest element) Often you need to locate the largest element in an array. If an array has more than one largest element, find the smallest index of such an element. Suppose the array myList is 51, 5, 3, 4, 5, 56. The largest element is 5 and the smallest index for 5 is 1. Use a variable named max to store the largest element and a variable named indexOfMax to denote the index of the largest element. Initially max is myList, and indexOfMax is 0. Compare each element in myList with max, and update max and indexOfMax if the element is greater than max. For-each Loops Java supports a convenient for loop, known as a for-each loop or enhanced for loop, which enables you to traverse the array sequentially without using an index variable. For example, the following code displays all the elements in the array myList: In general, the syntax for a for-each loop is CAUTION! Accessing an array out of bounds is a common programming error that throws a runtime ArrayIndexOutOfBoundsException. To avoid it, make sure that you do not use an index beyond arrayRefVar.length – 1. Programmers often mistakenly reference the first element in an array with index 1, but it should be 0. This is called the off-by-one error. It is a common error in a loop to use

Use Quizgecko on...
Browser
Browser