Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


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

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

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

Latest Searches:
struts e file jpeg
binary
String to array in jav
ascending and descendi
server side validation
how to pass same varia
2 resultset
change color in JTabl
jstl basics tomcat
Jigloo
http get example
VNCj (Java VNC S
how to capture multipl
DOM
count
how to use unique cons
Eclipse 3.4 EJB
Interview question on
href
jfreechart tutorials
Photoshop Effects Hole
Photoshop Photo Effect
ready made component o
java random numbers
count row in resultset
jdbc using prepared st
Horizontal splitpane
link
logiceic:equal
Pattern Testing
Java Pass Value
html code for developi
modity data in text fi
Photoshop Text Effects
validate radio button
drop down in jsp
TextField
flex datagrids
sum of rows and coloum
display
multiple submit button
user registration
jsp bean
java example program t
populate drop down fro
vector in java
Struts Validator Frame
java xml
Get Array List in a JS
Add listener to compon
distinct
game in java
jQuery To Hide the Div
rotate
Why Virtual Dedicated
runnable
how to execute update
helloe world servlet
t:htmltag
JSP:File Input Paramet
Check Box (Form Tag) E
FCKediter
2 tier ,3 tier ,n tier
user registration and
pthread.h
where we place plug-in
create table in struts
if not null condition
abstrct path name of f
howtodeclareanarrayinj
JPOX
dynamic insert value J
How to show expression
populate drop down fro
file input output
dwr reverse ajax
b tree implementation
jboss seam
iBatis Stored Procedur
Photoshop Effects Sig
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  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

Indian Software Development Company | iPhone 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.