Hello. I have a method like this:
public void p(Integer a, Integer b, Integer c)
{
...
}
b and c should be output data and a input.
But I don't know how to assing a value to b and c inside p without
changin the objet where they point.
That is:
public void p(X a, Integer b, Integer c)
{
...
b = doSomeThing(a);
c = doSomeThing2(a);
}
That doesn't make b and c to point to the result of doSomeThing(a) when
p ends.
What can I do?
Why not use some Pair class?
import java.io.Serializable;
/**
* convenience class for pairs of values. This class provides basic type
* safety, and can be used as a {@link java.util.Map}-key, and similar.
*
* Note that Serialization can only occur successfully if the type parameters T1
* and T2 also implement the {@link java.io.Serializable} interface.
*
*/
public class Pair<T1, T2> implements Serializable, Cloneable {
private static final long serialVersionUID = 3690473610825577009L;
private final T1 first;
private final T2 second;
/**
* creates a new Pair of values.
* @param first first value
* @param second second value
*/
public Pair(T1 first, T2 second) {
this.first = first;
this.second = second;
}
/**
* retrieves the first value of pair.
*
* @return the first value
*/
public T1 getFirst() {
return first;
}
/**
* retrieves the second value of pair.
*
* @return the second value
*/
public T2 getSecond() {
return second;
}
/**
* computes the hashcode for this object. This implementation is
* consistent with equals, if the member objects have hashcodes that
* are consistent with equals. It handles null-values gracefully.
*
* @return the hashcode
*/
@Override
public int hashCode(){
int firstCode = first != null ? first.hashCode() : 0;
int secondCode = second != null ? second.hashCode() >> 1 : 0;
return firstCode ^ secondCode;
}
/**
* compares two objects of class Pair for equality. A pair is equal if
* it's members either fulfill reference equality (==) or if their
* equals(Object) methods both return true.
*
* @param o another object
* @return true if the pairs are equal
*/
@Override
public boolean equals(Object o){
if (o == this)
return true;
if (o == null)
return false;
if (o instanceof Pair){
Pair<?,?> that = (Pair<?,?>) o;
Object thatFirst = that.getFirst();
Object thatSecond = that.getSecond();
if (thatFirst != first){
if (first == null || !first.equals(thatFirst))
return false;
}
if (thatSecond != second){
if (second == null || !second.equals(thatSecond))
return false;
}
return true;
}
return false;
}
/**
* returns a shallow copy of this Pair. The copy may be quicker
* then the obvious new Pair<T1, T2>(this.getFirst(), this.getSecond())
* method of copying.
*
* @return a copy of this object.
*/
@Override
@SuppressWarnings("unchecked")
public Pair<T1, T2> clone(){
try {
return (Pair<T1, T2>) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError(e);
}
}
}