Briefly explain what an array is. As part of your answer make use of a labelled example to show the declaration and components of an array Note: You will receive 4 marks for your explanation and 6 marks for your labelled example.
An array is a collection that stores a fixed number of values of a same type.
Examples:
Declaring an array:
// declares an array of integers
int[] ages;
// allocates memory for 10 integers
ages = new int[10];
// declares an array of strings
String[] names;
// allocates memory for 10 strings
names = new String[10];
Initializing array element:
String[] names= { "Peter", "Mary", "Mike",
"Denis", "Julia",
"Mark", "Olivia" };
String[] names= new Array[7];
names=[0] = "Peter";
names=[1] = "Mary";
names=[2] = "Mike";
names=[3] = "Denis";
names=[4] = "Julia";
names=[5] = "Mark";
names=[6] = "Olivia";
Comments
Leave a comment