4 Replies - 300 Views - Last Post: 23 August 2015 - 03:00 PM Rate Topic: -----

#1 iluvforloop  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 23-August 15

Help with for loop

Posted 23 August 2015 - 09:43 AM

Hi all, I am new to Java and i'm having a hard time with this piece of code :
public class MainProg
{
    public static void main(String[]args)
    {
        int[][] array={{1,2},{3,4}};
        
        for(int i=0;i<array.length;i++)
        {
            for(int j=0;j<array[i].length;j++)
            {
                array[i][j]=array[i/2][j%2];
                System.out.print(array[i][j]+" ");
            }
        }
    }
}



This is the output:
1212


I can't understand whats the divide and modulo excaly doing? is it divide and modulo the indexs itself? or the element inside [i][j]? really confused!
array[i][j]=array[i/2][j%2];


It's not a homework or somthing, i'm just trying to figure out that part...
thanks for the help and apologize for my bad English.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with for loop

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8739
  • View blog
  • Posts: 15,049
  • Joined: 19-March 11

Re: Help with for loop

Posted 23 August 2015 - 10:09 AM

View Postiluvforloop, on 23 August 2015 - 11:43 AM, said:

I can't understand whats the divide and modulo excaly doing? is it divide and modulo the indexs itself? or the element inside [i][j]? really confused!
array[i][j]=array[i/2][j%2];


the expression array[x] evaluates x and looks up the element in array, at that value. So this line evaluates i/2 and j%2, and uses them to look up a value in array. Then it writes this value to the position array[i][j].
If i=1, j=0, then we're looking up 1/2, which evaluates to 0 in an integer context, and 0/2, so we get array[0][0], which is 1, so we write the value 1 to array[1][0]

Does that help?

Notice that we're doing a concurrent modification to this array: we're using it for both the input and the output. This makes the code hard to read and hard to predict, and is usually considered bad style.

Note: I originally typed "If j=1, j=0, ...". Corrected in-line.

This post has been edited by jon.kiparsky: 23 August 2015 - 02:43 PM
Reason for edit:: mistyped "j" for "i"

Was This Post Helpful? 1
  • +
  • -

#3 iluvforloop  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 23-August 15

Re: Help with for loop

Posted 23 August 2015 - 01:07 PM

Quote

If j=1, j=0, then we're looking up 1/2, which evaluates to 0 in an integer context, and 0/2, so we get array[0][0], which is 1, so we write the value 1 to array[1][0]

Basically i think i understood what you explained, but i dont understand why you inserted the value 1 to array[1][0]?
because it's the next position after array[0][0]?
[i/2][j%2]
<-- solution of this, is the position of the new value?

thank you for your help!
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8739
  • View blog
  • Posts: 15,049
  • Joined: 19-March 11

Re: Help with for loop

Posted 23 August 2015 - 02:45 PM

Sorry, in my example I mistyped the variable assignments. I meant to set i=1, j=0. Does that explain why I assigned to array[1][0]. Remember, the assignment is made to the item on the left-hand side of the equal sign.

THIS = THAT


means that THIS gets whatever value results from evaluating THAT.
Was This Post Helpful? 1
  • +
  • -

#5 iluvforloop  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 23-August 15

Re: Help with for loop

Posted 23 August 2015 - 03:00 PM

I understood what you explained!
Tried to solve it on a paper, then compiled it, and it was ok!

Posted Image

Thanks alot!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1