0% found this document useful (0 votes)
34 views

Two Dimensional Arrays and Arraylist

The document discusses two-dimensional arrays, including how to declare, initialize, access elements of, and perform operations like summing on 2D arrays. It also covers topics like using the length field to determine rows and columns, passing 2D arrays as arguments to methods, and how 2D arrays are arrays of one-dimensional arrays.

Uploaded by

Kabir Luhana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Two Dimensional Arrays and Arraylist

The document discusses two-dimensional arrays, including how to declare, initialize, access elements of, and perform operations like summing on 2D arrays. It also covers topics like using the length field to determine rows and columns, passing 2D arrays as arguments to methods, and how 2D arrays are arrays of one-dimensional arrays.

Uploaded by

Kabir Luhana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Two Dimensional Arrays and

ArrayList
Lecture 3
Two-Dimensional Arrays
• Declaring a two-dimensional array requires two sets of
brackets and two size declarators
– The first one is for the number of rows
– The second one is for the number of columns.
double[][] scores = new double[3][4];

two dimensional array rows columns

• The two sets of brackets in the data type indicate that the
scores variable will reference a two-dimensional array.
• Notice that each size declarator is enclosed in its own set of
brackets.
Accessing Two-Dimensional Array Elements

• When processing the data in a two-


dimensional array, each element has two
subscripts:
– one for its row and
– another for its column.
Accessing Two-Dimensional Array Elements

The scores variable


holds the address of a
2D array of doubles.
column 0 column 1 column 2 column 3
Address
row 0 scores[0][0] scores[0][1] scores[0][2] scores[0][3]
row 1 scores[1][0] scores[1][1] scores[1][2] scores[1][3]
row 2 scores[2][0] scores[2][1] scores[2][2] scores[2][3]
Accessing Two-Dimensional Array Elements

Accessing one of the elements in a two-dimensional


array requires the use of both subscripts.
The scores variable scores[2][1] = 95;
holds the address of a
2D array of doubles.
column 0 column 1 column 2 column 3
Address
row 0 0 0 0 0
row 1 0 0 0 0
row 2 0 95 0 0
Accessing Two-Dimensional Array Elements
• Programs that process two-dimensional
arrays can do so with nested loops.
Number of rows, not the
• To fill the scores array: largest subscript
for (int row = 0; row < 3; row++)
Number of
{ columns, not the
for (int col = 0; col < 4; col++) largest subscript
{
System.out.print("Enter a score: ");
scores[row][col] = sc.nextDouble();
}
} keyboard references a
Scanner object
Accessing Two-Dimensional Array Elements

• To print out the scores array:


for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
System.out.println(scores[row][col]);
}
}
Initializing a Two-Dimensional Array
• Initializing a two-dimensional array requires
enclosing each row’s initialization list in its own set of
braces.
int[][] numbers = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
• Java automatically creates the array and fills its
elements with the initialization values.
– row 0 {1, 2, 3}
– row 1 {4, 5, 6}
– row 2 {7, 8, 9}
• Declares an array with three rows and three columns.
Initializing a Two-Dimensional Array
int[][] numbers = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};

The numbers variable produces:


holds the address of a
2D array of int values.
column 0 column 1 column 2
Address
row 0 1 2 3
row 1 4 5 6
row 2 7 8 9
The length Field
• Two-dimensional arrays are arrays of one-
dimensional arrays.
• The length field of the array gives the number
of rows in the array.
• Each row has a length constant tells how many
columns is in that row.
• Each row can have a different number of
columns.
The length Field
• To access the length fields of the array:
int[][] numbers = { { 1, 2, 3, 4 },
{ 5, 6, 7 },
{ 9, 10, 11, 12 } };

for (int row = 0; row < numbers.length; row++)


{
for (int col = 0; col < numbers[row].length; col++)
System.out.println(numbers[row][col]);
}

Number of rows Number of columns in this row.

The array can have variable length rows.


Summing The Elements of a Two-
Dimensional Array
int[][] numbers = { { 1, 2, 3, 4 },
{5, 6, 7, 8},
{9, 10, 11, 12} };
int total;
total = 0;
for (int row = 0; row < numbers.length; row++)
{
for (int col = 0; col < numbers[row].length; col++)
total += numbers[row][col];
}

System.out.println("The total is " + total);


Summing The Rows of a Two-Dimensional
Array
int[][] numbers = {{ 1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int total;

for (int row = 0; row < numbers.length; row++)


{
total = 0;
for (int col = 0; col < numbers[row].length; col++)
total += numbers[row][col];
System.out.println("Total of row "
+ row + " is " + total);
}
Summing The Columns of a Two-Dimensional
Array
int[][] numbers = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int total;

for (int col = 0; col < numbers[0].length; col++)


{
total = 0;
for (int row = 0; row < numbers.length; row++)
total += numbers[row][col];
System.out.println("Total of column "
+ col + " is " + total);
}
Passing and Returning Two-Dimensional
Array References
• There is no difference between passing a single or two-
dimensional array as an argument to a method.
• The method must accept a two-dimensional array as a parameter.
• Public void Method1(int[][] n1)
• {

• }
• Public static void main(String[] args)
• {
• Method1(numbers);
• }

You might also like