Vous êtes sur la page 1sur 4

Computer Science Notes- Arrays

Picture of an Array
An array is an object that is used to store a list of values. It is made out of a contiguous
block of memory that is divided into a number of cells. Each cell holds a value, and all the
values are of the same type. In the example array pictured at right, each cell holds an int.
The name of this array is data. The cells are indexed 0 through 9. Each cell can be
accessed by using its index. For example, data[0] is the cell which is indexed by zero
(which contains the value 23). data[5] is the cell which is indexed by 5 (which contains
the value 14).
Facts:
The cells are numbered sequentially starting at zero.
If there are N cells in an array, the indexes will be 0 through N-1.
Sometimes the index is called a subscript. The expression data[5] is usually
pronounced "data-sub-five" as if it were an expression from mathematics: data5.

Before

The value stored in a cell of an array is sometimes called an element of the array. An array has a fixed number
of cells, but the values placed in those cells (the elements) may change over time.

Using Arrays
Every cell of an array holds a value of the same type. So, for example, you can have an
array of int, an array of double, and so on. You can have an array of object
references, such as an array of Student objects, or any other class.
The "before" array in the picture holds data of type int. A cell of this array can be used
anywhere a variable of type int can be used. For example,
data[3] = 99 ;

// assigns the value 99 to the 4th element of data

works just like an assignment to an int variable. After it has been executed, the array
looks like the "after" array in the picture.
The value in cell 3 of the array has been changed.

Arithmetic Expressions
After

Since data[3] contains an int it can be used anywhere an int variable may be used.
An arithmetic expression can contain a mix of literals, variables, and subscripted variables. For example, if x
contains a 10, then
(x + data[2]) / 4
evaluates to (10+14) / 4 -> 6.
Here are some other legal statements:
data[0] = (x + data[2]) / 4 ;

data[2] = data[2] + 1;
x = data[3]++ ;
// increment the data in cell 3
data[4] = data[1] / data[6];

Arrays are Objects


Array declarations look like this:
type[] arrayName;

//just declares, doesnt construct or initialize (like int x;)

This tells the compiler that arrayName contains a reference to an array containing type. However, the actual
array object is not constructed by this declaration. The declaration merely declares a reference variable
arrayName which, sometime in the future, is expected to refer to an array object.
Often an array is declared and constructed in one statement, like this:
type[] arrayName = new type[ length ]; // declares and constructs
// e.g. int[] list = new int[10];
This statement does two things:
(1) It tells the compiler that arrayName will refer to an array containing cells of type.
(2) It constructs an array object containing length number of cells.
An array is an object, but note the array constructor uses different syntax than other object constructors:
new type[ length ]
This names the type of data in each cell and the number of cells.
Once an array has been constructed, the number of cells it has does not change. Here is an example:
int[] data = new int[10];
This statement creates an array of 10 ints, puts a zero into each cell, and puts a reference to that object in
data.

Bounds Checking
data[ -1 ]
data[ 10 ]
data[ 1.5 ]
data[ 0 ]
data[ 9 ]
data[ j ]

always illegal
illegal
(given the above declaration)
always illegal
always OK
OK
(given the above declaration)
can't tell
(depends on the value of j)

Recall that: The length of an array is how many cells it has. An array of length N has cells indexed 0..(N-1)
Indexes must be an integer type. It is OK to have spaces around the index of a subscripted variable, for example
data[1] and data[ 1 ] are exactly the same as far as the compiler is concerned.
It is not legal to refer to a cell that does not exist.

If your program contains an expression that is always illegal, it will not compile. But often the size of an array
is not known to the compiler. The size of an array often is determined by data at run time. Since the array is
constructed as the program is running, the compiler does not know its length and can't detect some errors.

Array Initialization
Lacking any other information, the cells of an array are initialized to the default value for their type. Each cell
of a numeric array is initialized to zero. Each cell of an array of object references is initialized to null.
Of course, the program can assign values to cells after the array has been constructed. In the following, the array
object is constructed and each cell is initialized to 0. Then some assignment statements explicitly change some
cells:
class ArrayEg1
{
public static void main ( String[] args )
{
int[] stuff = new int[5];
stuff[0] = 23;
stuff[1] = 38;
stuff[2] = 7*2;
System.out.println("stuff[0]
System.out.println("stuff[1]
System.out.println("stuff[2]
System.out.println("stuff[3]

has
has
has
has

"
"
"
"

+
+
+
+

stuff[0]
stuff[1]
stuff[2]
stuff[3]

);
);
);
);

}
}

Using a Variable as an Index


The index of an array is always an integer type. It can be a literal, or an expression that evaluates to an integer.
For example, the following are legal:
int values[] = new int[7];
int index;
values[ 6 ] = 42;
// literal
index = 0;
values[ index ] = 71;
// put 71 into cell 0
index = 5;
values[ index ] = 23;
// put 23 into cell 5
index = 3;
values[ 2+2 ] = values[ index-3 ]; // same as values[ 4 ] = values[ 0 ];

Using an Expression as an Index


Using an expression for an array index is a very powerful tool. You often solve a problem by organizing the data
into arrays, and then processing that data in a systematic way using variables as indexes.

Initializer Lists

You can declare, construct, and initialize the array all in one statement:
int[] data = {23, 38, 14, -3, 0, 14, 9, 103, 0, -56 };
// when you know exactly what you want beforehand
This declares an array of int which is named data. Then it constructs an int array of 10 cells (indexed 0..9).
Finally it puts the designated values into the cells. The first value in the initializer list corresponds to index 0,
the second value corresponds to index 1, and so on. (So in this example, data[0] gets the 23.)
You do not have to say how many cells the array has. The compiler will count the values in the initializer list
and make that many cells. Once an array has been constructed, the number of cells does not change. Initializer
lists are usually used only for small arrays.

Copying Values from Cell to Cell


In this example, the int in cell 0 of valA is copied to cell 0 of
valB.
valB[ 0 ]

= valA[ 0 ] ;

//This is just like an assignment statement

Super Bug Alert: The following statement does not do the same
thing:
valB = valA ;
Remember that arrays are objects. The statement above will
merely copy the object reference in valA into the object
reference variable valB, resulting in two ways to access the
single array object, as seen in the second picture.
The object that valB previously referenced is now lost it has become garbage.

Additional Info:
If an array named data is constructed, the size of the array can be obtained with data.length. Note this is
not a method (like String length, e.g. str.length() ), so no parenthesis are used.
So a simple example to loop through and print all the elements of data is:
for (int i=0; i<data.length; i++) // (indices 0 length-1)
{
System.out.println(Item number +i+ is +data[i]);
}

IMPORTANT!!!!

Vous aimerez peut-être aussi