Java Programming

 

         

 

ArrayPractice2

 


public class ArrayPractice2 {

/**
* A two dimensional array is an array within an array.
* Slowly read through the examples provided here and use
* this file when creating your Matrix problem.
*/
public static void main(String[] args) {
//Declaring a two dimensional Array[][]
// Anytime you see two sets of brackets you know you are
// dealing with a two dimensional array. It is useful to think
// of a two dimensional array as a set of rows and columns, much like a grid

//Here is how you declare a two dimensional array
String[][] teamMembers = new String[3][4]; // three rows and four columns
//OR
String teamMembers2[][] = new String[5][6];// five rows six columns
//OR
// Write the code to create a two dimensional array of integers that has two rows and two columns

//Initializing an Array
// Declare and initialize in the same statement
// This code creates an array of three rows and three columns
int[][] scores = {{1, 2, 3},{4, 5, 6}, {7, 8, 9}}; // three rows and three columns
// array size determined by the number of values provided.
// This method is used when you already know the contents of the array.
// This array would look like this
// 1 2 3
// 4 5 6
// 7 8 9
//
// To change the value of one of the values in a 2D array
scores[1][1] = 0; // which value in the above 2D array will be replaced with 0?
// Remember that arrays start with an idex value of 0
// Here are the index values of each cell in the scores array
// 1 2 3 here are the index values for these rows - 0,0 0,1 0,2
// 4 5 6 1,0 1,1 1,2
// 7 8 9 2,0 2,1 2,2
// So which value in the above 2D array will be replaced with 0?
// 1 2 3
// 4 0 6 - the 5 was replaced with 0 since it was found at index [1][1]
// 7 8 9
// What index value would you use if you wanted to replace the 9 with 5?

//How to iterate through a 2D Array
// To iterate through a single array we used a for loop
// To iterate through a 2D array we used a nested loop structure
// Normally the counter would be i and j in nested loops but it may be
// more helpful to use row and column as the counters so it is easier to visualize
// what is happening.
for (int row = 0; row < scores.length; row++) // use row as the counter here
{
System.out.print("Row " + row + ": ");
// Now comment out the above line of code to see what the output looks like then.
// use column as the inner counter
// notice how you must use scores[row].length to find the length of each row.
for (int column = 0; column < scores[row].length; column++)
System.out.print(scores[row][column]);
System.out.println(""); // this line is needed to force the output to the next line
}

// Write the code to change the first value in the last row to 5 and then run the code to
// confirm that the change was made.
// Now change the zero in row two back to five.

//printAnyArray(scores); uncomment out this line once you create the printAnyArray( ) at the end

// What happens if you replace < with <= ???? This is a very common error with arrays.

}// end of main method

// make a method that will print out any 2D array of integers - the parameter is a 2D array of integers
public static void printAnyArray(int[][] array)
{
// fill in the code to print up a 2D array using the input "array" provided by the parameter
{

}

}// end of printAnyArray( ) method

}