File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrays" Watch "arrays" New topic
Author

arrays

sankar sivassoupramanime
Greenhorn

Joined: Jan 03, 2001
Posts: 12
Friends,
Here goes the code,
class a{
public static void main(String args[]){
int a[][]= new [2][];
System.out.println(a[0][0]);
}}
Output;
null
In this case i suppose it should print 0. Why it doesn't happen? Would be happy if you give a simple example for three dimentional array.
thanks folks!!
Elisabeth Van
Ranch Hand

Joined: Feb 09, 2001
Posts: 42
class a{
public static void main(String args[]){
int a[][]= new [2][];
System.out.println(a[0][0]);
}}

First of all, I'm assuming that you're statement declaring the array was meant to be
int a[][] = new int [2][];
When I run this code, I get a null pointer exception, the reason being that a 2-dimensional array is an array of arrays. What you've declared so far is that you'll have 2 arrays that will contain arrays, but you haven't created or declared the second array.
If you're creating a rectangular array (both arrays hold the same number of elements, in this case 5), you could use:
int a[][] = new int[2][5];
If your array is non-rectangular, you can declare it in a format like this:
int a[][] = new int[2][];
a[0] = new int[5];
a[1] = new int[8];
So what you're declaring is that a is an array of two arrays, the first of which holds 5 integers, and the second of which holds 8 integers.
For more info and an example on 2 and 3-dimensional arrays, see my posting at http://www.javaranch.com/ubb/Forum33/HTML/001644.html
(and this is my first shot at using UBB tags, so pardon me if this doesn't show up quite the way I'd intended.)
sankar sivassoupramanime
Greenhorn

Joined: Jan 03, 2001
Posts: 12
thanks buddy!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrays