MKod
MKod

Reputation: 823

Java: Multi Dimensional array output confusion?

public class VarNoOfCols {
    public static void main(String[] args) {
        int a[][] = new int[3][];

        a[0]=new int[3];
        a[1]=new int[2];
        a[2]=new int[1];
        int temp=3;
        for(int i =0; i<3;i++) {
            for(int k=0;k<temp;k++) {
                a[i][k]= k*10;
                           temp-- ;
            }


        }
    }
}

--- output that I assumed ---- is below ---But this is incorrect.

(0 0) 0  (0 1) 10
(1 0) 0  (1 1) 10
(2 0) 0  (2,1) 10

I know this is incorrect. (My question is - on completing second iteration, "k" is greater than "temp" and when conditon fails it will stop the inner statments and do the next job (what ever it suppose to be).Why am i getting (0,2) = 20 and why i dont see (2,1) = 10 ?

You can see the correct output:

(0 0) 0  (0 1) 10 (0 2) 20
(1 0) 0  (1 1) 10
(2 0) 0

I am a learner and i really appreciate someone's help here. thank you

Upvotes: 1

Views: 180

Answers (4)

MKod
MKod

Reputation: 823

public class VarNoOfCols {           
    public static void main(String[] args) { 

        int a[][] = new int[3][];

        a[0]=new int[3];
        a[1]=new int[2];
        a[2]=new int[1];

        int temp=3;

        for(int i =0; i<3;i++)
        {
            for(int k=0;k<temp;k++)
                //the inner for lopp doesn't have curly "{" braces.
                //temp will be 3 for 1st loop and when k becomes 3 it exit inner loop.
                a[i][k]= k*10;
            temp--;
        }
        //temp--;
        //System.out.println("temp : " +temp + " \n " );

    }   
}

Thank you guys. I figuered out the logic behind. it is not logic, it is just that { that should be watched. I will get following output if I don't use braces { in inner loop:

(0 0 ) 0   (0 1 ) 10   (0 2 ) 20
(1 0 ) 0   (1 1 ) 10
(2 0 ) 0

If the braces { and } present in inner loop with temp variable inside inner loop then I will get the following output:

(0 0 ) 0   (0 1 ) 10
(1 0 ) 0

Upvotes: 0

alain.janinm
alain.janinm

Reputation: 20065

Actually with your last edit (with temp-- in the second for) you obtain neither the first nor the second output.

Why?

Because you never reassign temp and after 3 time be decremented the second loop will not be executed anymore. So you got a value for (0;0) (0;1) and (1;0) only.

Why you can't obtain output 1 (square one)?

a[2] have a size of 1 so you can't have something in (2;1)

How to obtain the second output?

Don't put the temp-- in the second loop but after the second loop (at the end of the first loop).

Upvotes: 0

NullPoiиteя
NullPoiиteя

Reputation: 57322

this is because the temp and the k

in your program first i=0

  1. k=0,temp=3 then (0 0) so output 0*10 = 0
  2. k=1,temp=2 then (0 1) so output 1*10 = 10
  3. k=2,temp=1 condition false in for loop (there the condition in the outer for loop(having i) is correct but in the inner for loop the condation k

i=1

  1. k=0,temp=3 then (1 0) so output 0*10 = 0
  2. k=1,temp=2 then (1 1) so output 1*10 = 10

i=2

  1. k=0,temp=3 then (2 0) so output 0*10 = 0
  2. k=1,temp=2 then (2 1) so output 1*10 = 10

there is no need of temp. to get correct output use this

for(int i =0; i<3;i++)
{
   for(int k=0;k<3;k++)
      a[i][k]= k*10;

}

Upvotes: 0

Boris Strandjev
Boris Strandjev

Reputation: 46943

Change the code like that:

for(int i =0; i<3;i++)
{
    for(int k=0;k<3;k++)
        a[i][k]= k*10;

}

If you wanted a square output, why do you use the control variable temp that will change the number of outputted entries on each iteration over i?

Upvotes: 1

Related Questions