Sign in to follow this  

[java] Hardcoding numbers into a 3 dimensional array in Java

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys. I was wondering if someone could help me with a problem I am having. I am trying to create a 3 dimensional array in Java and fill it right after I create it. I have done this with 2D arrays before with a format like this:

 int array[][] = {
	{0,0,0,0,0,0},
   	{2,4,6,8,10,12},
   	{14,16,18,20,22,24},
   	{26,28,30,32,34,36},
   };


I cant figure out how to add the third dimension though. I have searched the net and tons of Java tutorials and books but they all just cover one and two dimensional arrays. I appreciate any help I can get on this matter. Thanks so much!!! Josh a.k.a. CyberGeek

Share this post


Link to post
Share on other sites
Pretty much the same way, just nested another level deeper.


String[][][] array3 =
{
{
{"a","b","c"},
{"d","e","f"}
},
{
{"g","h","i"},
{"j","k","l"}
}
};

for (int i=0;i<array3.length;i++)
{
for (int j=0;j<array3[i].length;j++)
{
for (int k=0;k<array3[i][j].length;k++)
{
System.out.println(array3[i][j][k]);
}
}
}

Share this post


Link to post
Share on other sites

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this