Example:
|
Example:
|
|
Example:
|
Properties of arrays:
|
|
Example 1:
|
Example 2:
|
In Java, however, defining an array is a two-step process
|
Example:
Step 1:
double[] a; // The type "double[]" is the
// "array object reference" type
// The variable a contains an address of
// an array of doubles
|
Result of the array definition:
|
Explanation:
|
public class Avg2
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double[] a; // Define an (array) object variable named "a"
a = new double[5]; // new double[5]: create an array of 5 double variables
// and returns the location of the first element
******************************************
Ignore the rest of the program
******************************************
double sum, avg;
int i; // index
for ( i = 0; i <= 4; i++ )
{
System.out.print("Enter a number: ");
a[i] = in.nextDouble(); // Read in number
}
/* ---------------------------------------------------
Use the "running sum" algorithm to compute total
--------------------------------------------------- */
sum = 0.0;
for ( i = 0; i <= 4; i++ )
{
sum = sum + a[i];
}
avg = sum/5;
System.out.println(avg); // Print average
}
}
|
|
Example:
|
We can now use the reference (5000) stored in the variable a to locate the elements of the array !!!
|
Examples:
|
BUT: It will change the meaning of the data type completely Examples:
|
a = new double[5] ;
|
new ObjectType or: new DataType[ expression ] |
Notes:
|
Effect of the new operator:
|
We will illustrate the usage of the new operator by executing the above example program.
|
(The enclosing class definition has been omitted for brevity)
|
|
The program can now access the array element through its local variable a !!!
Java allows you to write both step in one combine statement
double[] a ; a = new double[5]; |
Can be written as: double[] a = new double[5]; |
Syntax: defining an array
Two steps:
datatype[] variableName; // Define a reference variable
variableName = new datatype[ EXPR ]; // Create array of length EXPR
// Assign starting address to
// variableName
|
double a[]; (instead of double[] a) |
| Alternate syntax to define array variable in Java |
|---|
Two steps:
datatype variableName[]; // Define a reference variable
variableName = new datatype[ EXPR ]; // Create array of length EXPR
// Assign starting address to
// variableName
|
public class Avg3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double a[] = new double[5]; // Define an array of 5 elements
double sum, avg;
int i; // index
for ( i = 0; i <= 4; i++ )
{
System.out.print("Enter a number: ");
a[i] = in.nextDouble(); // Read in number
}
/* ---------------------------------------------------
Use the "running sum" algorithm to compute total
--------------------------------------------------- */
sum = 0.0;
for ( i = 0; i <= 4; i++ )
{
sum = sum + a[i];
}
avg = sum/5;
System.out.println(avg); // Print average
}
}
|
The initial value depends on the data type of the array elements
| Data type | Initial value |
|---|---|
| A number type (e.g. int, double, ...) | 0 |
| boolean | false |
| char | the character NULL |
The syntax for defining an initialized array is:
datatype[] variableName = { list of initial values } ;
|
Example:
double[] a = { 3.0, 4.5, 6.7, 3.9, 9.0 } ;
|
Note:
|