Results 1 to 6 of 6
  1. #1
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default enhanced 4 loop 4 2d array (mod. edit: enhanced for-loop for 2D array)

    Hi all,


    can an enhanced for loop be used for mulitdimensional array? if yes then can anyone kindly give me some example. I tried it on my side and it gave me error "incompatible types".

    Thank you all
    Last edited by DarrylBurke; 03-29-2012 at 10:34 AM. Reason: Clarified subject line

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: enhanced 4 loop 4 2d array (mod. edit: enhanced for-loop for 2D array)

    Don't use the digit '4' for the word 'for'. This is a technical forum, not a kindergarten school.

    Clarified the title.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: enhanced 4 loop 4 2d array (mod. edit: enhanced for-loop for 2D array)

    Quote Originally Posted by java4amanda View Post
    can an enhanced for loop be used for mulitdimensional array? if yes then can anyone kindly give me some example. I tried it on my side and it gave me error "incompatible types".l
    First show what you tried.

    A multidimensional array is an array of arrays.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: enhanced 4 loop 4 2d array (mod. edit: enhanced for-loop for 2D array)

    it is some like this

    class Some

    {

    public static void main(String[] args)

    {

    int a[][] = {{1,2,3}, {1,2}};


    for(int b: a)

    {

    System.out.println(b);

    }

    }

    }

    Now when i try to run this it throws incompatible error !

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default Re: enhanced 4 loop 4 2d array (mod. edit: enhanced for-loop for 2D array)

    As Darryl wrote: a multi dimensional array is an array of arrays, so your enhanced for-loop iterates over one dimensional arrays:

    Java Code:
    for (int[] row : a) {
       for (int element : row) {
          ...
       }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: enhanced 4 loop 4 2d array (mod. edit: enhanced for-loop for 2D array)

    thanks!!

Similar Threads

  1. Using enhanced for loop to print array list??
    By bdl1127 in forum New To Java
    Replies: 1
    Last Post: 02-19-2012, 01:43 AM
  2. enhanced for loop
    By billq in forum New To Java
    Replies: 1
    Last Post: 05-01-2010, 03:07 PM
  3. Enhanced For Loop
    By terahawks in forum New To Java
    Replies: 3
    Last Post: 04-16-2010, 09:46 AM
  4. Help for Enhanced for loop
    By jboy in forum New To Java
    Replies: 6
    Last Post: 09-13-2009, 07:45 AM
  5. Enhanced for loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 10:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •