Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By wsaryada

Thread: System.out.println ();

  1. #1
    Twixmaster123 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Angry System.out.println ();

    This isn't necessarily a build issue I just have a question about
    Java Code:
    int[] data = {3, 2, 4, 3, 1, 0};
    data[1] = data[1] + data[3];
    System.out.println ( data[1] );
    ...the test said the output is 5, but how can there be a output if data[1] and data[3] haven't been established yet?

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

    Default Re: System.out.println ();

    What do you mean by the data haven't been established? In the first line you clearly declare and initialize the data array. In the second line you assign a new value for data[1] by adding the original value of data[1] and data[3] which mean 2 + 3. And this is of course give you 5 as the output.

  3. #3
    Twixmaster123 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: System.out.println ();

    I understand in the second line I am assigning a new value for data[1] but I am not understanding what the original value of data[1] and data[3] is. How is it 3 and 2? Wouldn't be 3 and 4? I guess I am just lost on where your getting the original values of data[1] and data[3]. Clarification on how its 2 and 3 would be great! Thanks for the quick reply.

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

    Default Re: System.out.println ();

    The index of an array started from 0 not 1. This mean when you access data[1] it will give you 2, when you access data[0] it will give you 3.

  5. #5
    Twixmaster123 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: System.out.println ();

    data[1] = 2 (how?) and data[0] = 3? So its saying data[3] hasn't been established yet and equals 0 or 3. Which gives us 2+3 then outputs 5, I get that much. Still lost on how data[1] = 2 and how data[3] is just 3? ^_____^

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: System.out.println ();

    Java Code:
    int[] data = {3, 2, 4, 3, 1, 0};
    Just wondering, but what do you think this line does?

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

    Default Re: System.out.println ();

    Please read the following tutorial for you to understand the basic of an array. Arrays (The Java Tutorials > Learning the Java Language > Language Basics)

  8. #8
    Twixmaster123 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: System.out.println ();

    To answer your question pbrockway2: Its initializing the data array with the numbers 3, 2 , 4, 3, 1, 0?

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

    Default Re: System.out.println ();

    Ok, you understand that it initialize your array. Now if you want to access the first value of your array which in this case is 3 how do you do it? What is the index of the first element of your array, or if I say it as data[x] , what is x?

  10. #10
    Twixmaster123 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: System.out.println ();

    data[1] = 3 and data[3] = 2?

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

    Default Re: System.out.println ();

    When you have:

    Java Code:
    int[] data = {3, 2, 4, 3, 1, 0};
    The corresponding index and value of the array will be:

    data[0] = 3;
    data[1] = 2;
    data[2] = 4;
    data[3] = 3;
    data[4] = 1;
    data[5] = 0;

    The index of an array started from 0 (zero). I hope this can make it clear for you.
    pbrockway2 likes this.

  12. #12
    Twixmaster123 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: System.out.println ();

    DURP! I think I just had the light bulb....the index of the array starts like 0,1,2,3,4,5 and the data is 3,2,4,3,1,0. So since the first value of the array is 3,

    data[0] = 3;
    data[1] = 2;
    data[2] = 4;
    data[3] = 3;
    data[4] = 1;
    data[5] = 0;

    and so on...

    Java Code:
    data[1] = data[1] + data[3];
    So here were taking data[1] which in this case is 2 and data[3] which in this case is 3 and doing simple addition and then outputting 5 to a new line, correct?

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: System.out.println ();

    Quote Originally Posted by Twixmaster123 View Post
    data[1] = 3 and data[3] = 2?
    I have no idea where you got the values 3 and 2 from.

    You are quite right - that line is initialising the array. In other words: it is putting values into the array. This more or less answers your question "Still lost on how data[1] = 2 and how data[3] is just 3?". They have those values because the array was initialised to have them.

    Again your supposition that "data[1] and data[3] haven't been established yet" turns out to be wrong. data[1] and data[3] have been *established* (in the sense of "initialised") precisely by that line of code.

    wsaryada's post (#11) shows exactly how the initialisation sets (/establishes/initialises) each of the elements of the data array.

    [Edit] ... slow ;(
    Last edited by pbrockway2; 03-24-2012 at 05:59 AM.

  14. #14
    Twixmaster123 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: System.out.println ();

    Yeah I corrected myself in the post above your last :P. Thanks guys for walking me through my episode of stupidity!

Similar Threads

  1. Replies: 8
    Last Post: 06-21-2011, 02:21 PM
  2. System.out.println
    By JohnDoe in forum New To Java
    Replies: 1
    Last Post: 09-05-2010, 10:14 AM
  3. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 08:52 AM
  4. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-18-2008, 11:06 PM
  5. System.out.println
    By sunjavaboy in forum Advanced Java
    Replies: 3
    Last Post: 03-22-2008, 01:30 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
  •