• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what will be the output of this two dimentional array and why

 
ravi suthar
Greenhorn
Posts: 17
Firefox Browser Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try to execute the code ?
 
ravi suthar
Greenhorn
Posts: 17
Firefox Browser Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Did you try to execute the code ?


i am expecting the output will print 3
but the actual output is 5
i am confused why it is so
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can guess what a[2]=a[0]; is doing ?
 
ravi suthar
Greenhorn
Posts: 17
Firefox Browser Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Can guess what a[2]=a[0]; is doing ?


a[2] is start referring to a[0]
both a[2] and a[0] have the same reference
and there indexing would be 0 1 2 3 4
so the sum would be 0+1+2=3 ???
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you start writing to a[2], what happens to the elements in a[0] ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47310
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct about the notion of assignment, but don’t say reference. You have an int[], which is an array of primitives. The contents of a variable pointing to a primitive is not a reference. It is the actual value. If you write int i = 2; the actual value in the memory location pointed to by i is 2.
 
ravi suthar
Greenhorn
Posts: 17
Firefox Browser Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i got the answer
when i=0 value of a[0][3] will be 3
and when i=2 value of a[2][3] will be 5
both a[2] and a[0] reffered at same.
so value 3 is replaced by 5
it means every value of a[0] is replaced by a[2]
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are correct about the notion of assignment, but don’t say reference. You have an int[], which is an array of primitives. The contents of a variable pointing to a primitive is not a reference. It is the actual value. If you write int i = 2; the actual value in the memory location pointed to by i is 2.



I think the OP used the terms "refering" and "reference" in regards to an int[] (elements of int[][]) -- and in that context, "reference" is correct.

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 47310
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right Henry. I didn’t notice that. Well done noticing, and I am sorry about my mistake.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic