2015-09-07 11:44:36 -06:00
|
|
|
|
// references/MutableInteger.java
|
2015-06-15 17:47:35 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
|
|
|
|
// A changeable wrapper class.
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
class IntValue {
|
|
|
|
|
private int n;
|
|
|
|
|
public IntValue(int x) { n = x; }
|
|
|
|
|
public int getValue() { return n; }
|
|
|
|
|
public void setValue(int n) { this.n = n; }
|
|
|
|
|
public void increment() { n++; }
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return Integer.toString(n);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MutableInteger {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
List<IntValue> v = new ArrayList<>();
|
|
|
|
|
for(int i = 0; i < 10; i++)
|
|
|
|
|
v.add(new IntValue(i));
|
|
|
|
|
System.out.println(v);
|
|
|
|
|
for(int i = 0; i < v.size(); i++)
|
|
|
|
|
v.get(i).increment();
|
|
|
|
|
System.out.println(v);
|
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
|
}
|
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
2015-09-07 11:44:36 -06:00
|
|
|
|
*/
|