Results 1 to 7 of 7
  1. #1
    Mercy is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Please explain to me this array

    I wondering why this program's result is different from mine. Please explain to me.

    public class Test1112 {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int [ ] data = {6, 2, 5, 1, 10, 3, -3};
    int result = 0;
    for (int i = 2; i < data.length; i++) {
    result = result + (data[i] - data[i-2]);
    System.out.println("result = " + result);
    }

    }

    }
    The result is :
    result = -1
    result = -2
    result = 3
    result = 5
    result = -8

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Please explain to me this array

    What is your expected result and why?

  3. #3
    Mercy is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Please explain to me this array

    I just want to know why the result suppose to be this.
    result = -1
    result = -2
    result = 3
    result = 5
    result = -8

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Please explain to me this array

    .....because the java compiler and runtime are working well ? :-)

    i=2
    data[i] -> data[2] = 5
    data[i-2] -> data[0] = 6
    5-6 = -1
    ...and so on....

  5. #5
    Mercy is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Please explain to me this array

    the first is -1 , but the rest of all is wrong result.
    For example, when i = 3,
    data[3] -> 1
    data[3-2] -> 2
    1-2=-1.But the result is -2.

  6. #6
    Mercy is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Please explain to me this array

    ye run time everything is working fine

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Please explain to me this array

    Quote Originally Posted by Mercy View Post
    the first is -1 , but the rest of all is wrong result.
    For example, when i = 3,
    data[3] -> 1
    data[3-2] -> 2
    1-2=-1.But the result is -2.
    You have this expression in your program:

    Java Code:
    result = result + (data[i] - data[i-2]);
    That's mean you have to include the last value of result in the calculation. In this case when the i = 2 the result will have a value of -1. When the i = 3 the result become:

    Java Code:
    result = -1 + (1 - 2);
    This will give you -2.

Similar Threads

  1. Can someone explain...
    By Army in forum New To Java
    Replies: 17
    Last Post: 01-13-2012, 06:14 AM
  2. Replies: 2
    Last Post: 12-13-2010, 02:34 PM
  3. Replies: 3
    Last Post: 12-13-2010, 08:22 AM
  4. Replies: 3
    Last Post: 09-06-2010, 06:39 AM
  5. Can someone explain why...
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2009, 07:59 AM

Posting Permissions

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