Explain declaring- allocating and initializing two dimension, JAVA Programming

Assignment Help:

Explain the following terms declaring, Allocating and initializing two dimensional array?

Two dimensional arrays are declared, allocated and initialized much such as one dimensional arrays. Therefore you have to specify two dimensions rather than one, and you classically use two nested for loops to fill the array.

This example fills a two-dimensional array along with the sum of the row and column indexes

class FillArray {
   public static void main (String args[]) {
      int[][] matrix;
    matrix = new int[4][5];
      for (int row=0; row < 4; row++) {
      for (int col=0; col < 5; col++) {
        matrix[row][col] = row+col;
      }
    }
      }
  }

Of course the algorithm you use to fill the array depends fully on the use to that the array is to be put. The further example calculates the identity matrix for a given dimension. The identity matrix of dimension N is a square matrix that contains ones along the diagonal and zeros in all other positions.

class IDMatrix {
   public static void main (String args[]) {
      double[][] id;
    id = new double[4][4];
      for (int row=0; row < 4; row++) {
      for (int col=0; col < 4; col++) {
        if (row != col) {
          id[row][col]=0.0;
        }
        else {
          id[row][col] = 1.0;
        }
      }
    }
      }
  }

In two-dimensional arrays ArrayIndexOutOfBoundsExceptions occur while you exceed the maximum column index or row index.

You can also allocate, declare, and initialize a a two-dimensional array at the similar time through providing a list of the initial values inside nested brackets. For example the three by three identity matrix could be set up like this:

double[][] ID3 = {
  {1.0, 0.0, 0.0},
  {0.0, 1.0, 0.0},
  {0.0, 0.0, 1.0}
};

The spacing and the line breaks used above are purely for the programmer. The compiler doesn't care. The subsequent works equally well:

double[][] ID3 = {{1.0, 0.0, 0.0},{0.0, 1.0, 0.0},{0.0, 0.0, 1.0}};


Related Discussions:- Explain declaring- allocating and initializing two dimension

Minimum spanning tree based heuristic, Problem description: A travelling s...

Problem description: A travelling salesman wants to make a tour of the cities and returns back to the starting point. What is the minimum length tour? Formal Definiti

What do you understand by private and public class, What do you understand ...

What do you understand by private, protected and public? These are accessibility modifiers. Private is the most restrictive, whereas public is the least restrictive. There is n

What is difference between design and system architecture, What is differen...

What is difference between Design and System Architecture? System architecture is the conceptual design which defines the structure and/or behavior of a system. Whereas designs

Programming Project, Design and implement a class called Sphere that contai...

Design and implement a class called Sphere that contains instance data that represents the sphere''s diameter, and include getter and setter methods for the diameter. Include metho

Difference between bean factory and application context, On the surface, an...

On the surface, an application context is similar as a bean factory. But application context offers much more.. ? Application contexts give a means for resolving text messages,

Explain the remainder or modulus operator in java, Explain the Remainder or...

Explain the Remainder or Modulus Operator in Java? Java has one significant arithmetical operator you might not be familiar with, %, also called as the modulus or remainder ope

Describe overriding methods, Describe Overriding Methods ? Assume that...

Describe Overriding Methods ? Assume that one day you've just finished your Car class. It's been plugged into your traffic simulation that is chugging along merrily simulating

Write a program using local variable, Write a program using local variable ...

Write a program using local variable Public void someMethod( ) { int x; //local variable System.out.println(x); // compile time error } - Constructor - Doesn't

What is the purpose to use the enableevents method, What is the purpose to ...

What is the purpose to use the enableEvents() method?

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd