Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > Java

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

Multi-dimensional arrays in Java

By pradeep pradeep is offline

On 11th August, 2006
Post Multi-dimensional arrays in Java

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy Multi-dimensional arrays in Java link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

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.

Declaration

Declare a 2-dimensional array as follows:

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

Allocation

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

Code: Java
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,

Code: Java
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]);
     }
     System.out.println("");
 }

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.

Code: Java
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]);
     }
     System.out.println("");
 }
Old 08-12-2006, 02:14 PM   #2
abcdou
Light Poster
 
Join Date: Aug 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
abcdou is on a distinguished road

Re: Multi-dimensional arrays in Java


hi pradeep!

how about if the user will input student information?
Label(row 0) Lastname firstname address
dou abc malaysia

There are method()
1.Assign name
2.Display name
3.Sort name in ascending
4.Search name

how to do this pradeep
pls........
abcdou is offline   Reply With Quote
Old 08-12-2006, 03:18 PM   #3
pradeep
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,461
Thanks: 0
Thanked 19 Times in 16 Posts
Rep Power: 6
pradeep will become famous soon enough
Send a message via Yahoo to pradeep

Re: Multi-dimensional arrays in Java


We will use a multi-dimensional array.

Code: Java
String[][] Data;
 
 //Assign the values, do it either dynamically or statically
 //For first fow
 Data[0][0] = "S"; //lastname
 Data[0][1] = "Pradeep"; //firstname
 Data[0][2] = "Kolkata"; //location
 
 //Second row
 Data[1][0] = "Bhimani"; //lastname
  Data[1][1] = "Shabbir"; //firstname
  Data[1][2] = "Kolkata"; //location
 
 //Add as many rows you want
 
 //printing
 System.out.print("Lastname\tFirstname\tLocation\n");
 for(i=0;i<2;i++)
 {
   for(j=0;j<3;j++)
   {
     System.out.print(Data[i][j]+"\t");
   }
   //move to new line
   System.out.print("\n");
 }


This should do the work for you.
__________________
Vote for the Most Entertaining Member of 2008

To err is human,to detect is divine!
pradeep is offline   Reply With Quote
Old 08-14-2006, 06:02 AM   #4
abcdou
Light Poster
 
Join Date: Aug 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
abcdou is on a distinguished road
Thumbs up

Re: Multi-dimensional arrays in Java


thank you pradeep!!!

how about sorting and searching?

do you have any tutorial in two dimensional array?


thanks.....
abcdou is offline   Reply With Quote
Old 08-14-2006, 12:31 PM   #5
pradeep
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,461
Thanks: 0
Thanked 19 Times in 16 Posts
Rep Power: 6
pradeep will become famous soon enough
Send a message via Yahoo to pradeep

Re: Multi-dimensional arrays in Java


You can use simple sorting algorithms like Bubble Sort,Selection Sort to sort the array data. Multi-dimensional array concepts can be applied to two-dimensional arrays. You are free to ask any specific questions you have about two-dimensional arrays.
__________________
Vote for the Most Entertaining Member of 2008

To err is human,to detect is divine!
pradeep is offline   Reply With Quote
Old 08-19-2006, 10:08 AM   #6
abcdou
Light Poster
 
Join Date: Aug 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
abcdou is on a distinguished road

Re: Multi-dimensional arrays in Java


how about in searching?

thanks pradeep
abcdou is offline   Reply With Quote
Old 07-12-2007, 06:37 PM   #7
champion
Light Poster
 
Join Date: May 2007
Location: Harare, Zimbabwe
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
champion is on a distinguished road
Send a message via Yahoo to champion

Re: Multi-dimensional arrays in Java


How do we test if the array is null not testing for a null element?
champion is offline   Reply With Quote
Old 07-12-2007, 08:56 PM   #8
champion
Light Poster
 
Join Date: May 2007
Location: Harare, Zimbabwe
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
champion is on a distinguished road
Send a message via Yahoo to champion

Re: Multi-dimensional arrays in Java


Quote:
Originally Posted by pradeep
We will use a multi-dimensional array.

Code: Java
String[][] Data;
 
 //Assign the values, do it either dynamically or statically
 //For first fow
 Data[0][0] = "S"; //lastname
 Data[0][1] = "Pradeep"; //firstname
 Data[0][2] = "Kolkata"; //location
 
 //Second row
 Data[1][0] = "Bhimani"; //lastname
  Data[1][1] = "Shabbir"; //firstname
  Data[1][2] = "Kolkata"; //location
 
 //Add as many rows you want
 
 //printing
 System.out.print("Lastname\tFirstname\tLocation\n");
 for(i=0;i<2;i++)
 {
   for(j=0;j<3;j++)
   {
     System.out.print(Data[i][j]+"\t");
   }
   //move to new line
   System.out.print("\n");
 }


This should do the work for you.
How do we test if the array is null not testing for a null element?
champion is offline   Reply With Quote
Old 03-20-2008, 04:08 PM   #9
shilpa dessai
Newbie Member
 
Join Date: Mar 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
shilpa dessai is on a distinguished road
Unhappy

Re: Multi-dimensional arrays in Java


Pradeep please reply soon we have to complete our project and we are stuck only due to this sorting code.......
shilpa dessai is offline   Reply With Quote
Old 03-20-2008, 04:32 PM   #10
pradeep
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,461
Thanks: 0
Thanked 19 Times in 16 Posts
Rep Power: 6
pradeep will become famous soon enough
Send a message via Yahoo to pradeep

Re: Multi-dimensional arrays in Java


Please post the 1D sorting you have already written!
__________________
Vote for the Most Entertaining Member of 2008

To err is human,to detect is divine!
pradeep is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
JSP and Java Servlets pradeep JSP 12 06-27-2009 04:51 PM
Using Regular Expressions in Java pradeep Java 13 02-20-2008 05:07 PM
Arrays in Java pradeep Java 2 01-09-2007 05:20 PM
A Simple Java Applet pradeep Java 2 08-11-2006 11:38 AM
Java resources Amit Ray Java 1 03-06-2006 08:23 PM

 

All times are GMT +5.5. The time now is 04:40 AM.