Multi-value arrays of arrays (matrices)
As an alternative to multi-valued arrays of arrays in Java, a Map or single array of arrays of custom objects can be utilized to get the same behavior. By using a Map you get sparse data storage improvement over arrays and maintain constant time access. If the data isn’t sparse, this mechanism would use more memory proportional to the number of elements. Iteration time is capacity proportional which is slightly inferior to an arrays constant time. Also, array copying requires a small amount of additional custom code to duplicate the Elements, i.e., a copy constructor.
This approach is simpler than using multiple arrays since it minimizes the indexing to a single implementation vs. an indexing implementation for each array type.
If it is necessary to return some particular values when no data exists, decorate or extend the Map or the Element class and provide the custom behavior.
For Map base implementation utilize the “x” + “y” indices as the id for equals and key for hashCode. Create a custom Map/Decorator accessor or static accessor for computing the key to keep that implementation centralized and abstracted.
This approach is simpler than using multiple arrays since it minimizes the indexing to a single implementation vs. an indexing implementation for each array type.
If it is necessary to return some particular values when no data exists, decorate or extend the Map or the Element class and provide the custom behavior.
For Map base implementation utilize the “x” + “y” indices as the id for equals and key for hashCode. Create a custom Map/Decorator accessor or static accessor for computing the key to keep that implementation centralized and abstracted.
/**
* Immutable ID/key & hashCode.
* Ideally Element would be an inner class of a custom class encapsulating the Map
* and providing access to the individual data elements via x and y.
* Since Element would have custom getters, the access can be chained,
* i.e., someStorage.get(x, y).getS1();
*/
public static class Element {
final int x;
final int y;
final String id; //cached for performance
final int hashCode; //cached for performance
byte b1;
byte b2;
int i
String s1;
String s2;
public Element(final int x, final int y) {
this.x= x;
this.y = y;
this.id = getId(x, y);
hashCode = id.hashCode();
}
/**
* Single id algorithm instance.
*/
private String getId(final int x, final int y) {
return String.format(“%d,%d”, x, y);
}
/**
* Ideally this would be part of the custom storage implementation containing the Map.
*/
public static Element getElement(final int x, final int y, final Mapelements) {
return elements.get(getId(x, y));
}
@Override
public String toString() {
return id;
//and other data as desired
}
/**
* Shallow equals
*/
@Override
public boolean equals(final Object other) {
if (this == other) return true;
if (!(other instanceof Element)) return false;
return id.equals(((Element)other).id);
}
@Override
public int hashCode() {
return hashCode;
}
}
Comments