SlideShare a Scribd company logo
MULTIDIMENSIONAL
ARRAYS
2
Two-Dimensional Array Basics
 Data in a table or a matrix can be represented using a two-dimensional array
 element in a two-dimensional array is accessed through a row and column
index
 syntax for declaring a two-dimensional array
 elementType[][] arrayRefVar; or elementType arrayRefVar[][];
 int[][] matrix; or int matrix[][];
 matrix = new int[5][5];
3
4
Initialize an array
5
Obtaining the Lengths of Two-
Dimensional Arrays
 A two-dimensional array is actually an array in which each element is a one-
dimensional array
6
Obtaining the Lengths of Two-
Dimensional Arrays (Contd…)
 The length of an array x is the number of elements in the array, which can be
obtained using x.length
 x[0], x[1], . . . , and x[x.length-1]
 x[0].length, x[1].length, . . . , and x[x.length-1].length
7
Ragged Arrays
 Each row in a two-dimensional array is itself an array. Thus, the rows can
have different lengths. An array of this kind is known as a ragged array
8
create a ragged array
 int[][] triangleArray = new int[5][];
triangleArray[0] = new int[5];
triangleArray[1] = new int[4];
triangleArray[2] = new int[3];
triangleArray[3] = new int[2];
triangleArray[4] = new int[1];
9
Processing Two-Dimensional Arrays
 Nested for loops are used to process a two-dimensional array
 Initializing arrays with input values
 java.util.Scanner input = new Scanner(System.in);
System.out.println("Enter " + matrix.length + " rows and " +
matrix[0].length + " columns: ");
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
matrix[row][column] = input.nextInt();
}
}
10
Processing Two-Dimensional Arrays
(Contd…)
 Other Processes
 Initializing arrays with random values
 Printing arrays
 Summing all elements
 Summing elements by column
 Which row has the largest sum?
11
Passing Two-Dimensional Arrays to
Methods
 When passing a two-dimensional array to a method, the reference of the
array is passed to the method
 You can pass a two-dimensional array to a method just as you pass a one-
