Results 1 to 14 of 14
Thread: System.out.println ();
- 03-24-2012, 04:23 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
System.out.println ();
This isn't necessarily a build issue I just have a question about
...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?Java Code:int[] data = {3, 2, 4, 3, 1, 0}; data[1] = data[1] + data[3]; System.out.println ( data[1] );
- 03-24-2012, 04:33 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 03-24-2012, 04:53 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
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.
- 03-24-2012, 05:05 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 03-24-2012, 05:14 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
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? ^_____^
- 03-24-2012, 05:18 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,548
- Rep Power
- 11
Re: System.out.println ();
Just wondering, but what do you think this line does?Java Code:int[] data = {3, 2, 4, 3, 1, 0};
- 03-24-2012, 05:20 AM #7
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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)
Website: Learn Java by Examples
- 03-24-2012, 05:22 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
Re: System.out.println ();
To answer your question pbrockway2: Its initializing the data array with the numbers 3, 2 , 4, 3, 1, 0?
- 03-24-2012, 05:27 AM #9
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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?
Website: Learn Java by Examples
- 03-24-2012, 05:32 AM #10
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
Re: System.out.println ();
data[1] = 3 and data[3] = 2?
- 03-24-2012, 05:43 AM #11
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: System.out.println ();
When you have:
The corresponding index and value of the array will be:Java Code:int[] data = {3, 2, 4, 3, 1, 0};
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.Website: Learn Java by Examples
- 03-24-2012, 05:51 AM #12
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
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...
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?Java Code:data[1] = data[1] + data[3];
- 03-24-2012, 05:56 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,548
- Rep Power
- 11
Re: System.out.println ();
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.
- 03-24-2012, 06:08 AM #14
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
is System.out.print same as System.out.println ?
By usuf in forum New To JavaReplies: 8Last Post: 06-21-2011, 02:21 PM -
System.out.println
By JohnDoe in forum New To JavaReplies: 1Last Post: 09-05-2010, 10:14 AM -
Println VS system.out.println
By ccie007 in forum New To JavaReplies: 2Last Post: 05-20-2010, 08:52 AM -
difference between system.out.println() & out.println()
By wickedrahul9 in forum Advanced JavaReplies: 5Last Post: 10-18-2008, 11:06 PM -
System.out.println
By sunjavaboy in forum Advanced JavaReplies: 3Last Post: 03-22-2008, 01:30 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks