Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Casting
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Operators
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays
Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance
Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Polymorphism
Introduction
Interfaces
Packages
Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions


Multi Dimensional Arrays

We have seen that an array can hold references to objects and an array itself is a reference type so an array of arrays seems quite plausible. These are referred to as multidimensional arrays. We can arrays of two, three or more dimensions. We will look at multi dimensional arrays, with special focus on two dimensional arrays which are quite frequently used. They are generally used to represent matrices. But unlike a mathematical matrix, there are a few differences which we will see now.

We have represented an array type using a pair of brackets. Two dimensional arrays are in a similar way represented by two such pairs of brackets and an N dimensional array is represented by using N such pairs of brackets. The following statement creates a two dimensional array of integers, which contains 3 arrays containing 4 integers each.

int[][] a=new int[3][4];

We can also think of this array as a 3*4 matrix in mathematics, with each row of the matrix being a Java array. You might also think of it in the other way, also as a 4*3 matrix, but we generally use the first notation.

Elements of this array are accessed by specifying the index numbers, here two of them. The first representing the array number and the second representing the index element in that particular array.

a[0][2] = 34;

A two dimensional airy can also be initialised using an array initialiser in the following way. This paints a better picture of a 2D array as an array of arrays.

int[][] d = { { 1,5,74,2}, {4,68,45,65},{5,0,34,54}}:

The above array a[][] contains three arrays { 1,5,74,2}, {4,68,45,65} and {5,0,34,54}. This particular 2D array contains equal number of elements in each of its sub arrays. We can, if we wish to, create 2D arrays which have different number of elements in its sub arrays.

int[][] c = { { 4,56,7}, {1}, {45,78} };

The above two dimensional array contains three arrays. The first of these has three elements, the second has a single element and the last of these has two elements. We can also create such non symmetric arrays using the new keyword in the following way:

int[][] b = new int[3][];
b[0]=new int[3];
b[1]=new int[1];
b[2]=new int[2];

The above three sets of statements also creates an array space b identical to that of a created earlier using an array initializer. Observe that in the first statement, we haven't specified any integer in the second pair of square brackets following [3]. b[][] is reference type and so are b[0], b[1] and b[2]. However, b[0][0], b[0][1] .... are of primitive type int. These things are to be dealt with carefully when we pass an array, a sub array or an element of the array as parameters.

The length of a two dimensional or a multi-dimensional array gives the number of arrays it contains. For example: b.length gives 3 while b[0].length gives 3, b[1].length gives 1 and so on. All these facts are easy to assimilate if we consider two dimensional arrays to be an array of arrays rather than as a mathematical matrix.

We manipulate multi-dimensional arrays using nested loops. For example, the following code snippet is used to print the elements of an array as a matrix. The code works even when the rows of the array are of different lengths.

for ( int i=0; i<a.length; i++) {
    for(int j=0; j< a[i].length; j++) 
        System.out.print ( a[i][j] );
        System.out.println();
}

Searching a two dimensional array is performed using a linear search. The labelled break statement is used to break out of the loop once the element is found.

outer:
for ( int i=0; i<a.length; i++) {
    for(int j=0; j< a[i].length; j++) {
        if ( a[i][j] == target) {
            System.out.println("Element found at ["+i+"]["+j+"]");
            break outer;
        }
    }
}

Sorting isn't generally performed on multidimensional arrays since we generally use them to represent a physical situation rather than as data structures to hold information. For example, we may use a three dimensional array to hold Student objects in a more realistic way. We have different cities and each city has different schools which has students. Let each city, school and Student have a code. A Student object in that case can be accessed more conveniently as s[city code][school code][student code]. This 3D representation gives a more realistic picture than a 1D array of Students.

Privacy Policy