dimensional array
12
 public class PassTwoDimensionalArray {
public static void main(String[] args) {
int[][] m = getArray(); // Get an array
System.out.println("nSum of all elements is " + sum(m));
}
 public static int[][] getArray() {
Scanner input = new Scanner(System.in);
int[][] m = new int[3][4];
System.out.println("Enter " + m.length + " rows and “ + m[0].length + "
columns: ");
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
m[i][j] = input.nextInt();
return m;
}
13
Multidimensional Arrays
 two-dimensional array consists of an array of one-dimensional arrays
 Three dimensional array consists of an array of two-dimensional arrays
 double[][][] scores = new double[6][5][2];
 Suppose x = new int[2][2][5]
 x[0] and x[1] are two-dimensional arrays
 x[0][0], x[0][1], x[1][0], and x[1][1] are one-dimensional arrays and each contains
five elements
 x.length is 2
 x[0].length and x[1].length are 2
 x[0][0].length, x[0][1].length, x[1][0].length, and x[1][1].length are 5

More Related Content

PPT
Multi dimensional arrays
PPT
L10 array
PPTX
TwoDArrays which can easily help you understand java 2D arrays.pptx
PPTX
Chapter 7.4
PPTX
How to create a two-dimensional array in java
PPTX
Java arrays
PPT
2423846327650397549138593456_2D_Arrays.ppt
PPTX
Lecture 7 arrays
Multi dimensional arrays
L10 array
TwoDArrays which can easily help you understand java 2D arrays.pptx
Chapter 7.4
How to create a two-dimensional array in java
Java arrays
2423846327650397549138593456_2D_Arrays.ppt
Lecture 7 arrays

Similar to Ch 8 Multidimensional Array in oop as per gtu.pptx (20)

PPTX
6_Array.pptx
PDF
Java arrays
PPT
Java™ (OOP) - Chapter 7: "Multidimensional Arrays"
PPTX
Java arrays
PPTX
OOPs with java
PPSX
dizital pods session 6-arrays.ppsx
PPTX
dizital pods session 6-arrays.pptx
PDF
PPTX
PPTX
PPTX
PDF
11 1. multi-dimensional array eng
PPTX
Arrays in Java with example and types of array.pptx
PPTX
jhuigygtftrdtrdersresygyijhvtdghgytfkjhuiyugok
PPT
JavaProgramming 17321_CS202-Chapter8.ppt
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
PPT
05slide_arrays_creation_searching_sorting.ppt
PPT
Two Dimensional Array
PPTX
2D-Array
6_Array.pptx
Java arrays
Java™ (OOP) - Chapter 7: "Multidimensional Arrays"
Java arrays
OOPs with java
dizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.pptx
11 1. multi-dimensional array eng
Arrays in Java with example and types of array.pptx
jhuigygtftrdtrdersresygyijhvtdghgytfkjhuiyugok
JavaProgramming 17321_CS202-Chapter8.ppt
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
05slide_arrays_creation_searching_sorting.ppt
Two Dimensional Array
2D-Array
Ad

More from nilampatoliya (15)

PPTX
Ch2 Elementry Programmin as per gtu oop.pptx
PPTX
ch 7 single dimension array in oop .pptx
PPTX
ch 4 mathematical function character and string.pptx
PDF
Fundamental_Rights__Fundamental_Duties_Revision_no_anno.pdf
PPTX
unit-5 Data Wrandling weightage marks.pptx
PPTX
Software Quality Assurance and Software Quality Standards.pptx
PPTX
UNIT1-Introduction to Software and Software Engineering.pptx
PPT
unit 8 project closure software project management.ppt
PPT
ch24_quality Management in software engineering.ppt
PPTX
Unit - 3 - Non Linear Data Structures.pptx
PPTX
context free grammer ppt of theory of computationoc.pptx
PPTX
Unit 3 Basic of Machine Learning gtu.pptx
PPTX
SE-Unit 2_ Requirement Analysis and Modeling.pptx
PPTX
SE-Unit 4_software testing stretagy.pptx
PPTX
SE-Unit 3_Software Architecture and Design.pptx
Ch2 Elementry Programmin as per gtu oop.pptx
ch 7 single dimension array in oop .pptx
ch 4 mathematical function character and string.pptx
Fundamental_Rights__Fundamental_Duties_Revision_no_anno.pdf
unit-5 Data Wrandling weightage marks.pptx
Software Quality Assurance and Software Quality Standards.pptx
UNIT1-Introduction to Software and Software Engineering.pptx
unit 8 project closure software project management.ppt
ch24_quality Management in software engineering.ppt
Unit - 3 - Non Linear Data Structures.pptx
context free grammer ppt of theory of computationoc.pptx
Unit 3 Basic of Machine Learning gtu.pptx
SE-Unit 2_ Requirement Analysis and Modeling.pptx
SE-Unit 4_software testing stretagy.pptx
SE-Unit 3_Software Architecture and Design.pptx
Ad

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
DOCX
573137875-Attendance-Management-System-original
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Geodesy 1.pptx...............................................
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
PPT on Performance Review to get promotions
PPTX
OOP with Java - Java Introduction (Basics)
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Well-logging-methods_new................
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Construction Project Organization Group 2.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
web development for engineering and engineering
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT
Project quality management in manufacturing
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
UNIT 4 Total Quality Management .pptx
573137875-Attendance-Management-System-original
Mechanical Engineering MATERIALS Selection
Geodesy 1.pptx...............................................
Internet of Things (IOT) - A guide to understanding
PPT on Performance Review to get promotions
OOP with Java - Java Introduction (Basics)
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Well-logging-methods_new................
R24 SURVEYING LAB MANUAL for civil enggi
Construction Project Organization Group 2.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Embodied AI: Ushering in the Next Era of Intelligent Systems
web development for engineering and engineering
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Project quality management in manufacturing
CYBER-CRIMES AND SECURITY A guide to understanding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...

Ch 8 Multidimensional Array in oop as per gtu.pptx

  • 2. 2 Two-Dimensional Array Basics  Data in a table or a matrix can be represented using a two-dimensional array  element in a two-dimensional array is accessed through a row and column index  syntax for declaring a two-dimensional array  elementType[][] arrayRefVar; or elementType arrayRefVar[][];  int[][] matrix; or int matrix[][];  matrix = new int[5][5];
  • 3. 3
  • 5. 5 Obtaining the Lengths of Two- Dimensional Arrays  A two-dimensional array is actually an array in which each element is a one- dimensional array
  • 6. 6 Obtaining the Lengths of Two- Dimensional Arrays (Contd…)  The length of an array x is the number of elements in the array, which can be obtained using x.length  x[0], x[1], . . . , and x[x.length-1]  x[0].length, x[1].length, . . . , and x[x.length-1].length
  • 7. 7 Ragged Arrays  Each row in a two-dimensional array is itself an array. Thus, the rows can have different lengths. An array of this kind is known as a ragged array
  • 8. 8 create a ragged array  int[][] triangleArray = new int[5][]; triangleArray[0] = new int[5]; triangleArray[1] = new int[4]; triangleArray[2] = new int[3]; triangleArray[3] = new int[2]; triangleArray[4] = new int[1];
  • 9. 9 Processing Two-Dimensional Arrays  Nested for loops are used to process a two-dimensional array  Initializing arrays with input values  java.util.Scanner input = new Scanner(System.in); System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns: "); for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { matrix[row][column] = input.nextInt(); } }
  • 10. 10 Processing Two-Dimensional Arrays (Contd…)  Other Processes  Initializing arrays with random values  Printing arrays  Summing all elements  Summing elements by column  Which row has the largest sum?
  • 11. 11 Passing Two-Dimensional Arrays to Methods  When passing a two-dimensional array to a method, the reference of the array is passed to the method  You can pass a two-dimensional array to a method just as you pass a one- dimensional array
  • 12. 12  public class PassTwoDimensionalArray { public static void main(String[] args) { int[][] m = getArray(); // Get an array System.out.println("nSum of all elements is " + sum(m)); }  public static int[][] getArray() { Scanner input = new Scanner(System.in); int[][] m = new int[3][4]; System.out.println("Enter " + m.length + " rows and “ + m[0].length + " columns: "); for (int i = 0; i < m.length; i++) for (int j = 0; j < m[i].length; j++) m[i][j] = input.nextInt(); return m; }
  • 13. 13 Multidimensional Arrays  two-dimensional array consists of an array of one-dimensional arrays  Three dimensional array consists of an array of two-dimensional arrays  double[][][] scores = new double[6][5][2];  Suppose x = new int[2][2][5]  x[0] and x[1] are two-dimensional arrays  x[0][0], x[0][1], x[1][0], and x[1][1] are one-dimensional arrays and each contains five elements  x.length is 2  x[0].length and x[1].length are 2  x[0][0].length, x[0][1].length, x[1][0].length, and x[1][1].length are 5