Arrays
Arrays
Prepared by:
Miss Myra G. Flores
What is an Array
It stores multiple data items of the same
data type in a contiguous block of
memory, divided into a number of slots.
Each slot is referred to as a member, or
element, of the array and each slot may
hold different value.
◦ All value must be of the same data type
Accessing Array Element
Index number or subscript, is assigned
to each member of the array allowing the
program and the programmer to access
individual values when necessary
◦ They begin with zero
◦ The practice of zero-indexing is common to
many programming languages
◦ It may help to think of the first array member
as being zero members away from the
beginning of the array
Array Notation
Array must be declared
List the data type followed by a set of square
brackets, followed by the identifier name.
dataType [ ] identifier;
Example
int [ ] ages; or
int ages [ ];
Note: Java uses brackets instead of parenthesis for
arrays, so as not to confuse them with methods.
Array
You must construct the array and specify
its length with a constructor statement
Example
ages = new ages[100];
Often an array is declared and constructed
in a single statement
int[ ] ages = new int[100];
Coding Guidelines:
1. It is usually better to initialize or instantiate the array
right away after you declare it.
For example, the declaration,
int []arr = new int[100];
is preferred over,
int []arr;
arr = new int[100];
2. The elements of an n-element array have indexes from 0
to n-1. Note that there is no
array element arr[n]! This will result in an array-index-
out-of-bounds exception.
3. You cannot resize an array.
Array Length
An array is an object, it is constructed out of main
storage as the program is running and not at
compile time.
The array constructor uses different syntax than
most object constructors because it must initialize
the length or total number of elements
Length of an array is a fixed-length structure.
Once it has constructed, the number of elements it
contains does not change.
The length field of an array returns the size of the
array
Array Length
It can be used by writing,
arrayName.length
For example, given the previous example, we can re-write it
as,
public class ArraySample
{
public static void main( String[] args ){
int[] ages = new int[100];
for( int i=0; i<ages.length; i++ ){
System.out.print( ages[i] );
}
}
}
Coding Guidelines:
1.When creating for loops to process the elements of an
array, use the array object's
length field in the condition statement of the for loop.
This will allow the loop to
adjust automatically for different-sized arrays.
2.Declare the sizes of arrays in a Java program using
named constants to make them
easy to change. For example,
final int ARRAY_SIZE = 1000; //declare a constant
...
int[] ages = new int[ARRAY_SIZE];
Example program
public class ArraySample{
public static void main( String[] args ){
int[] ages = new int[100];
for( int i=0; i<100; i++ ){
System.out.print( ages[i] );
}
}
}
Example
//creates an array of boolean variables with ientifier
//results. This array contains 4 elements that are
//initialized to values {true, false, true, false}
boolean results[] ={ true, false, true, false };
//creates an array of 4 double variables initialized
//to the values {100, 90, 80, 75};
double []grades = {100, 90, 80, 75};
//creates an array of Strings with identifier days and
//initialized. This array contains 7 elements
String days[] = { “Mon”, “Tue”, “Wed”, “Thu”,
“Fri”, “Sat”, “Sun”};
Example
System.out.println(answer[5]);
◦ answer is the name of the array, the sixth element of
the array will display during executing
Temperature[3] = 78;
◦ After it has been executed, the fourth member of
the array, temperature, will hold the value 78.
overtime = (hours[6] – 40)* rate[6] *1.5;
◦ Overtime is calculated by taking the seventh
number of the hours array, subtracting 40 and then
multiplying it by the seventh of the rate array and
finally by 1.5 for time-and-a-half pay.
Exercise
Greatest number- Using BufferedReader or
JOptionPane, ask for 10 numbers from the user.
Use an array to store the values of these 10
numbers. Output on the screen the number
with the greatest value.
Days of the Week - Create an array of Strings
which are initialized to the 7 days of the week.
For Example, String days[] = {“Monday”,
“Tuesday”….}; Using a while-loop, print all
the contents of the array. (do the same for do-
while and for loop)
Sources:
Shelly, G.B., Cashman, T.J., Starks, J.L.
(2001). Java Programming Complete
Concepts and Techniques. Course
Technology division of Thomson
Learning. Singapore
JEDI.(2006). Introduction to
Programming I.Java Education &
Development Initiative. Version 1.3. June
2006