Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

forcing pass by value?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a recursive method in my program. Its main argument is a Vector, which contains a list of indices the recursion has visited (an element is appended to the Vector in each call to the method). Obviously, as its recursion, I need the elements in the Vector to change "automatically" as the recursion back-tracks up the stack. However, as Java passes objects by reference (kind of), this isn't happening.
I would like to know if there is any way of forcing Java to pass the Vector by value, or if I should add a line somewhere in the method to manually remove the last element in the Vector (although I don't know where) to simulate the recursion stack, or if using a normal array (of ints) would work (but the problem with this is the extra memory/time required to copy the array when deleting/adding elements from/to it).
Which is the best solution?
thanks,
Graham.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure exactly what you are doing, but if I was using a vector for recursion like this I would use element zero to store the index of the last element in the array. That element zero would point to an object encapsulating an int which could be incremented and decremented (unlike an Integer wrapper class). As I descended the recursion I would increment the index and as I ascended I would decrement the thing.
If you can give a simplified version of your code, I guess someone will suggest something suitable.
-Barry
 
Graham Clark
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, here's a simplified version of my recursive method. I'm using the elements in the local Vector to update cells in a matrix: the cell matrix[a constant][last element in Vector] will be set to 1, matrix[a constant][last element but one in the Vector] will be set to 2, and so on.

I'm not sure I understand your suggestion - would the array contain ints or this Integer wrapper?
thanks for any more help.
Graham.
[ September 05, 2002: Message edited by: Graham Clark ]
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic