A two-dimensional array can be used to represent a grid or set of data that are arranged in tabular format (with rows and columns similar to a spreadsheet). Examples of data that can be represented and stored in two dimensional array are checkerboards, streets in a city, seats in theater, a tic-tac-toe game and etc.
variableType[][] variableName; //declaration
variableName = new variableType[numOfRows][numOfColumns]; //instantiation
String[][] chars = new String[5][5]; //2-D array with 5 rows, 5 columns)
The length property can be used to determine the number of rows and columns in a two-dimensional array with two separate statements:
int rows = chars.length;
int cols = chars[0].length
An item in a two-dimensional array can be accessed using the indexes of the row and column in brackets after the array name. The following statement assigns the character "$" in the first row and first column of the array.
chars[0][0] = "$"
Nested for statements are often used to access the items in a two-dimensional array. The outer loops traverses the row and inner loop traverses the column. The following code segment displays the contents of a two-dimensional array:
for(int row = 0; row < chars.length; row++){
for(int col = 0; col < chars[0].length; col++){
System.out.print(chars[row][col]);
}
System.out.println();
}
int[][] numbers = new int[4][4] //instantiates a 2Array
int[][] nums; //declares a 2D rray
nums = new int[3][3] //instantiates with 3 rows & 3 columns
int[][] someNumbers = {{10, 40},{20, 22},{32, 43}}; //initializes
public class TwoDArrayInteger {
public static void main(String[] args){
Random r = new Random();
Scanner input = new Scanner(System.in);
int[][] board = new int[5][5];
/*
* Assigns random integers (0-100) to board
*
*/
for(int i = 0; i < board.length; i++ ){
for(int j = 0; j < board[i].length; j++){
board[i][j] = r.nextInt(100);
}
}
/*
* Prints values of board[][] array
*
for(int i = 0; i < board.length; i++ ){
for(int j = 0; j < board[i].length; j++){
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
}
import java.util.Scanner;
/**
*
* @author maanderson
*/
public class TwoDArrayForDiscussion {
static int[][] numbers = {{1,3,5},{3,2,1},{1,2,5},{4,7,8}};
/*
* Prints all elements of numbers array
*/
public static void printNumbers(){
for(int r = 0; r < numbers.length; r++){
for(int c = 0; c < numbers[r].length; c++){
System.out.print(numbers[r][c] + "\t");
}
System.out.println();
}
}
/*
* Counts the number of even numbers per row
* and displays the number of even numbers per actual row number
*/
public static void countEvensPerRow(){
int count = 0;
for(int r = 0; r < numbers.length; r++){
for(int c = 0; c < numbers[r].length; c++){
if(numbers[r][c] % 2 == 0){
count++;
}
}
System.out.println("Row " + (r+1) + " has " + count + " even numbers.");
count = 0;
}
}
/*
* Displays the sums of all the rows in numbers array
*/
public static void sumOfRows(){
int sum = 0;
for(int r = 0; r < numbers.length; r++){
for(int c = 0; c < numbers[r].length; c++){
sum += numbers[r][c];
}
System.out.println("Sum of row " + (r+1) + " is " + sum);
sum = 0;
}
}
/*
* Calculates the sum of integers at specified row index number
* @param: row index number
* @return: sum of the elements at row index number
* pre: rowIndexNum > -1 and rowIndexNum < the array length
* post: sum of numbers at specified row index number
*
*/
public static int getSumOfRow(int rowIndexNum){
int sum = 0;
for(int r = 0; r < numbers.length; r++){
for(int c = 0; c < numbers[r].length; c++){
if(rowNumber == r){
sum += numbers[r][c];
}
}
}
return sum;
}
/*
* Main method to test all methods of the class
*/
public static void main(String[] args) {
printNumbers();
System.out.println("");
countEvensPerRow();
System.out.println(""); //blank line
sumOfRows();
System.out.println("");
int rowIndex = 2;
System.out.print("Sum of row " + (rowIndex+1) + " is:" + getSumOfRow(rowIndex));
}
}
1 3 5
3 2 1
1 2 5
4 7 8
Row 1 has 0 even numbers.
Row 2 has 1 even numbers.
Row 3 has 1 even numbers.
Row 4 has 2 even numbers.
Sum of row 1 is 9
Sum of row 2 is 6
Sum of row 3 is 8
Sum of row 4 is 19
Sum of row 3 is:8
BUILD SUCCESSFUL (total time: 0 seconds)