Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 2

Multidimensional arrays

One-dimensional arrays are good at storing linear collections of elements. Two-dimensional arrays can be used to store a matrix or a table.

The syntax for declaring a two-dimensional array is:

 data_type[][] arrayName;
 data_type arrayName[][];

An example of a two-dimensional array of 5-by-6 int values and assign to variable myArray is:

 int[][] myArray = new int[5][6]; 

To assign the value 10 to a specific element at row 2 and column 3, use:

 myArray[2][3] = 10;

To use an array initializer to declare, create and initialize a two-dimensional array, use:

 int[][] myArray = {
                     {1,2,3},
                     {4,5,6},
                     {7,8,9} 
 };

To get the length of each element, we can write:

 for (int i = 0; i<myArray.length; i++){
      int len = myArray[i].length;
      System.out.println(len);
 }

Analogically, it is possible to create multidimensional arrays.

Session 2
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment