Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Syntax for 3D array enhanced

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Syntax for 3D array enhanced

    I learned 2D arrays si I though I would just take a stab at 3D but I cannot figure out the correct syntax. Here is what I have:
    public class ThreeDArray
    {
    	public static void main(String[] args)
    	{
    		int[][][] x = new int[2][3][4];
    		x[0][0][0] = 5;
    		x[0][1][0] = 3;
    		x[0][2][0] = 1;
    		x[1][0][0] = 5;
    		x[1][1][0] = 3;
    		x[1][2][0] = 1;
     
     
     
    		for(int[] i: x )
    		{
    			for(int j : i)
    			{
    				for(int m : j)
    				{
    					System.out.print(m + " ");
    				}
    			}
    			System.out.println();
     
    		}
     
    	}
    }

    here's the two errors I am getting:
    ThreeDArray.java:15: error: incompatible types
    		for(int[] i: x )
    		             ^
      required: int[]
      found:    int[][]
    ThreeDArray.java:19: error: for-each not applicable to expression type
    				for(int m : j)
    				            ^
      required: array or java.lang.Iterable
      found:    int
    2 errors

    thanks in advance!


  2. #2
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: Syntax for 3D array enhanced

    The error message says it all. i should be a reference to a reference i.e with [][] instead of [], and j should have one []
    for(int [][] i : x)
    ...
    for(int[] j : i)
    But I would recommend plain old for loop with a counter for arrays with fixed size such as yours.

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Syntax for 3D array enhanced

    what do you mean by "plain old loop?"

    thanks

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: Syntax for 3D array enhanced

    Quote Originally Posted by mwr76 View Post
    what do you mean by "plain old loop?"

    thanks
    I mean something like
    for(int i = 0;i < 2;i++)
      for(int j = 0;j < 3;j++)
          for(int k = 0;k < 4;k++)
              System.out.println(x[i][j][k])
    Personally I find this simpler than the first method and is what is commonly thought as a method of accessing
    fixed size arrays. More here Multi-dimensional arrays in Java

  5. The Following User Says Thank You to dabdi For This Useful Post:

    mwr76 (September 22nd, 2011)

  6. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Syntax for 3D array enhanced

    You can print out contents of nested arrays with java.util.Arrays.deepToString(...), or you can roll your own array-descent code using the for..each style (ideal for this), or you can use loop indexes *but remember to loop from 0 to < array.length* - don't hard-code your array index maxes. For example:
    for (int i = 0; i < arrayIndex.length; i++)
      for (int j = 0; j < arrayIndex[i].length; j++)
      { /* do something */ }

Similar Threads

  1. java syntax highlighting
    By Saulius in forum Java Theory & Questions
    Replies: 2
    Last Post: August 24th, 2011, 06:47 AM
  2. do while syntax error problem
    By derekxec in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 1st, 2011, 07:30 PM
  3. syntax question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2011, 04:01 PM
  4. How do you read this syntax?
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 8th, 2010, 04:09 PM
  5. New Syntax Highlighting Feature!
    By JavaPF in forum Forum Updates & Feedback
    Replies: 15
    Last Post: July 14th, 2010, 10:20 AM