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 assigning values to an array at two times
During regular program execution When the array is defined

3 Assigning Values to an Array
This to is very similar to working with an individual variable. Example: Assign the value 7 to the num variable num = 7; When dealing with an array, it is the exact same process except we also must specify which element we want to hold the value. To do this we use square brackets and an index Example: Assign the value 7 to index 3 of the nums array nums[3] = 7;

4 Assign Values to the Whole Array
Often times you may know all the values of the array when it is created. Such as setting all the scores in a top 10 scoreboard to 0 to start. Unfortunately, there is no one line command to do this, you cannot just say nums = 0, this will break the program At this point in the course (this will change later) there are only two ways to assign all the elements of an array to a single value. One by one starting at index 0, e.g. scores[0] = 0 Assign them at the time of definition

5 Assign Values to the Whole Array
Previously our array define statement was simple: int [] nums = new int[5]; When assigning the values of each element at the time of definition we no longer need to give the number of elements in the square brackets Like the value 5 above This is because the compiler will know the number based on how many values you give it Example: Define a scores array of 5 ints, each with the starting value of 10 int [] scores = new int[]{10,10,10,10,10}; OR Just the values: int [] scores = {10,10,10,10,10}; Note the curly braces surround the values to be stored


Download ppt "Arrays ICS2O."

Similar presentations


Ads by Google