Notes5 Java Arrays
Notes5 Java Arrays
Java Arrays
1 Objectives
In this section, we will be discussing about Java Arrays. First, we are going to define what arrays are,
and then we are going to discuss on how to declare and use them.
2 Introduction to arrays
In the previous sections, we have discussed on how to declare different variables using the primitive
data types. In declaring variables, we often use a unique identifier or name and a datatype. In order to
use the variable, we call it by its identifier name.
For example, we have here three variables of type int with different identifiers for each variable.
int number1;
int number2;
int number3;
number1 = 1;
number2 = 2;
number3 = 3;
As you can see, it seems like a tedious task in order to just initialize and use the variables especially if
they are used for the same purpose. In Java and other programming languages, there is one capability
wherein we can use one variable to store a list of data and manipulate them more efficiently. This type
of variable is called an array.
3 Declaring Arrays
Arrays must be declared like all variables. When declaring an array, you list the data type, followed by
a set of square brackets[], followed by the identifier name. For example,
int []ages;
or you can place the brackets after the identifier. For example,
int ages[];
After declaring, we must create the array and specify its length with a constructor statement. This
process in Java is called instantiation (the Java word for creates). In order to instantiate an object,
we need to use a constructor for this. We will cover more about instantiating objects and constructors
//declaration
int ages[];
//instantiate object
ages = new int[100];
In the example, the declaration tells the Java Compiler that the identifier ages will be used as the
name of an array containing integers, and to create or instantiate a new array containing 100
elements.
Instead of using the new keyword to instantiate an array, you can also automatically declare, construct
and assign values at once.
Examples are,
An index number or subscript is assigned to each member of the array, allowing the program and
the programmer to access individual values when necessary. Index numbers are always integers.
They begin with zero and progress sequentially by whole numbers to the end of the array .
Take note that the elements inside your array is from 0 to (sizeOfArray-1).
Take note that once an array is declared and constructed, the stored value of each member of the
array will be initialized to zero for number data. However, reference data types such as Strings are not
initialized to blanks or an empty string “”. Therefore, you must populate the String arrays explicitly.
The following is a sample code on how to print all the elements in the array. This uses a for loop, so
our code is shorter.
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.
5 Array length
In order to get the number of elements in an array, you can use the length field of an array. The
length field of an array returns the size of the array. It can be used by writing,
arrayName.length
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];
6 Multidimensional Arrays
Multidimensional arrays are implemented as arrays of arrays. Multidimensional arrays are declared by
appending the appropriate number of bracket pairs after the array name. For example,
// character array 8 x 16 x 24
char[][][] threeD = new char[8][16][24];
To access an element in a multidimensional array is just the same as accessing the elements in a one
dimensional array. For example, to access the first element in the first row of the array dogs, we write,
System.out.print( dogs[0][0] );
7 Exercises
7.1 Days of the Week
Create an array of Strings which are initialized to the 7 days of the week. For Example,
Using a while-loop, print all the contents of the array. (do the same for do-while and for-loop)
Name : Florence
Tel. # : 735-1234
Address : Manila
Name : Joyce
Tel. # : 983-3333
Address : Quezon City
Name : Becca
Tel. # : 456-3322
Address : Manila