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

Explain overriding methods and the solution, Explain Overriding Methods: Th...

Explain Overriding Methods: The Solution The object oriented solution to this problem is to describe a new class, call it SlowCar, that inherits from Car and imposes the additi

Overloaded Methods, QUESTION 3: Overloaded methods Write the overloaded me...

QUESTION 3: Overloaded methods Write the overloaded method named average () for each of the following problems: a) The first method receives THREE (3) integer values and returns

What is preferred organizational structure for organization, What is the pr...

What is the preferred organizational structure for the organization? The Network Organization is becoming the preferred organizational structure.

What are the various struts tag libraries, The Struts tag libraries are: ...

The Struts tag libraries are: ? HTML Tags ? Logic Tags ? Template Tags ? Bean Tags ? Tiles Tags ? Nested Tags

Make changes to an editable pdf form, Make Changes to an Editable PDF Form ...

Make Changes to an Editable PDF Form Project Description: I need for someone who is able to make some changes to PDF forms. Skills required: Data Processing, Data Entry

Elaborate with examples vector or object oriented graphics, Elaborate with ...

Elaborate with examples Vector or Object oriented graphics? Vector or Object-Oriented Graphics treats everything which is drawn as an object. Objects retain their identity afte

Methods in java, public class Foothill {    public static void main(String[...

public class Foothill {    public static void main(String[] args)    {       Client person_1 = new Client("Bruce Ruprecht", 16000, 10);       Writer writer_1 = new Writer("Adam Buf

Write a program to perform multiplication and division, Write a program to ...

Write a program to perform multiplication and division in java? Of course Java could also do multiplication and division. Because most keyboards don't have the times and divisi

Need support display tiff in internet explorer, Need support Display TIFF i...

Need support Display TIFF in Internet Explorer without ActiveX plugin I would like to get a client side viewer designed that permits user to view TIFF files on IE 8 and IE 9 bro

Object diagrams, Object diagrams define instances instead of classes. They ...

Object diagrams define instances instead of classes. They are useful for illustrating some complicated objects in detail about their recursive relationships.     When

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