Question: A two dimensional array is an array of array references: int[][] twoD = new int[2][3]; creates a two dimensional array with 2 rows and 3
A two dimensional array is an array of array references:
int[][] twoD = new int[2][3]; creates a two dimensional array with 2 rows and 3 columns:
This is twoD[0]
Im twoD[0][0]
Im twoD [0][1]
Im twoD [0][2]
This is twoD[1]
Im twoD [1][0]
Im twoD [1][1]
Im twoD [1][2]
Two dimensional arrays can be traversed in two ways: standard array traversal or for each traversal:
Standard:
for (int i = 0; i < twoD.length; i++)
{
for (int j = 0; j < twoD[i].length; j++)
{
System.out.print( + twoD[i][j]);
}
System.out.println(); // New line...
}
For each:
for (int[] row : twoD)
{
for (int value : row)
{
System
.out.print( + value);
}
System.out.println(); // New line...
}
The purpose of this lab is to give you practice in handling two dimensional arrays.
Create a Java program according to the following specifications.
.
In main method:
o Prompt the user for the dimensions of the array and accept their input
o Create a twoD int array reference named twoD
o Call the create2D method to create the actual 2D array instance
o Call the init2d method to initialize the array
o Print the array.
o Call the double2D method to double the number of rows in your array.
oPrint the array again.
In create2D method:
Code a method (create2D) that accepts two integers and returns an actual two d array
In init2d method:
Create a method (init2D) that will fill the array elements with values (use row + column). This method get a reference to the array as an incoming parm.
In double 2d method:
Create a method (double2d) that will accept a reference to a twoD, create an array that is twice the number of rows of the passed in array, copy the values in the passed in array to the new array, and return the new array In printRow method:
Print the elements in one row of the array.
In printArray method:
Call printRow, once for each row in the 2D.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
