List of values.
A list of values where every member is of the
same type.
Each member in an array is called an element.
Each array has a variable name associated with it.
Each element in an array has an index.
Indexing always starts with zero.
eclaring an Array
int [] scores ;
Allocates a variable that can ³point´ to an
array of integers
Array variables declared but not created have
a value of NULL
Inspecting a declared array
Creating an Array
int [] values = new int[6] ;
efault values for integer arrays - zero
Arrays are Reference ata Types ± They contain addresses not values
Creating, Initializing and
Populating an array
After an array is declared:
scores = new int [6] ; //create
scores = {3,5,6,7,4,3}; // initialize
Using a loop with a variable for the
index to populate an array.
eclaring, Creating, Initializing
int [] scores = new int [6] ;
scores = {23, 34, 55, 64, 23, 21};
int[] scores = {23, 34, 55, 64, 23, 21};
Accessing Elements
int grade = scores[index] ;
where index represents the specific element.
uestion
Assume correct declaration of an integer
array called numbers.
Will the following code compile and work
as intended?
Answer
Yes
uestion
Create an array called names that contains
15 Strings.
Create should (probably) assume the array
is already declared.
Answer(s)
names = new String [15] ;
or String [] names = new String[15] ;
uestion
Initialize an array of integers to the values
31, 32, 33, 34, and 35. Use an array
initializer.
Answer(s)
someName = {31, 32, 33, 34, 35 } ;
int [] someName = {31, 32, 33, 34, 35 } ;
uestion
Write an output statement that outputs the
third element in an array
Answer
System.out.println(someName[2]);
Third element will have an index of 2.
Fifth element will have an index of 4.
An index of 17 is the 18th element of the
array.
uestion
Write an output statement that will print the
number of elements in an array of integers
called numbers.
Answer
System.out.println(numbers.length);
Note - NO parentheses