Multi-dimensional array in Java

Since Java does not have pointers I was thinking if it can hold multi-dimensional arrays. Multi-dimensional array is an array where each row is another array, which can have different sizes. In C you can create an array of pointers and allocates each one to an array of different sizes. The following are two examples to allocates a multi-dimensional array in Java:

// declaration statement
int tri[][] = { {1,2,3}, {4,5}, {1} };

// dinamically statement
int[][] tri;
tri = new int[10][];
for (int r=0; r<tri.length; r++) {
	tri[r] = new int[r+1];
}
 
// print the triangle matrice
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("");
}

Read more:
http://www.go4expert.com/forums/showthread.php?t=1162

See ya

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: