Multi-Dimensional Arrays

advertisement
Multi-Dimensional
Arrays
A Table of Values
Balances for Various Interest Rates
Compounded Annually
(Rounded to Whole Dollar Amounts)
Year
5.00%
5.50%
6.00%
1
$1050
$1055
$1060
2
$1103
$1113
$1124
3
$1158
$1174
$1191
Row and Column Indices for an
Array
Indices
0
1
2
0
$1050
$1055
$1060
1
$1103
$1113
$1124
2
$1158
$1174
$1191
Multi-Dimensional Array
Basics
 We declare a variable, table, to be a two-
dimensional array as follows:
int [][] table = new int
[10][6];
 This is the same as for one-dimensional
arrays except we use a second pair of
square brackets in two places.
 You can have arrays of any number of
dimensions.
Multi-Dimensional Array Basics
(cont’d)
 In a two-dimensional array we consider
the first index to represent the row
number and the second to represent the
column.
 As was true of the indexed variables for
one-dimensional arrays, indexed
variables of multi-dimensional arrays are
variables of the base type and can be
used anyplace that a variable of the
base type is allowed.
Example of Using a TwoDimensional Array
/**
Displays a two-dimensional table showing how interest
rates affect bank balances.
*/
public class InterestTable
{
public static void main(String[] args)
{
int[][] table = new int[10][6];
int row, column;
for (row = 0; row < 10; row++)
for (column = 0; column < 6; column++)
table[row][column] =
balance(1000.00, row + 1, (5 + 0.5*column));
System.out.println("Balances for Various Interest Rates");
System.out.println("Compounded Annually");
System.out.println("(Rounded to Whole Dollar Amounts)");
System.out.println("Years 5.00% 5.50% 6.00% 6.50% 7.00% 7.50%");
System.out.println( );
for (row = 0; row < 10; row++)
{
System.out.print((row + 1) + "
");
for (column = 0; column < 6; column++)
System.out.print("$" + table[row][column] + " ");
System.out.println( );
}
}
Example of Using a TwoDimensional Array (cont’d)
/**
Returns the balance in an account that starts with startBalance
and is left for the indicated number of years with rate as the
interest rate. Interest is compounded annually. The balance is
rounded to a whole number.
*/
public static int balance(double startBalance, int years, double
rate)
{
double runningBalance = startBalance;
int count;
for (count = 1; count <= years; count++)
runningBalance = runningBalance*(1 + rate/100);
return (int) (Math.round(runningBalance));
}
}
Multi-Dimensional Array
Parameters and Returned Values
 Methods may have multi-dimensional
array parameters and may return a multidimensional array.
 The situation is similar to that of the onedimensional case, except that you use
more square brackets.
Example Using a MultiDimensional Array Parameter
/**
Displays a two-dimensional table showing how interest
rates affect bank balances.
*/
public class InterestTable2
{
public static void main(String[] args)
{
int[][] table = new int[10][6];
int row, column;
for (row = 0; row < 10; row++)
for (column = 0; column < 6; column++)
table[row][column] =
balance(1000.00, row + 1, (5 + 0.5*column));
System.out.println("Balances for Various Interest Rates");
System.out.println("Compounded Annually");
System.out.println("(Rounded to Whole Dollar Amounts)");
System.out.println("Years 5.00% 5.50% 6.00% 6.50% 7.00% 7.50%");
System.out.println( );
showTable(table);
}
Example Using a MultiDimensional Array Parameter
(cont’d)
/**
}
Precondition: The array displayArray has 10 rows and 6 columns.
Postcondition: The array contents are displayed with dollar signs.
*/
public static void showTable(int[][] displayArray)
{
int row, column;
for (row = 0; row < 10; row++)
{
System.out.print((row + 1) + "
");
for (column = 0; column < 6; column++)
System.out.print("$" + displayArray[row][column] + " ");
System.out.println( );
}
}
public static int balance(double startBalance, int years, double rate)
{
double runningBalance = startBalance;
int count;
for (count = 1; count <= years; count++)
runningBalance = runningBalance*(1 + rate/100);
return (int) (Math.round(runningBalance));
}
Implementation of MultiDimensional Arrays
 In Java, multi-dimensional arrays are
implemented using one-dimensional arrays.
 The array table declared as follows:
int [][] table = new int[10][6];
is a one-dimensional array of length 10 whose
elements are one-dimensional arrays of length 6.
 In other words, multi-dimensional arrays are
arrays of arrays.
The Method showTable
Redefined
/**
The array displayArray can have any dimensions.
Postcondition: The array contents are displayed with dollar signs.
*/
public static void showTable(int[][] displayArray)
{
int row, column;
for (row = 0; row < displayArray.length; row++)
{
System.out.print((row + 1) + "
");
for (column = 0; column < displayArray[row].length; column++)
System.out.print("$" + displayArray[row][column] + " ");
System.out.println( );
}
}
Programming Example Using
Multi-Dimensional Arrays
/**
Class for a one-week record of the time worked by
each employee. Uses a five-day week (Mon.-Fri.).
main has a sample application.
*/
public class TimeBook
{
private int numberOfEmployees;
private int[][] hours;
//hours[i][j] has the hours for employee j on day i.
private int[] weekHours;
//weekHours[i] has the week’s hours worked for
//employee i+1.
private int[] dayHours;
//dayHours[i] has the total hours worked by all
//employees on day i. Monday is 0, Tuesday 1, etc.
/**
Reads hours worked for each employee on each day of
the week into the two-dimensional array hours. (The method
for input is just a stub in this preliminary version.)
Computes the total weekly hours for each employee and
the total daily hours for all employees combined.
*/
public static void main(String[] args)
{
TimeBook book = new TimeBook(3);
book.setHours( );
book.update( );
book.showTable( );
}
Programming Example Using
Multi-Dimensional Arrays (cont’d)
public TimeBook(int theNumberOfEmployees)
{
numberOfEmployees = theNumberOfEmployees;
hours = new int[5][numberOfEmployees];
//the 5 is for the 5 days Monday through Friday.
weekHours = new int[numberOfEmployees];
dayHours = new int[5];
}
public void setHours(
{
hours[0][0] = 8;
hours[1][0] = 8;
hours[2][0] = 8;
hours[3][0] = 8;
hours[4][0] = 8;
}
) //This is just a stub.
hours[0][1]
hours[1][1]
hours[2][1]
hours[3][1]
hours[4][1]
public void update( )
{
computeWeekHours( );
computeDayHours( );
}
=
=
=
=
=
0;
0;
8;
8;
8;
hours[0][2]
hours[1][2]
hours[2][2]
hours[3][2]
hours[4][2]
=
=
=
=
=
9;
9;
8;
4;
8;
Programming Example Using
Multi-Dimensional Arrays (cont’d)
private void computeWeekHours( )
{
int dayNumber, employeeNumber, sum;
for (employeeNumber = 1;
employeeNumber <= numberOfEmployees; employeeNumber++)
{//Process one employee:
sum = 0;
for(dayNumber = 0; dayNumber < 5; dayNumber++)
sum = sum + hours[dayNumber][employeeNumber - 1];
//sum contains the sum of all the hours worked
//in one week by employee with number employeeNumber.
weekHours[employeeNumber - 1] = sum;
}
}
private void computeDayHours( )
{
int dayNumber, employeeNumber, sum;
for (dayNumber = 0; dayNumber < 5; dayNumber++)
{//Process one day (for all employees):
sum = 0;
for (employeeNumber = 1;
employeeNumber <= numberOfEmployees;
employeeNumber++)
sum = sum + hours[dayNumber][employeeNumber - 1];
//sum contains the sum of all hours worked by all
//employees on day dayNumber.
dayHours[dayNumber] = sum;
}
}
Programming Example Using
Multi-Dimensional Arrays (cont’d)
public void showTable( )
{
int row, column;
System.out.print("Employee ");
for (column = 0; column < numberOfEmployees; column++)
System.out.print((column + 1) + "
");
System.out.println("Totals");
System.out.println( );
for (row = 0; row < 5; row++)
{
System.out.print(day(row) + " ");
for (column = 0; column < hours[row].length; column++)
System.out.print(hours[row][column] + "
");
System.out.println(dayHours[row]);
}
System.out.println( );
System.out.print("Total = ");
for (column = 0; column < numberOfEmployees; column++)
System.out.print(weekHours[column] + " ");
System.out.println( );
}
Programming Example Using
Multi-Dimensional Arrays (cont’d)
//Converts 0 to "Monday", 1 to "Tuesday" etc.
//Blanks used to make all strings the same length.
private String day(int dayNumber)
{
String dayName = null;
switch (dayNumber)
{
case 0:
dayName = "Monday
";
break;
case 1:
dayName = "Tuesday ";
break;
case 2:
dayName = "Wednesday";
break;
case 3:
dayName = "Thursday ";
break;
case 4:
dayName = "Friday
";
break;
default:
System.out.println("Fatal Error.");
System.exit(0);
break;
}
return dayName;
}
}
Download