Title: Multi-Dimensional Arrays
1Multi-Dimensional Arrays
2A Table of Values
Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts) Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts) Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts) 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
3Row and Column Indices for an Array
Indices 0 1 2
0 1050 1055 1060
1 1103 1113 1124
2 1158 1174 1191
4Multi-Dimensional Array Basics
- We declare a variable, table, to be a
two-dimensional array as follows - int table new int 106
- 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.
5Multi-Dimensional Array Basics (contd)
- 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.
6Example of Using a Two-Dimensional 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 int106 int row,
column for (row 0 row lt 10 row)
for (column 0 column lt 6 column)
tablerowcolumn
balance(1000.00, row 1, (5 0.5column))
System.out.println("Balances for Various
Interest Rates") System.out.println("Compou
nded Annually") System.out.println("(Rounde
d 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 lt 10 row)
System.out.print((row 1) " ")
for (column 0 column lt 6 column)
System.out.print("" tablerowcolumn "
") System.out.println( )
7Example of Using a Two-Dimensional Array (contd)
- / 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 lt years
count) runningBalance
runningBalance(1 rate/100) return (int)
(Math.round(runningBalance)) -
8Multi-Dimensional Array Parameters and Returned
Values
- Methods may have multi-dimensional array
parameters and may return a multi-dimensional
array. - The situation is similar to that of the
one-dimensional case, except that you use more
square brackets.
9Example Using a Multi-Dimensional 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 int106 int row,
column for (row 0 row lt 10 row)
for (column 0 column lt 6 column)
tablerowcolumn
balance(1000.00, row 1, (5 0.5column))
System.out.println("Balances for Various
Interest Rates") System.out.println("Compou
nded Annually") System.out.println("(Rounde
d 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)
10Example Using a Multi-Dimensional Array Parameter
(contd)
- / 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 lt 10
row) System.out.print((row
1) " ") for (column 0 column
lt 6 column) System.out.print(""
displayArrayrowcolumn " ")
System.out.println( ) public
static int balance(double startBalance, int
years, double rate) double
runningBalance startBalance int count
for (count 1 count lt years count)
runningBalance runningBalance(1
rate/100) return (int) (Math.round(runningB
alance))
11Implementation of Multi-Dimensional Arrays
- In Java, multi-dimensional arrays are implemented
using one-dimensional arrays. - The array table declared as follows
- int table new int106
- 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.
12The 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 lt
displayArray.length row)
System.out.print((row 1) " ") for
(column 0 column lt displayArrayrow.length
column) System.out.print(""
displayArrayrowcolumn " ")
System.out.println( )
13Programming 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 //hoursij has the
hours for employee j on day i. private int
weekHours //weekHoursi has the weeks
hours worked for //employee i1.
private int dayHours //dayHoursi
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( )
14Programming Example Using Multi-Dimensional
Arrays (contd)
- public TimeBook(int theNumberOfEmployees)
numberOfEmployees theNumberOfEmployees
hours new int5numberOfEmployees
//the 5 is for the 5 days Monday through
Friday. weekHours new
intnumberOfEmployees dayHours new
int5 public void setHours( ) //This
is just a stub. hours00 8
hours01 0 hours02 9
hours10 8 hours11 0 hours12
9 hours20 8 hours21 8
hours22 8 hours30 8
hours31 8 hours32 4
hours40 8 hours41 8 hours42
8 public void update( )
computeWeekHours( ) computeDayHours(
)
15Programming Example Using Multi-Dimensional
Arrays (contd)
- private void computeWeekHours( )
int dayNumber, employeeNumber, sum for
(employeeNumber 1 employeeNumber
lt numberOfEmployees employeeNumber)
//Process one employee sum 0
for(dayNumber 0 dayNumber lt 5
dayNumber) sum sum
hoursdayNumberemployeeNumber - 1
//sum contains the sum of all the hours
worked //in one week by employee
with number employeeNumber.
weekHoursemployeeNumber - 1 sum
private void computeDayHours( )
int dayNumber, employeeNumber, sum
for (dayNumber 0 dayNumber lt 5
dayNumber) //Process one day (for all
employees) sum 0 for
(employeeNumber 1
employeeNumber lt numberOfEmployees
employeeNumber)
sum sum hoursdayNumberemployee
Number - 1 //sum contains the
sum of all hours worked by all
//employees on day dayNumber.
dayHoursdayNumber sum
16Programming Example Using Multi-Dimensional
Arrays (contd)
- public void showTable( ) int row,
column System.out.print("Employee ")
for (column 0 column lt
numberOfEmployees column)
System.out.print((column 1) " ")
System.out.println("Totals")
System.out.println( ) for (row 0 row
lt 5 row)
System.out.print(day(row) " ")
for (column 0 column lt hoursrow.length
column) System.out.print(hoursr
owcolumn " ")
System.out.println(dayHoursrow)
System.out.println( )
System.out.print("Total ") for
(column 0 column lt numberOfEmployees
column) System.out.print(weekHoursc
olumn " ") System.out.println( )
17Programming Example Using Multi-Dimensional
Arrays (contd)
- //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