This lab serves multiple goals:
- To introduce you to arrays of different datatypes,
- To introduce you to the different ways of declaring, assigning and initializing arrays,
- To iterate over arrays,
- To use the
Length
property of array,
Declaration, Assignment & Initialization of an Array {#declaration-assignment-initialization-of-an-array}
Warm-up
Write a program that implements the following steps:
- declares an array
myArray
ofint
of size , - initializes
myArray
with the values , , , and , - displays the content of
myArray
on the screen.
❓ Questions |
---|
What values are stored in this array after declaring it but before initializing it? |
There are a few different ways you can declare and initialize an array of size holding values , , , and . Can you think of two different ways of doing this? |
Answers:
- All the values in the array are set to 0,
- Two possible ways are
int[] myArray = new int[] {1, 2, 3, 4, 5};
andint[] myArray = {1, 2, 3, 4, 5};
.
Going wrong
Now, let us write incorrect statements. For each of the programs below, compile them and make sure you understand the error messages that are displayed.
Trying to set all the values at once after declaring
Out of bound error (read)
Out of bound error (write)
Reading the array as a whole (technically not an error)
This last statement is not “incorrect” in the sense that it will not prevent your program from executing, but it is not doing what you could or would have expected.
Second Array Manipulation
Write a program that
- declares an array
myArray
ofint
of size , - initializes
myArray
with the values , , , …, and , - displays the content of
myArray
. - sums the values stored in
myArray
and displays the result. - computes the product of the values stored in
myArray
and displays the result.
If you are unsure how to get started, you can use the following code.
Getting started:
Exploring Arrays
For this part, create a new array:
- declare a
char
array of length , name itletters
- initialize the first 4 indices of
letters
with the following values:'a', 'b', 'c', 'd'
- initialize index 5 of
letters
with the value'f'
Now, write the following statements:
- Write a statement to display the last
char
value inletters
(should displayf
). - Write a statement to display the value stored at index 4. What is that value? Why?
- Write a statement to display the characters in the first half of
the array (
'a', 'b', 'c'
but no others).
Execute your program to ensure you are seeing the expected output before proceeding.
Next, update the part of the program where letters
is declared and
change the length of letters
to . Do not modify any other parts of
the program. Then execute the program again.
Answer the following questions:
- What is the last
char
of theletters
array now, after changing its length? - Does your program still output the last
char
value inletters
array? - When displaying the first half of the array, does your program still
display the first half? (After changing the length, the first half
contains the values
'a', 'b', 'c', 'd'
) - If you did not get the last value or the first half you expected, can you think of a way to perform these array operations in a way that can accommodate arrays of different lengths?