Results 1 to 10 of 10
  1. #1
    selam is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default Basic help here please....for each loop

    Hallo All;

    I have a question please regarding For-Each style for loop, just puzzeled i'm new to java and i'm reading a very wonderful book "Java a Beginner's guide" by Herbert Schildt. here goes the code and my question:

    Java Code:
    class ForEach2{
             public static void main(String args[]){
                     int sum = 0;
                     int nums[] [] = new int [3] [5];
                 for(int i = 0; i < 3; i++)
                     for(int j = 0; j < 5; j++)
                       nums[i][j] = (i+1)*(j+1);
    
    
                 for(int x[] : nums) {
                     for(int y : x)  {
                       System.out.println("value is : " + y);
                       sum += y;
                      }
                    }
                    System.out.println("Summation: " +sum);
          }
        }
    the out put from this program is

    value is: 1
    value is: 2
    value is: 3
    value is: 4
    value is: 5
    value is: 2
    value is: 4
    value is: 6
    value is: 8
    value is: 10
    value is: 3
    value is: 6
    value is: 9
    value is: 12
    value is: 15

    Summation: 90

    we have here four 'for' loops, i am just lost. can any way explain to me please. what value does exactly has 'y' here? i know for some of you it is very easy but i cann't get it.

    thanks in advance!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Pick things apart a little bit and play with it to see what's going on.
    Like this:
    Java Code:
    public class Test {
        public static void main(String[] args) {
            int sum = 0;
            int[][] nums = new int [3] [5];
            for(int i = 0; i < 3; i++)
                for(int j = 0; j < 5; j++)
                    nums[i][j] = (i+1)*(j+1);
    
            print(nums);
    
            for(int[] x : nums) {
                print(x);
                for(int y : x)  {
                    System.out.println("value is : " + y);
                    sum += y;
                }
            }
            System.out.println("Summation: " +sum);
        }
    
        private static void print(int[][] n) {
            for(int i = 0; i < n.length; i++) {
                System.out.printf("num[%d] = ", i);
                print(n[i]);
            }
        }
    
        private static void print(int[] n) {
            System.out.print("[");
            for(int i = 0; i < n.length; i++) {
                System.out.print(n[i]);
                if(i < n.length-1) {
                    System.out.print(", ");
                } else {
                    System.out.print("]\n");
                }
            }
        }
    }

  3. #3
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    i'm confused about that syntax... newbie here too. when the for loop says for (int x[] : nums) does that mean its going through each value of the two dimensional array? i've never yet encountered that syntax of int x[].

  4. #4
    tghn2b is offline Member
    Join Date
    Jan 2008
    Posts
    31
    Rep Power
    0

    Default

    what is this for(int x[] : nums)
    & what is this for(int y : x)
    what does the ':' do?


    have reread the question after my reply,

    and you can read more here h t t p : / /
    java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html


    I think what I wrote below is a waste of time

    Because we have 15 "value is: "
    I take it, it is only using the i++ and J++
    how about this
    value is: 1 // i = 0 j = 0
    value is: 2 // i = 0 j = 1
    value is: 3 // i = 0 j = 2
    value is: 4 // i = 0 j = 3
    value is: 5 // i = 0 j = 4
    value is: 2 // i = 1 j = 0
    value is: 4 // i = 1 j = 1
    value is: 6 // i = 1 j = 2
    value is: 8 // i = 1 j = 3
    value is: 10 // i = 1 j = 4
    value is: 3 // i = 2 j = 0
    value is: 6 // i = 2 j = 1
    value is: 9 // i = 2 j = 2
    value is: 12 // i = 2 j = 3
    value is: 15 // i = 2 j = 4
    Last edited by tghn2b; 12-30-2008 at 09:41 AM.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Syntax of the enhanced for loop, aka, the for-each loop. For more about this see The For–Each Loop.

  6. #6
    tghn2b is offline Member
    Join Date
    Jan 2008
    Posts
    31
    Rep Power
    0

    Default

    OK it seems to be also called "for-in loop."

    so there is not
    we have here four 'for' loops,
    There is two "for loops" and two "for-in loops."

    read more here
    bugger://leepoint.net/notes-java/flow/loops/foreach.html
    Last edited by tghn2b; 12-28-2008 at 08:28 AM. Reason: in red (I was making my reply when "hardwired" posted)

  7. #7
    selam is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Ohh thanks All!

    hardwired let me try ur way and let me read the links i'll be back with result
    hopefully will get it all.

    thanks again

  8. #8
    selam is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Hi again

    so far so good,

    Java Code:
    print(nums);
    prints these three arrays, am i right?

    num[0] = [1, 2, 3, 4, 5]
    num[1] = [2, 4, 6, 8, 10]
    num[2] = [3, 6, 9, 12, 15]


    and the
    Java Code:
    print(x)
    does this
    [1, 2, 3, 4, 5]


    and
    Java Code:
    sum += y;
    does this

    [2, 4, 6, 8, 10]

    and last what i got is
    Java Code:
    for(int y : x)
    the value of x is in y right?

    thanks again for the links and info.
    i will be back with more puzzels, just want to understand how java is hopefully i'm in a good hand here :)

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation don't go buggy at Whip and Buggy Software Company

    for(int y : x) would be read, for each int in x, which to answer tghn2b and diggitydog, the elements may in fact be ints, arrays, could even be SomeObsucreClassWroteToBeABigObscureClass. or an array of them. If the elements of an array are arrays, you may use the variable[index] code to "talk" to each element in the sub-array.

    We see some bizzare looking sytax in Artificial Intelligence that looks like:
    Java Code:
    variable = first[index_1][index_2].getValue();
    Or one which I use all the time:
    Java Code:
    System.out.println(array[index].toString());
    which I use for de-bugging, so called because you go buggy trying to use de-buggers.

    de buggers are full of bugs, an ......

    oh, well. sorta gets re-cursive.....
    Last edited by Nicholas Jordan; 12-30-2008 at 04:52 PM.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  10. #10
    selam is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

Similar Threads

  1. very basic code
    By BlueJ2008 in forum New To Java
    Replies: 1
    Last Post: 10-14-2008, 01:40 PM
  2. inheritance basic.
    By newmember in forum New To Java
    Replies: 1
    Last Post: 08-02-2008, 09:35 PM
  3. basic java help
    By adred in forum New To Java
    Replies: 0
    Last Post: 03-08-2008, 12:36 PM
  4. Basic Program Please Help!!
    By VinceGuad in forum New To Java
    Replies: 3
    Last Post: 02-01-2008, 03:35 PM
  5. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 05:45 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
  •