Results 1 to 9 of 9
  1. #1
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default Why for God's sakes!? Why ?

    Can anybody explain to me why is that? Why in position 5 all changes? suddenly we have nr.10 there? :confused::mad:
    - x<index - what the hell is x and index here?

    class test1{
    public static void main(String[]args){
    int[]arr=new int[10];
    int x=17;
    for (int index=0; index<arr.length; index++){
    arr[index]=x;
    x-=3;
    if (x<index)
    arr[index]+=5;
    }
    System.out.println("Arrayen:");
    for (int z : arr)
    System.out.println(z);

    }
    }
    Answer is:
    Arrayen:
    17
    14
    11
    8
    10
    7
    4
    1
    -2
    -5

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    I never knew that Java was so tightly related to religious topics ...

    kind regards,

    Jos

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Or you could let the code do the "walking" for you:
    Java Code:
       public static void main(String[] args) {
          int[] arr = new int[10];
          int x = 17;
          for (int index = 0; index < arr.length; index++) {
             arr[index] = x;
             x -= 3;
             if (x < index)
                arr[index] += 5;
             System.out.printf("index = %d    x = %3d    arr[index] = %3d%n", index, x, arr[index]);
          }
          System.out.println("Arrayen:");
          for (int z : arr)
             System.out.println(z);
       }

  5. #5
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default

    thank you very much!!!!...well thats not my code, of course...I try to learn how it works etc...learn Java..if it possible..ever ....;)

  6. #6
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default

    ok still questions here... why is that while x is 2 so arr[index] is 10 but while x is -1 so arr[index] is 7....so even x is smaller than index is not matter any more the plus 5 to arr[index], is just not happening anymore---why?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Izabela View Post
    ok still questions here... why is that while x is 2 so arr[index] is 10 but while x is -1 so arr[index] is 7....so even x is smaller than index is not matter any more the plus 5 to arr[index], is just not happening anymore---why?
    Take a pencil and a piece of paper and 'walk through' the code manually, step by step and see for yourself what actually happens.

    kind regards,

    Jos

  8. #8
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

    Default

    I did and still not get it...

  9. #9
    Izabela is offline Member
    Join Date
    Aug 2010
    Location
    Sweden, Stockholm
    Posts
    12
    Rep Power
    0

Posting Permissions

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