Source of MutableInteger.java


  1: //: appendixa:MutableInteger.java
  2: // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
  3: // www.BruceEckel.com. See copyright notice in CopyRight.txt.
  4: // A changeable wrapper class.
  5: import java.util.*;

  7: class IntValue { 
  8:   int n;
  9:   IntValue(int x) { n = x; }
 10:   public String toString() { 
 11:     return Integer.toString(n);
 12:   }
 13: }

 15: public class MutableInteger {
 16:   public static void main(String[] args) {
 17:     ArrayList v = new ArrayList();
 18:     for(int i = 0; i < 10; i++) 
 19:       v.add(new IntValue(i));
 20:     System.out.println(v);
 21:     for(int i = 0; i < v.size(); i++)
 22:       ((IntValue)v.get(i)).n++;
 23:     System.out.println(v);
 24:   }
 25: } ///:~