Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Two-dimensional arrays 
 

In this section, you will learn about two-dimensional arrays with an example.`

 

Two-Dimensional Arrays

                         

Two-dimensional arrays are defined as "an array of arrays". Since an array type is a first-class Java type, we can have an array of ints, an array of Strings, or an array of Objects. For example, an array of ints will have the type int[]. Similarly we can have int[][], which represents an "array of arrays of  ints". Such an array is said to be a two-dimensional array. 
The command

    int[][] A = new int[3][4];

declares a variable, A, of type int[][], and it initializes that variable to refer to a newly created object. That object is an array of arrays of ints. Here, the notation int[3][4] indicates that there are 3 arrays of ints in the array A, and that there are 4 ints in each of those arrays.
To process a two-dimensional array, we use nested for loops. We already know about for loop. A loop in a loop is called a Nested loop. That means we can run another loop in a loop. 

Notice in the following example how the rows are handled as separate objects. 

Code: Java
int[][] a2 = new int[10][5];
 // print array in rectangular form
 for (int i=0; i<a2.length; i++) {
     for (int j=0; j<a2[i].length; j++) {
         System.out.print(" " + a2[i][j]);
     }
     System.out.println("");
 }

In this example, "int[][] a2 = new int[10][5];" notation shows a two-dimensional array. It declares a variable a2 of type int[][],and it initializes that variable to refer to a newly created object. The notation int[10][5] indicates that there are 10 arrays of ints in the array a2, and that there are 5 ints in each of those arrays. 

Here is the complete code of the example:

public class twoDimension{
  public static void main(String[] args) {
    int[][] a2 = new int[10][5];
    for (int i=0; i<a2.length; i++) {
      for (int j=0; j<a2[i].length; j++) {
        a2[i][j= i;
        System.out.print(" " + a2[i][j]);
      }
      System.out.println("");
    }
  }
}

Here is the output for the program:

C:\tamana>javac twoDimension.java
C:\tamana>java twoDimension
 0 0 0 0 0
 1 1 1 1 1
 2 2 2 2 2
 3 3 3 3 3
 4 4 4 4 4
 5 5 5 5 5
 6 6 6 6 6
 7 7 7 7 7
 8 8 8 8 8
 9 9 9 9 9
C:\tamana>_

Download this program

                         

» View all related tutorials
Related Tags: java c string arrays com array class strings object io objects type command new int id ai define for example

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

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

this website is coool!!!!!!!!!!!

wHAT a very good examples!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Posted by t-cool on Wednesday, 10.29.08 @ 15:35pm | #81390

plz give me all available progs of 2d arrays

Posted by crazy on Sunday, 02.3.08 @ 22:38pm | #47121

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

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

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.