• Post Reply Bookmark Topic Watch Topic
  • New Topic

A simple Array Problem  RSS feed

 
Sivaraman Lakshmanan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai all,
I have a simple array problem. I am not sure where I am going wrong. please review my code and correct me.
my code is:

Here in the above code when I print for the first time (Printing -1) I get the values of the array correctly. but when I print for the second time this same array (printing - 2) all the vales expect for the last row is null.
I am give the out put what I got below

atleast if everything comes as null, its fine I am not sure how I am getting the values for the last row...
please help me to sort out the problem.
Thanks in advance.
Sivaraman.L
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sivaraman Lakshmanan:



shouldn't it be:
 
Sivaraman Lakshmanan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Wagner,
even though I give the array index as u told.. The output is the same.
siva
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sivaraman
I have made a couple of changes to fix your code and a couple of additions to test it:
I moved the strMenucnt declaration outside of the for loop as you were overriding each instance with the next instance of the main lhs subscript. I left the declaration of the rhs subscript in the for loop. I also changed the lhs subscript in your print loop to match the actual element since my test had different numbers of elements for different rows.
 
Sivaraman Lakshmanan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Eddie Vanda,
you have really solved the problem the big headache I had. I was struggling for the past 3 days to solve the problem. thanks you very much.
can u sum up and tell me what actually went wrong in my code, so that I will be carful in my future.
Once again Thanks for your excellent answer.
regards,
sivaraman.L
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sivaraman,
It's a pleasure. I think the main problem is that two dimensional arrays in java do not follow your traditional mathematical concept or c's layout.
The left hand side (lhs) subscript could be viewed as a row of single subscript array holders. That's the bit that got overridden by each iteration of your inner for loop, so only the last row was valid. The "item" array was then added to each of your row (lhs) holders. I just split the array declaration so that rows were declared outside that loop.
You could think of the right hand side (rhs) subscript as being the columns of your array. Each is a separate single dimension array in its own right.
Good luck.
Ed
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!