-1

I came across THIS, but it is too elaborate.

Can someone please suggest how to write a easy and less comprehensive mutable integer wrapper? Or even pointers/steps will help

Why I need a mutable integer? For THIS :

Community
  • 1
  • 1
Andy897
  • 6,915
  • 11
  • 51
  • 86
  • It would probably help if you shared more information about your use case. Why can you not use a primitive value? – thatidiotguy Feb 04 '15 at 18:09
  • 1
    http://stackoverflow.com/questions/28326915/right-view-of-binary-tree-without-using-queue-in-java/28327774?noredirect=1#comment45004023_28327774 – Andy897 Feb 04 '15 at 18:10
  • Why don't you pick and choose the methods you want to use? – Luminous Feb 04 '15 at 18:18

1 Answers1

2

Your question is somewhat abstract. From the other question that you've referenced the issue with your algorithm is that you're attempting to increment a value, however the value is within a method. As Java is pass by value, not pass by reference, the value is mutated within the method but the original value that you've passed into your method (reference) is not touched.

In this case you can make the integer value mutable by just wrapping it within a class, and using getters / setters to modify the value. This is done using 'AtomicInteger' in the previous answer; a simple example of how that's implemented (which is by no means thread-safe or optimal):

public class MutableInt {

    private int value;

    public MutableInt(int value) { this.value = value; }

    public void setValue(int value) { this.value = value; }
    public int  getValue() { return this.value; }
}
chrisburke.io
  • 1,497
  • 2
  • 17
  • 26