<p>I have a recursive algorithm which steps through a string, character by character, and parses it to create a tree-like structure.  I want to be able to keep track of the character index the parser is currently at (for error messages as much as anything else) but am not keen on implementing something like a tuple to handle multiple returned types.</p>

<p>I tried using an Integer type, declared outside the method and passed into the recursive method, but because it's final, recursive call increments are "forgotten" when I return. (Because the increment of the Integer value makes the passed-by-value object reference point at a new object)</p>

<p>Is there a way to get something similar to work which won't pollute my code?</p>

[{'answer_id': 34678, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 2, 'selected': False, 'text': "<p>It's kind of a hack, but sometimes I use an AtomicInteger, which is mutable, to do things like this.  I've also seen cases where an int[] of size 1 is passed in.</p>\n"}, {'answer_id': 34689, 'author': 'Andrew Harmel-Law', 'author_id': 2455, 'author_profile': 'https://Stackoverflow.com/users/2455', 'pm_score': 2, 'selected': False, 'text': '<p>The current solution I am using is:</p>\n\n<pre><code>int[] counter = {0};\n</code></pre>\n\n<p>and then pass it to the recursive algorithm:</p>\n\n<pre><code>public List&lt;Thing&gt; doIt (String aString, int[] counter) { ... }\n</code></pre>\n\n<p>and when I want to increment it:</p>\n\n<pre><code>counter[0]++;\n</code></pre>\n\n<p>Not super elegant, but it works...</p>\n'}, {'answer_id': 34700, 'author': 'levand', 'author_id': 3044, 'author_profile': 'https://Stackoverflow.com/users/3044', 'pm_score': 2, 'selected': False, 'text': '<p>Integers are immutable, which means that when you pass it as an argument it creates a copy rather than a reference to the same item. (<a href="http://www.javaworld.com/javaworld/javaqa/2000-06/01-qa-0602-immutable.html" rel="nofollow noreferrer">explanation</a>).</p>\n\n<p>To get the behavior you\'re looking for, you can write your own class which is like Integer only mutable. Then, just pass it to the recursive function, it is incremented within the recursion, and when you access it again after the recursion is over it will still maintain its new values.</p>\n\n<p>Edit: Note that using an int[] array is a variation on this method... In Java, arrays are also passed by reference rather than copied like primitives or immutable classes.</p>\n'}, {'answer_id': 34716, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 3, 'selected': True, 'text': '<p>Since you\'ve already discovered the pseudo-mutable integer "hack," how about this option:</p>\n\n<p>Does it make sense for you to make a separate Parser class?  If you do this, you can store the current state in a member variable.  You probably need to think about how you\'re going to handle any thread safety issues, and it might be overkill for this particular application, but it might work for you.</p>\n'}, {'answer_id': 34740, 'author': 'ejack', 'author_id': 1377, 'author_profile': 'https://Stackoverflow.com/users/1377', 'pm_score': 0, 'selected': False, 'text': '<p>To be honest I would recode the function to make it a linear algorithm that uses a loop. This way you have no chance of running out of heap space if you are stepping through an extremely large string. Also, you would not need to have a the extra parameter just to keep track of the count.</p>\n\n<p>This also would probably have the result of making the algorithm faster because it does not need to make a function call for every character.</p>\n\n<p>Unless of course there is a specific reason it needs to be recursive.</p>\n'}, {'answer_id': 34856, 'author': 'Jason Day', 'author_id': 737, 'author_profile': 'https://Stackoverflow.com/users/737', 'pm_score': 0, 'selected': False, 'text': '<p>One possibility I can think of is to store the count in a member variable of the class.  This of course assumes that the public <code>doIt</code> method is only called by a single thread.</p>\n\n<p>Another option is to refactor the public method to call a private helper method.  The private method takes the list as a parameter and returns the count.  For example:</p>\n\n<pre><code>public List&lt;Thing&gt; doIt(String aString) {\n    List&lt;Thing&gt; list = new ArrayList&lt;Thing&gt;();\n    int count = doItHelper(aString, list, 0);\n    // ...\n    return list;\n}\n\nprivate int doItHelper(String aString, List&lt;Thing&gt; list, int count) {\n    // ...\n    // do something that updates count\n    count = doItHelper(aString, list, count);\n    // ...\n    return count;\n}\n</code></pre>\n\n<p>This assumes that you can do the error handling in the public <code>doIt</code> method, since the <code>count</code> variable isn\'t actually passed back to the caller.  If you need to do that, you could of course throw an exception:</p>\n\n<pre><code>public List&lt;Thing&gt; doIt(String aString) throws SomeCustomException {\n    List&lt;Thing&gt; list = new ArrayList&lt;Thing&gt;();\n    int count = doItHelper(aString, list, 0);\n    // ...\n    if (someErrorOccurred) {\n        throw new SomeCustomException("Error occurred at chracter index " + count, count);\n    }\n    return list;\n}\n</code></pre>\n\n<p>It\'s difficult to know whether that will help without knowing more about how your algorithm actually works.</p>\n'}, {'answer_id': 34890, 'author': 'Bill the Lizard', 'author_id': 1288, 'author_profile': 'https://Stackoverflow.com/users/1288', 'pm_score': 1, 'selected': False, 'text': '<p>You could just use a static int class variable that gets incremented each time your doIt method is called.</p>\n'}, {'answer_id': 9588103, 'author': 'Jonny', 'author_id': 232206, 'author_profile': 'https://Stackoverflow.com/users/232206', 'pm_score': 1, 'selected': False, 'text': '<p>You could also do:</p>\n\n<pre><code>private int recurse (int i) {\n\n    if (someConditionkeepOnGoing) {\n        i = recurse(i+1);\n    }\n\n    return i;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/34669', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2455/']