Unit_4_-_Arrays
Unit_4_-_Arrays
What is an Array?
1
The size of an array is fixed
2
Arrays Subscripts
Arrays in Java
3
Declaring Arrays
Constructing Arrays
4
The length variable for 1D Arrays
5
Example
import io.*;
public class arrayTest
{
public static void main( String args[])
{
int x[], y[][], z[][][];
x = new int[10];
y = new int[5][5];
z = new int[3][3][3];
System.out.println("x has " + x.length + " elements");
System.out.println("y has " + y.length + " rows ");
System.out.println("y has "+y[0].length + " columns
");
System.out.println("z has " + z.length + " planes");
System.out.println("z has " + z[0].length + " rows ");
System.out.println(
"z has " + z[0][0].length + " columns ");
}
}
6
Example
7
Example
import io.*;
public class raggedeg2
{
public static void main( String [] args)
{
int ragged [][];
ragged = createArray(10);
fillArray( ragged);
outputArraySize( ragged);
}
8
private static void fillArray( int array[][])
{
for(int row =0; row<10; row++)
for(int col=0; col<row+2;col++)
array[row][col] = row*col;
}
private static void outputArraySize( int [][] array)
{
System.out.println("arrays has " + array.length + "
rows");
for(int row =0; row<10; row++)
System.out.println("row " + row + " has “ +
array[row].length + " columns");
}
}
Array Initializer
18
Index values of 0, 1, 2, 3, 4
9
Exercise
19
10
Enhanced for Statement
21
11
Enhanced for Statement (Cont.)
23
Usage
Canaccess array elements
Cannot modify array elements
12
1 // Fig. 7.20: VarargsTest.java
2
3
// Using variable-length argument lists.
Outline
25
4 public class VarargsTest
5 {
6 // calculate average
7 public static double average( double... numbers )
VarargsTest
8 {
.java
(1 of 2)
9 double total = 0.0; // initialize total Line 7
Lines 12-13
10 Line 15
11 Method
// calculate total using the enhanced average receives a variable
for statement
12 for ( double d : numbers ) length sequence of doubles
13 total += d;
14
15 return total / numbers.length;
Calculate
the total of the
16 } // end method average
doubles in the array
17
18 public static void main( String args[] )
19 {
Access numbers.length to obtain
20 double d1 = 10.0; the size of the numbers array
21 double d2 = 20.0;
22 double d3 = 30.0;
23 double d4 = 40.0;
24
13
Using command-line Arguments
Command-line arguments
Pass arguments from the command line
String args[]
Appear after the class name in the java command
java MyClass a b
Number of arguments passed in from command line
args.length
14
29 // display array index and value
29
30
31
for ( int counter = 0; counter < array.length; counter++ )
System.out.printf( "%5d%8d\n", counter, array[ counter ] );
Outline
32 } // end else
33 } // end main
34 } // end class InitArray
java InitArray 10 1 2
Index Value
0 1
1 3
2 5
3 7 Three command-line arguments are
4 9
5 11 10, 1 and 2
6 13
7 15
8 17
9 19
java.utils.Arrays
30
15
To be equal, the arrays must have the same
number of elements and each corresponding
element must be equivalent, using the equals( )
for each element
For primitives, that primitive’s wrapper class equals( )
is used
such as Integer.equals()
16