Processing data stored in
2-dimensional arrays:
background info
- The computer
only provide
instructions that
process
primitive
data types
(e.g.:
int)
- Data stored
in arrays
must be
processed
one element
at a time
(i.e.: use a loop)
- Use the following
length
variables
to help iterate
through
a 2-dimensional array:
arrayName.length = number of rows in the 2-dimensional array
arrayName[i].length = number of elements in row i
|
- The
for-loop used
to
process data stored in
a 2-dimensional
array arrayName is
as follows:
for ( int i = 0; i < arrayName.length; i++ ) // Row i
{
for ( int j = 0; j < arrayName[i].length; j++ ) // Elem j in row i
{
process data in arrayName[i][j]
}
}
|
|
Processing arrays 1:
initialization and printing
- Initialize
a 3×5 array myGrid
with myGrid[i][j] = i*j
and
print it out.
public static void main(String[] args)
{
double[][] myGrid = new double[3][5];
for ( int i = 0; i < myGrid.length; i++ )
for ( int j = 0; j < myGrid[i].length; j++ )
myGrid[i][j] = i*j;
for ( int i = 0; i < myGrid.length; i++ )
{
for ( int j = 0; j < myGrid[i].length; j++ )
System.out.print(myGrid[i][j] + " ");
System.out.println();
}
}
|
|
Processing arrays 1:
initialization and printing
Processing arrays 1:
initialization and printing
Processing arrays 1:
initialization and printing
Processing arrays 1:
initialization and printing
DEMO:
demo/09-multi-dim-array/03-array-processing/Demo1.java
Processing arrays 2:
sum all elements in an array
-
Compute the
sum of
all elements
stored in the 2-dimensional
array myGrid:
public static void main(String[] args)
{
double[][] myGrid = { {1, 2, 3},
{4, 5},
{6, 7, 8, 9} };
double sum; // sum = 1+2+3+4+5+6+7+8+9
// Compute sum = sum all elements in array myGrid
sum = 0;
for ( int i = 0; i < myGrid.length; i++ )
for ( int j = 0; j < myGrid[i].length; j++ )
sum = sum + myGrid[i][j];
System.out.println("sum = " + sum);
}
|
|
Processing arrays 2:
sum all elements in an array
Processing arrays 2:
sum all elements in an array
Processing arrays 2:
sum all elements in an array
Processing arrays 2:
sum all elements in an array
DEMO:
demo/09-multi-dim-array/03-array-processing/Demo2.java
Processing arrays 3:
sum all elements in one row of
the array
-
Compute the
row sum of
all elements
in each row of
a 2-dimensional
array myGrid:
public static void main(String[] args)
{
double[][] myGrid = { {1, 2, 3}, // --> sum[0] = 1+2+3
{4, 5}, // --> sum[1] = 4+5
{6, 7, 8, 9} }; // --> sum[2] = 6+7+8+9
double[] sum = new double[myGrid.length];
// Compute sum[i] = sum all elements in row i in myGrid
for ( int i = 0; i < myGrid.length; i++ )
{
sum[i] = 0;
for ( int j = 0; j < myGrid[i].length; j++ )
sum[i] = sum[i] + myGrid[i][j];
}
System.out.println( Arrays.toString(sum) );
}
|
|
Processing arrays 3:
sum all elements in one row of
the array
Processing arrays 3:
sum all elements in one row of
the array
Processing arrays 3:
sum all elements in one row of
the array
Processing arrays 3:
sum all elements in one row of
the array
DEMO:
demo/09-multi-dim-array/03-array-processing/Demo3.java
❮
❯