OnJava8-Examples/references/MutableInteger.java

31 lines
740 B
Java
Raw Normal View History

2015-05-05 11:20:13 -07:00
//: references/MutableInteger.java
// 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
2015-05-18 23:05:20 -07:00
public String toString() {
return Integer.toString(n);
}
2015-05-05 11:20:13 -07:00
}
public class MutableInteger {
public static void main(String[] args) {
2015-05-06 15:14:33 -07:00
List<IntValue> v = new ArrayList<>();
2015-05-05 11:20:13 -07:00
for(int i = 0; i < 10; i++)
v.add(new IntValue(i));
System.out.println(v);
for(int i = 0; i < v.size(); i++)
2015-05-06 15:14:33 -07:00
v.get(i).increment();
2015-05-05 11:20:13 -07:00
System.out.println(v);
}
2015-05-18 23:05:20 -07:00
} /* Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*///:~