Basics in Java Array
Basics in Java Array
Arrays
The Basics of Arrays in Java
Creating an Array
There are two kinds of data in Java: primitive types (such as int and double), and objects. In
many programming languages (even object-oriented ones like C++) arrays are a primitive type,
but in Java they're treated as objects. Accordingly you must use the new operator to create an
array.
The [ ] operator is the sign to the compiler we're naming an array object and not an ordinary
variable. You can also use an alternative syntax for this operator, placing it after the name
instead of the type:
int intArray[ ] = new int[100]; // alternative syntax However, placing the [ ] after the int makes it
clear that the [ ] is part of the type, not the name. Because an array is an object, its name—
intArray in the code above—is a reference to an array; it's not the array itself. The array is
stored at an address elsewhere in memory, and intArray holds only this address. Arrays have a
length field, which you can use to find the size, in bytes, of an array:
int arrayLength = intArray.length; // find array length Remember that this is the total
number of bytes occupied by the array, not the number of data items you have placed in it. As
in most programming languages, you can't change the size of an array after it's been created.
Array elements are accessed using square brackets. This is similar to how other languages work:
Initialization
Unless you specify otherwise, an array of integers is automatically initialized to 0 when it's
created. Unlike C++, this is true even of arrays defined within a method (function). If you create
an array of objects, like this:
then, until they're given explicit values, the array elements contain the special null object. If you
attempt to access an array element that contains null, you'll get the runtime error "Null Pointer
Assignment." The moral is to make sure you assign something to an element before attempting
to access it. You can initialize an array of a primitive type to something besides 0 using this
syntax:
Perhaps surprisingly, this single statement takes the place of both the reference declaration
and the use of new to create the array. The numbers within the curly braces are called the
initialization list. The size of the array is determined by the number of values in this list.
An Array Example
Let's look at some example programs that show how an array can be used. We'll start with an
old-fashioned procedural version, and then show the equivalent objectoriented approach.
Listing 2.1 shows the old-fashioned version, called array.java.
// array.java
// demonstrates Java arrays
// to run this program: C>java ArrayApp import java.io.*;
// for I/O ////////////////////////////////////////////////////////////////
Insertion
Inserting an item into the array is easy; we use the normal array syntax arr[0] = 77; We also
keep track of how many items we've inserted into the array with the nElems variable.
Searching
The searchKey variable holds the value we're looking for. To search for an item, we step
through the array, comparing searchKey with each element. If the loop variable j reaches the
last occupied cell with no match being found, then the value isn't in the array. Appropriate
messages are displayed: "Found 66" or "Can't find 27."
Deletion
Deletion begins with a search for the specified item. For simplicity we assume (perhaps rashly)
that the item is present. When we find it, we move all the items with higher index values down
one element to fill in the "hole" left by the deleted element, and we decrement nElems. In a
real program, we'd also take appropriate action if the item to be deleted could not be found.
Display
Displaying all the elements is straightforward: we step through the array, accessing each one
with arr[j] and displaying it.
Program Organization
The organization of array.java leaves something to be desired. There is only one class,
ArrayApp, and this class has only one method, main(). The program is essentially an old-
fashioned procedural program. Let's see if we can make it easier to understand (among other
benefits) by making it more object-oriented. We're going to provide a gradual introduction to
an object-oriented approach, using two steps. In the first, we'll separate the data storage
structure (the array) from the rest of the program. This remaining part of the program will
become a user of the structure. In the second step, we'll improve the communication between
the storage structure and its user.