Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes

Arrays -- Multi-dimensional

[an error occurred while processing this directive]

All arrays in Java are really linear, one-dimensional arrays. However, you can easily build multi-dimensional arrays from these, and Java has features in the language to help you do this.

These examples all use two-dimensional arrays, but the same syntax and coding can easily be extended to arrays of any dimension. By convention two dimensional arrays have rows (horizontal) and columns (vertical). The first subscript selects the row (which is a one-dimensional array itself), and the second subscript selects the element in that row/array.

Visualizing two-dimensional arrays

Assume we have an array, a, with three rows and four columns.

a[0][0]a[0][1]a[0][2]a[0][3]
a[1][0]a[1][1]a[1][2]a[1][3]
a[2][0]a[2][1]a[2][2]a[2][3]
Two-dimensional arrays are usually visualized as a matrix, with rows and columns. This diagram shows the array a with its corresponding subscripts.
    +-----+
    |     |    +-----+-----+-----+-----+
    |a[0] | -> | [0] | [1] | [2] | [3] |
    |     |    +-----+-----+-----+-----+
    +-----+
    |     |    +-----+-----+-----+-----+
    |a[1] | -> | [0] | [1] | [2] | [3] |
    |     |    +-----+-----+-----+-----+
    +-----+
    |     |    +-----+-----+-----+-----+
    |a[2] | -> | [0] | [1] | [2] | [3] |
    |     |    +-----+-----+-----+-----+
    +-----+
In Java two-dimensional arrays are implemented is a one-dimensional array of one-dimensional arrays -- like this.

Declaring and Allocating a two-dimensional array

Let's declare a board for playing the game of tic-tac-toe. It will have three rows (the first subscript) and three columns (the second subscript) and contain an int in each element.

int[][] board = new int[3][3];

Initial values

It's possible to assign initial values to an array when you declare it in a manner very similar to one-dimensional arrays, but with an extra level of braces. The dimension sizes are computed by the compiler from the number of values.

int[][] board = new int[][] {{0,0,0},{0,0,0},{0,0,0}};

You must assign values to an element before you use it, either with an initializer as above or assignment.

Example -- drawing the tic-tac-toe board

It's often easiest to use two-dimensional arrays with nested for loops. For example, the following code draws the tic-tac-toe board in a paint method. It assumes that a cell is 10 pixels on a side, and that a positive number represents an X and a negative number represents a O.

for (int row=0; row<3; row++) {
    for (int col=0; col<3; col++) {
        if (board[row][col] > 0) { // draw X
            g.drawLine(col*10, row*10  , col*10+8, row*10+8);
            g.drawLine(col*10, row*10+8, col*10+8, row*10  );
        } else if (board[row][col] < 0) { // draw O
            g.drawOval(col*10, row*10, 8, 8);
        }
    }
}
Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (post your own) View All Comments Latest 10 Comments:

how to change or replace values of atwo dimensional array

Posted by gnanaprakashgopi on Thursday, 01.21.10 @ 16:25pm | #94209

you should have placed other examples for this one.

Posted by jigz on Tuesday, 11.10.09 @ 10:54am | #92461

I want to know, how to get the output in matrix format.
First, i'm giving input in normal ways.
second, i'm doing some calculation. Then storing the values in particular variables.
All the results (which i have been found ) should come in matrix format

Posted by magesh on Sunday, 03.15.09 @ 03:57am | #85853

tic tac toe game of 100 boxes in java language. consequtive 0's or *'s will make the person win.

Posted by sunil on Tuesday, 07.31.07 @ 01:30am | #22249

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]