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 accessing array elements for use in our programs

3 Get the Values stored in an Array
This is the same as retrieving the values from a regular array. We simply need to use its name and the value is returned. With an array, we simply need to state the name. However, we also need to specify an index to retrieve as you can only get one piece of data at a time Example: Add the values stored in the nums array of 5 elements. int [] nums = new int[]{3,7,-2,4,0}; int sum = nums[0] + nums[1] + nums[2] + nums[3] + nums[4]; System.out.println(“Sum: “ + sum);

4 Array’s Length At times in your program you may be using an array as a parameter for a subprogram. When this is the case, you may not know much about the array. One thing we can check about any array is its length (number of elements) This is NOT the number of elements with values, it is the number of all elements with or without data To do this simply refer to the length property of the array Example: Display the length of an array named studentNames System.out.println(“Num Students: “ + studentNames.length);


Download ppt "Arrays ICS2O."

Similar presentations


Ads by Google