A java array can hold multiple values of the same data type simultaneously. The size of java arrays need to be specify before using it. The primitive variables we have worked with so far are designed to hold one value at a time. A java array, however, is an object that can store a group of values with the same data types. Creating and using an array in java is similar in creating and using any other type of object. We declare a reference variable and use the new key word to create an instance of the array in memory.
int[] num;
The statement above declares num as an array reference variable. The num variable can reference an array of int values. Note, the statement looks like a regular declaration statement we've seen before, except for the brackets that appear after the int. The brackets indicate that we are referencing an array. The next step is use the new key word to create an array and assign its address to the num variable.
num = new int[5];
The number inside the brackets is the size of the array. It indicates the number of element or values of the array that it can hold. Hence, the above array can hold 6 elements, because an array always starts at an index at 0. Note, we are allow to put the above two statements together and it works the same.
int[] num = new int[5];
The two declarations are equivalent.
Remember, array can only hold the same data type values. Hence, we have to create a whole new array if we want to hold other values such as Strings, char, double, float and so on. The declaration is exactly the same, except we need to change the data type. Here is some examples:
int[] num = new int[5] // an array that holds integer values
Strings[] num = new Strings[5] // an array that holds String values
char[] num = new char[5] // an array that holds character values
Accessing Java Array Elements
We know how to create an array, but how do we access them. Each elements inside an array is assigned a number known as a subscript. A subscript is used as an index to pinpoint a specific element within an array. The first element of an array is assigned with a subscript 0, the second element is assigned with 1, and so forth. The subscript always starts at zero and ends with one less than the total elements in the array. Lets see some examples on creating an array and accessing them.
Note: num[0], it's pronounced as " num sub zero "
Example
The example above is pretty self explanatory on how we store an array and how we access them. Though, there is still a question remain, what if we have a large java array how are we going to store or access a very large array? Now, it's time to go back to the beauty of loops, we can use a traditional for loop or enhanced for loop to access or store an array. We would only do one example on the enhanced for loop and uses the traditional for loop through out this chapter. Each array in java has a public filed named length, the filed contains the number of elements in the array, and with its help we are able to determine the size of an array. Now we have enough knowledge let see some examples.
ClickHere to download NameAccess.java
The above example shows simple declaration of java string array and how to access them using two different loops, one is the traditional for loop and the other one is the enhanced for loop.
ClickHere to download EmployeeArray.java
Passing Array As Arguments to Methods
An array can be passed as an argument to a method. To pass an array, we pass the value in the variable that reference the array. Lets see a basic example so we can have a clear idea on how it works.
ClickHere to download PassArray.java
The above example shows how to pass an array to a void method, now lets observe an example on how to pass an array into a returning method and uses the array.
ClickHere to download ArrayReturn.java
Searching In The Array
ClickHere to download SearchArray.java
Two Dimensional Arrays
So far the array we've been working on is only one-dimensional. A two dimensional array is an array of arrays, we can thought of it as having rows and columns. A two dimensional array sometimes called 2D arrays, it can hold multiple sets of data. We would not discuss much here, although we can still see a example of how a two dimensional array works.
ClickHere to download TwoDimensional.java
In two dimensional array values are store
according to rows and columns. Lets retrospect, back in algebra II we learned
what a matrix is. A two dimensional array is exactly a matrix, where each values
are store in a different rows and columns.