Two-Dimensional Arrays


Java treats two-dimensional arrays as nested one-dimensional arrays. Each row of a 2-D array is a 1-D array. There are two acceptable syntaxes for creating a 2-D array. You can place two sets of square brackets either after the variable type or after the variable name .

 variable_type array_name[][] =   new  variable_type[num_rows][num_columns];   variable_type[][] array_name = new  variable_type[num_rows][num_columns]; 

You can create a 2-D array with a variable number of columns for each row by omitting the num_columns argument. In this case you have to specify the number of columns for each row.

 variable_type[][] array_name =                   new variable_type[num_rows][]; array_name[0] = new variable_type[num_columns0]; array_name[1] = new variable_type[num_columns1]; ... array_name[num_rows-1] =                 new variable_type[num_columnsX]; 

Example: A 2-D Numerical Grid

Finite-difference or finite-element techniques are often used to solve complex physical problems. The domain of the problem is divided into discrete units called cells and the governing equations for whatever physical model is being solved are applied to each cell . A collection of cells is called a grid. For a 2-D analysis, the x - and y -coordinates of each cell vertex are usually stored in 2-D arrays.

The CircCylGrid class creates a 90-degree, equispaced, circular grid with an inner radius of 1.0. This is a grid that you might use if you were performing an analysis on a 2-D circular cylinder configuration. The CircCylGrid class defines two 2-D arrays of double values representing the x - and y -values of each point of the grid. The size of the arrays is set according to integer values passed to the constructor. The generateGrid() method assigns values to the elements of the array. The writeLine() method writes the x - and y -values along one radial line of the grid.

 public class CircCylGrid {   private double x[][];   private double y[][];   private double angle, R;   private int idim, jdim;   public CircCylGrid(int idim, int jdim) {      this.idim = idim;      this.jdim = jdim;      x = new double[idim][jdim];      y = new double[idim][jdim];      generateGrid();   }   //  The grid points are computed and stored in   //  the 2-D arrays.  The grid is an equispaced   //  90 degree circular grid of inner radius 1.0   private void generateGrid() {     for(int i=0; i<idim; ++i) {       angle = 0.5*Math.PI*i/(idim-1);       for(int j=0; j<x[0].length; ++j) {         R = 1.0 + 1.0*j/(jdim-1);         x[i][j] = R*Math.cos(angle);         y[i][j] = R*Math.sin(angle);       }     }   }   //  This method writes the x and y values along   //  one radial line of the grid.   public void writeLine(int i) {     System.out.println("i="+i);     for(int j=0; j<jdim; ++j) {       System.out.println("j=" + j +                " x=" + x[i][j] + " y=" + y[i][j]);     }   } } 

The TwoDimDemo class is a driver program that creates a CircCylGrid object and writes out the x - and y -values along the 0 and 45 degree radial lines of the grid.

 public class TwoDimDemo {   public static void main(String args[]) {     CircCylGrid grid = new CircCylGrid(11,11);     //  Write out the x- and y-values along the 0     //  and 45 degree radial lines of the grid     grid.writeLine(0);     System.out.println();     grid.writeLine(5);   } } 

Output ”

 i=0 j=0   x=1.0  y=0.0 j=1   x=1.1  y=0.0 j=2   x=1.2  y=0.0 j=3   x=1.3  y=0.0 j=4   x=1.4  y=0.0 j=5   x=1.5  y=0.0 j=6   x=1.6  y=0.0 j=7   x=1.7  y=0.0 j=8   x=1.8  y=0.0 j=9   x=1.9  y=0.0 j=10  x=2.0  y=0.0 i=5 j=0   x=0.707106  y=0.707106 j=1   x=0.777817  y=0.777817 j=2   x=0.848528  y=0.848528 j=3   x=0.919238  y=0.919238 j=4   x=0.989949  y=0.989949 j=5   x=1.060660  y=1.060660 j=6   x=1.131370  y=1.131370 j=7   x=1.202081  y=1.202081 j=8   x=1.272792  y=1.272792 j=9   x=1.343502  y=1.343502 j=10  x=1.414213  y=1.414213 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net