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

I want linux server expert with tomcat skills, I want Linux server expert w...

I want Linux server expert with Tomcat skills Project Description: I want a Linux server expert to look in to an issue with my domain. It is down since three days as there is

Write javascript code to convert the number, Write JavaScript code to conve...

Write JavaScript code to convert the number 236.2363611111556 into currency format and JavaScript statement to show output? Var input_amt = 236.2363611111556 ; mid_data = input

What are the design goals of java, What are the Design Goals of Java Ma...

What are the Design Goals of Java Massive growth of the Internet and World-Wide Web leads us to a completely new way of looking at development of software which can run on diff

Outbound submissions and tracking, Outbound Submissions and Tracking: ...

Outbound Submissions and Tracking: Project Overview: In the current ARISg environment, expedited reports are qualified and distributed electronically to contacts maintaine

We require expert programmers who will solve problems, We require expert pr...

We require expert programmers who will solve problems, who can code by the book, and who will code in java and JavaScript what is designed. Skills required: CSS, Java, HTML5,

What should be public and private, What should be public? What should be pr...

What should be public? What should be private? As a rule of thumb: Classes are public. Fields are private. Constructors are public. Getter and setter methods

Develop a supply chain management, Develop a Supply Chain Management Pro...

Develop a Supply Chain Management Project Description: Supply Chain Management from HK and China Skills required is Java

I want fitlife app for android - ios, I want FitLIFE app for Android, IOS, ...

I want FitLIFE app for Android, IOS, Windows Project Description: Hello, I want to develop an app for Android, IOS and Windows Phone. This app will be work with open source B

I want hall rental website, I want Hall Rental Website Project Descripti...

I want Hall Rental Website Project Description: I want to prepare website for my party venue where i will post the photos and detail of place along with availability calendar

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