You are on page 1of 5

Programming NC IV

1
Arrays

Arrays

This module covers the fundamentals of understanding, using and


manipulating arrays within the Java environment.
Upon completing this module, the students should be able to understand and
do the following:
1. declaring and initializing,
2. manipulating, and
3. copying arrays.

Declaring and Initializing


Before we dive in head first in this topic, let us first define what an array is.
Arrays are objects that contain a specific number of elements of a certain data
type. It may seem simple, but it gets complicated as we go on.

Declaring Arrays
Arrays declarations in Java are almost the same as in the C-based programs.
Well, Java is built based on C, so you get the idea?
Anyway here’s the syntax in declaring an array:
<data type> [ ] <array name>;
Example:
int[] myArray;
char[] newArray;

Use data type followed by open and close braces to tell the interpreter that it
is an array, followed by the array’s name.
The braces can also be at the end of the array’s name, but this practice is
strongly discouraged as it produces weird complications.
Wait, there’s more! Not only ints can be stored in arrays; all these can be
stored in arrays:
 byte[ ]
 short[ ]
 long[ ]
 float[ ]
 double[ ]
 boolean[ ]
 char[ ]
 String[ ]
Course Module
Initializing Arrays
Initialization values can be put in arrays in a lot of ways. Here are a few
examples.

Figure 1. Initializing Arrays

In Figure 1, the array is initialized one by one with each initialization using
one assignment declaration.
Line 7 says “myArray = new int[5]”, this line tells the interpreter to give
the array “myArray” enough memory for (5) five elements.

Figure 2. Initializing Arrays (2)

An old man once said, “In every long method, there is a short cut”.
He declared arrays this way. As you can see in Figure 2, the line where the
memory allocation process is missing.
This is because the number of initialization values are already telling the
interpreter to allocate that much memory to it.

Multi-Dimensional Arrays
You may see arrays as linear set of values but as mentioned, it gets
complicated really fast.
Arrays can have more than one dimension. That’s right, an array can have
one, two, three and any number of dimensions you can handle, if you are
extremely good at tracking things, or your computer, if it can handle that
amount of data and calculations.

Figure 3. Array
Programming NC IV
3
Arrays

Figure 3 is a representation of a 1 dimensional array. As seen above, it is


linear and very simple: a set of memory blocks allocated for the array, a set of
values and indexes going one direction.

Figure 4. Two Dimensional Array

Figure 4 now is a bit more complex as it has values going in the x and y axis.
This is called a two dimensional array.
This type of array can be declared as: “int[ ][ ] myArray”, the two
braces tells the interpreter that the array is 2-dimensional.
Accessing the array contents can be manual for singling out specific values
and/or automated via loops which are very common for searches, sorts and
copying from one array to another.

Figure 5. Multidimensional Array

Course Module
Finally, we have here a 3-dimensional array shown in Figure 5 which utilizes
the x, y and z axes. Can there be more?
Yes, there can be more than three dimensions.
Though we won’t be touching that, unless you are going to use them for
physics simulations, rocket science or creating tesseracts.
This is just the same as the two dimensional array, the declaration would just
add another pair of braces [ ] like “int[ ][ ][ ] my array”. The first
pair of braces holding the x-axis, the second holding the y-axis and the third
holding the x-axis.

Copying Arrays
Look at the program below:

Figure 6. Copying Arrays

One of the basic operations that can be done with arrays is copying one
array to another.
This can be very useful when you want to mess with the data in an array by
creating a duplicate to process and retaining the original for later use.
Figure 6 shows how such an operation can be achieved.
Lines 6-7 declare and initialize the array.
Line 8 declares an array with the size of 5, and line 9 copies the array
“myArray” from index(0) onwards.
Since the destination array “newArray” only has space for 5 elements, the
operation only copies the first 5 elements of the first array and disregards
the rest.
Programming NC IV
5
Arrays

Array Manipulation
Manipulating arrays in Java with built-in methods can ease the hard job of
doing them manually. Figure 6 is an example of array manipulation. There
are other ways of doing such operations.
Other methods, that can be called from “java.util.Arrays”, to manipulate
arrays are:
 Binarysearch – array search that returns the index of a specific
value
 Equals – compares two arrays and returns true or false
 Fill – fills all the indexes of the array with a specific value
 Sort/parallelSort – sorts the array into an ascending order, the
parallel sort is only introduced in ‘java SE8’ that utilizes all the
cores in your system to calculate faster.

References
Oracle. Java™ Platform Standard Ed. 7. Retrieved from:
https://docs.oracle.com/javase/tutorial/java/

Course Module

You might also like