Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array, Array who gets you?  RSS feed

 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there!

I've got this one dimensional array:



and than I do stuff like this with it:



No problem. But than I go on on doing a small trick:



Thank I think it would be nice to admire what I've don, so I go on printing the array out:



and, to my surprise I get result:
10 20 30 40

WHY???

wouldn't have I destroyed the previous array od int[2] with values 10 and 20 by calling new int[4]???

Your insight as always will be greatly appreciated.
 
Dave Tolls
Rancher
Posts: 2911
35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to show your full code.
Something we could actually run.
Because this:

gives the expected result:
0
0
30
40
Though I did have to fix a type on the first new int[2].
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read these Use A Meaningful Subject Line and Post Real Code
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that is the whole code.

you can dress it in a class if you like but that's all there is.



I suppose I am right and whoever wrote the test has no clue. I see no way there could be any other result as 0 0 30 40 .

Thanks.
 
Priyavrat Kumar
Greenhorn
Posts: 27
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output is-
0  0  30  40
 
Piet Souris
Rancher
Posts: 1979
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:Please read these Use A Meaningful Subject Line and Post Real Code

Well, the subject IS about arrays, and the title was so inviting, it drew my attention. So, I think the title is perfect.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
Ganish Patil wrote:Please read these Use A Meaningful Subject Line and Post Real Code

Well, the subject IS about arrays, and the title was so inviting, it drew my attention. So, I think the title is perfect.

Use A Meaningful Subject Line wrote:A subject like "Problem with Swing" or "JPanel" doesn't motivate many people to take a closer look, whereas "difference between Component and Container" (hopefully posted in the Swing forum) will attract exactly those people who can - and like to - answer
I felt, Array showing strange output or Array not showing correct output or Array output problem or Previous array reference not assigning new array reference? or Reassignment of  array reference not working ?  these would have been more expressive so anyone who is having trouble related array output then they can check this thread. I felt subject line should be useful not only to the people who wants to answer but also who searches for similar question when they get similar kind of problems, here array output.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!