150 likes | 295 Views
Day 23. 6.1 Arrays. Array Basics. An Array is a data type structure that can hold multiple values An array is like a ladder. You can specify how long the ladder should be and what values each step of the ladder has. Array Basics.
E N D
Day 23 6.1 Arrays
Array Basics • An Array is a data type structure that can hold multiple values • An array is like a ladder. You can specify how long the ladder should be and what values each step of the ladder has
Array Basics • An Array is a data type structure that can hold multiple values
Defining Arrays Data_Type[] arrayName = new Data_Type[# of steps];
Defining Arrays Data_Type[] arrayName = new Data_Type[# of steps]; For example, double[] temperatures = new double[10]
Defining Arrays Exercise:define an array which has - the name sentence- length/#steps of 5
Setting Values in Arrays ArrayName[0] = first value;ArrayName[1] = second value;…ArrayName[n - 1] = nth value;
Setting Values in Arrays Exercise: set the values of the array sentence
Setting Values in Arrays *Note that not every step in the array needs to be defined.
Reading values in Arrays The first value in the array is the 0thposition, the second value in the array is 1st position, …. the nthvalue in the array is thn-1 thposition.
Accessing values in Arrays To access the value at its third step, ArrayName[2];
Accessing values in Arrays Exercise: Print out the last word in your array
Accessing values in Arrays Exercise: Print out the last word in your array
Exercise: Add a clause to your for loop. If one of the words of your array is “yo”, then it should alert the user of “highly unprofessional language used”.Otherwise, print “all words are professional”.