Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays ICS2O.

Similar presentations


Presentation on theme: "Arrays ICS2O."— Presentation transcript:

1 Arrays ICS2O

2 Objectives Learn about arrays and when to use them
Learn the syntax for declaring/defining a new array of any data type

3 What is an Array? An array of doubles
An array is a collection of related data of the same type. It is stored as a block of consecutive memory locations for quick access by the computer. Individual locations are called the array’s elements. When we say “element” we often mean the value stored in that element. 1.39 1.69 1.74 0.0 An array of doubles

4 What is an Array (cont’d)
Rather than treating each element as a separate named variable, the whole array gets one name. Individual array elements are referred to by using the array’s name and the element’s numbered address, called the index. For example, we could say the value 1.74 is stored in the nums array at index 2 nums[0] nums[1] nums[2] nums[3] 1.39 1.69 1.74 0.0 nums is this array’s name

5 Indices In Java, an index is written within square brackets following the array’s name, e.g. Assuming the array is named nums, nums[0] will reference the value in the nums array at index 0. Indices start from 0; the first element of an array nums is referred to as nums[0] and the n-th element as nums[n-1]. n-1 because of the 0 start. An index must be any whole number value from 0 to the array’s length-1 (total number of elements - 1)

6 Indices (cont’d) An index can be an integer variable or any expression that evaluates to an integer value. For example: (Assuming the array is named nums) nums[3]  references the 4th element of the array nums[studentNumber] nums[studentNumber – 2]

7 Indices (cont’d) In Java we MUST define an array with a specific length, think of it like reserving a table at a restaurant with a precise number of seats. This implies you cannot put more people at the table than you have chairs. The same works for an array, we cannot store more data than we promised to store. In Java, while the program is running if you try to access an element not in the range of indices (from 0 to length – 1) an “out of bounds” error will occur and crash the program.

8 Why Do We Need Arrays? The power of arrays stems from data organization. If we did not use arrays we would be forced to use individual variables in their place. Imagine creating a program that stored the name of every student in the school. You would need 1400 string variables or 1 string array with 1400 elements

9 Declaring(Defining) an Array
Like any variable, before we can use it we must define it so it is in the Java dictionary and the memory is reserved. RECALL: Defining an int variable named num int num; Defining an array is the same, but we have to remember we are defining a collection of data, so our names are typically pluralized. We also must inform the compiler that we are defining an array and how big it will be Example: Define an array of 5 ints named nums int [] nums = new int[5]; The square brackets tell the compiler it is an array, the second have reserves enough memory for 5 int values


Download ppt "Arrays ICS2O."

Similar presentations


Ads by Google