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 -- 2-dimensional

[an error occurred while processing this directive]

Multi-dimensional arrays

Java, as with most languages, supports multi-dimensional arrays - 1-dimensional, 2-dimensional, 3-dimensional, ... This discusses 2-dimensional arrays, but the same principles apply to higher dimensions.

2-dimensional arrays

2-dimensional arrays are usually represented in a row-column approach on paper, and the terms "rows" and "columns" are used in computing.

Arrays of arrays

There are two ways to implement 2-dimensional arrays. Many languages reserve a block of memory large enough to hold all elements of the full, rectangular, array (number of rows times number of columns times the element size). Java doesn't do this. Instead Java builds multi-dimensional arrays from many one-dimensional arrays, the so-called "arrays of arrays" approach. [C++ supports both styles.]

There are a couple of interesting consequences of this: Rows may be different sizes. Also, each row is an object (an array) that can be used independently.


Declare a 2-dimensional array as follows:

int[][] a2; // Declares, but doesn't allocate, 2-dim array.


As with all arrays, the new keyword must be used to allocate memory for an array. For example,

int[][] a2 = new int[10][5];

This allocates an int array with 10 rows and 5 columns. As with all objects, the values are initialized to zero (unlike local variables which are uninitialized).

This actually allocates 6 objects: a one-dimensional array of 5 elements for each of the rows, and a one-dimensional array of ten elements, with each element pointing to the appropriate row array.

Processing 2-dimensional arrays

Often 2-dimensional arrays are processed with nested for loops. Notice in the following example how the rows are handled as separate objects. For example,

int[][] a2 = new int[10][5];
// print array in rectangular form
for (int r=0; r<a2.length; r++) {
    for (int c=0; c<a2[r].length; c++) {
        System.out.print(" " + a2[r][c]);

Uneven rows

One consequence of arrays of arrays is that each row can be a different size ("ragged" arrays). For example, we could create a lower triangular array, allocating each row "by hand" as follows.

int[][] tri;
tri = new int[10][];  // allocate array of rows
for (int r=0; r<tri.length; r++) {
    tri[r] = new int[r+1];

// print the triangular array (same as above really)
for (int r=0; r<tri.length; r++) {
    for (int c=0; c<tri[r].length; c++) {
        System.out.print(" " + tri[r][c]);
Ask programming questions?



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:

hi can you give me the two dimensional arrays that has a colums and rows,
and can you specify it putting the codes so that i could put it in JCreator or NetBeans?? please and thank you....

Posted by Lexine on Saturday, 01.30.10 @ 07:04am | #94441

  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]