Grow your CSS skills. Land your dream job.

Multidimensional array in JAVA

  • # September 3, 2012 at 7:50 am

    for(int i = 0; i < 3; i++){
    for(int j=0; j < 5; j++){
    nums[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 output from this program is shown here:
    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

    Out put of this array seems to be confusing :(

    It should be something like this, 1, 4, 9 instead of 1,2,3
    what is actually happening?

    # September 3, 2012 at 9:12 am

    the output is correct
    the j for loop will finish looping before i is incremented and then j will loop again…

    edit// I’m curious why you don’t post this and other java questions in a java forum?

    # September 3, 2012 at 9:43 am

    I have posted in java forums, But haven’t been notified yet…

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".