Java Reference
In-Depth Information
single value (in this case, return the offset in the return statement, and let the user know that
-1
indicates not found), or define a trivial wrapper class containing both the integer and the
Boolean. However, there is precedent in the standard API: this code is remarkably similar to
how the
ParsePosition
class is used. Anyway, this functionality is requested often enough
that I feel justified in showing how to do it, accompanied by this disclaimer: try to avoid do-
ing it this way in new code!
Having said all that, here is the
MutableInteger
class:
com/darwinsys/lang/MutableInteger.java
package
package
com
.
darwinsys
.
lang
;
/** A MutableInteger is like an Integer but mutable, to avoid the
* excess object creation involved in
* c = new Integer(c.getInt()+1)
* which can get expensive if done a lot.
* Not subclassed from Integer, since Integer is final (for performance :-))
*/
public
public class
class
MutableInteger
MutableInteger
{
private
private
int
int
value
=
0
;
public
public
MutableInteger
(
int
int
i
) {
value
=
i
;
}
public
public
MutableInteger
() {
this
this
(
0
);
}
public
public
int
int
incr
() {
value
++;
return
return
value
;
}
public
public
int
int
amt
) {
value
+=
amt
;
return
int
incr
(
int
return
value
;
}
public
public
int
int
decr
() {
value
--;
return
return
value
;
}