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
 
 
Hot Web Programming Job

 

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

[an error occurred while processing this directive]

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);
        }
    }
}

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

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

Current Comments

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

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

      View This Comment Separately
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
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.