After you finish this tutorial, you will understand:
- The array data structure.
- The use of arrays to store, sort and search lists and tables of values.
- How to declare an array, initialize an array and refer to individual elements in am array.
- How to pass an array to a method.
- Some basic sorting techniques.
- How to declare and manipulate multi-dimensional arrays.
- Quiz on Arrays
What is an array? We all understand the concept of an array. If I say that you have a bookcase that has ten shelves and you can place one book on a shelf. We all understand this instruction.
Furthermore, if I say once you place the first book on a shelf, the other nine books must be of the same type (i.e., dictionary, mystery, computer science, etc.), we all understand this instruction. If we all
understand the above instructions, then we understand the concept of an array. A bookcase contains books. An array contains data. An array of size ten contains 10 pieces of data. This is just like our
bookcase that contained ten books with one book per shelf. An array is an important data structure in any programming language. An array is a fixed-length structure that stores multiple values of the same data type (i.e., int, float, long, char, double, etc.). You can group values of the same type within arrays.
You cannot mix data types. For instance, you cannot store data of type int and float in the same array of primitive data types. . Arrays are supported directly by the Java programming language; there is no array class. Arrays are implicit extensions of the Object class, so you can assign an array to a variable whose type is declared as Object.
- Data structures consisting of related data items of the same type
- Static - remain the size once they are created
- An array is a group of contiguous memory locations that all have the same name and type
- Most variables occupy a single location. The name of that variable is a reference to a memory location
where that value of that variable is stored. Consider the following example.
| int a = 10 ;
| 10 |
a -> +----+
address of where
'a' is located in
| int ar = new int ;
ar = 10 ;
| | | | | |
| 10 | | | | |
ar -> +----+----+----+----+----+
address of where
'ar' is located in
|Figure 1: Variable locations
You will note that the value of '10' is stored at the location referenced by 'a'. In other words, the letter 'a' does not contain the value of '10'.
Your house address does not contain your possessions, it is the house at that location that contains your possessions. Also note that ar refers to the first location of the array ar.
- The first element in an array is referenced by subscript 0.
- The position number in the square brackets is called a subscript
- A subscript must be an integer or an integer expression
- When an expression is used as a subscript, the expression is evaluated first
- If the array name is ar, the length may be determined by
int len = ar.length ;
- Every array in Java knows its own length
- The brackets that enclose the subscript are an operator in Java
- An array reference may be reassigned to an array of a different size
Arrays occupy space in memory. The programmer specifies the data type of the elements and uses the new operator to
dynamically allocate the space required to hold the elements specified.
Arrays are considered objects and all objects must be allocated with the new operator.
For example, to allocate a ten_element array named ar
When arrays are allocated, the elements are automatically initialized to
int ar = new int;
---------------- or --------------------
ar = new int;
| Figure 2: Declarations and Allocations
For examples of declarations, consider the following:
- Zero for the numeric primitive types variables.
- false for boolean, or
- null for references (any nonprimitive type).
1- String b = new String[ 100 ],
s = new String [ 27 ];
2- double array1, array2;
3- double array1 = new double[ 10 ],
array2 = new double[ 20 ];
Every element of a String is a reference to a String that
has the value null by default