L05 - Arrays
L05 - Arrays
Arrays
Specifies an array of
variables of type int
We are creating
a new array object
For example:
int[] prices;
String[] names;
Creating a New "Empty" Array
Use this syntax: new int[20]
The new keyword creates an array of type
int that has 20 compartments
The new array can then be assigned to an
array variable:
int[] prices = new int[20];
When first created as above, the items in
the array are initialized to the zero value of
the datatype
int: 0 double: 0.0 String: null
Array Indexes
prices[0] = 6.75;
prices[1] = 80.43;
prices[2] = 10.02;
Constructing Arrays
Output: 5
Output:
Hello Selam.
Hello Sara.
Hello Abebe.
Hello Kebede.
Hello Alemu.
Modifying Array Elements
Example:
names[0] = “Bekele"
b. int[] arr;
arr = new int[4];
d. int[] arr;
Exercise 2
Example:
A landscape grid of a 20 x 55 acre piece of land:
We want to store the height of the land at each
row and each column of the grid.