使用Java/Guava可选<;T>;类来创建和处理对象的矩阵
我正在考虑使用Guava库中可选的使用Java/Guava可选<;T>;类来创建和处理对象的矩阵,java,multidimensional-array,matrix,guava,Java,Multidimensional Array,Matrix,Guava,我正在考虑使用Guava库中可选的类来处理对象的矩阵(2D网格),同时避免使用空引用来表示空单元格 我正在做一些类似于: class MatrixOfObjects() { private Optional<MyObjectClass>[][] map_use; public MatrixOfObjects(Integer nRows, Integer nCols) { map_use = (Optional<MyObjectClass>
class MatrixOfObjects() {
private Optional<MyObjectClass>[][] map_use;
public MatrixOfObjects(Integer nRows, Integer nCols) {
map_use = (Optional<MyObjectClass>[][]) new Optional[nRows][nCols];
// IS THIS CAST THE ONLY WAY TO CRETE THE map_use INSTANCE?
}
public MyObjectClass getCellContents(Integer row, Integer col) {
return map_use[row][col].get();
}
public void setCellContents(MyObjectClass e, Integer row, Integer col) {
return map_use[row][col].of(e);
// IS THIS THE CORRECT USE OF .OF METHOD?
}
public void emptyCellContents(Integer row, Integer col) {
map_use[row][col].set(Optional.absent());
// BUT SET() METHOD DOES NOT EXIST....
}
public Boolean isCellUsed(Integer row, Integer col) {
return map_use[row][col].isPresent();
}
}
类MatrixOfObjects(){
私有可选[][]地图使用;
公共矩阵对象(整数nRows、整数NCOL){
map_use=(可选[][])新可选[nRows][nCols];
//此强制转换是创建map_use实例的唯一方法吗?
}
公共MyObjectClass getCellContents(整数行、整数列){
返回map_使用[row][col].get();
}
public void setCellContents(MyObject类e,整数行,整数列){
返回(e)的[行][列];
//这是正确使用的方法吗?
}
public void emptyCellContents(整数行、整数列){
map_使用[row][col].set(可选。缺席());
//但是SET()方法不存在。。。。
}
使用公共布尔值(整数行、整数列){
返回映射\使用[row][col].isPresent();
}
}
关于上述代码,我有三个问题:
谢谢我修复了代码中的一些错误,并添加了注释来解释:
class MatrixOfObjects { // class declaration should not have a "()"
private Optional<MyObjectClass>[][] map_use;
public MatrixOfObjects(Integer nRows, Integer nCols) {
map_use = (Optional<MyObjectClass>[][]) new Optional[nRows][nCols];
}
public MyObjectClass getCellContents(Integer row, Integer col) {
return map_use[row][col].get();
}
public void setCellContents(MyObjectClass e, Integer row, Integer col) {
// removed "return" keyword, since you don't return anything from this method
// used correct array assignement + Optional.of() to create the Optional
map_use[row][col] = Optional.of(e);
}
public void emptyCellContents(Integer row, Integer col) {
// unlike lists, arrays do not have a "set()" method. You have to use standard array assignment
map_use[row][col] = Optional.absent();
}
public Boolean isCellUsed(Integer row, Integer col) {
return map_use[row][col].isPresent();
}
}
类MatrixOfObjects{//类声明不应包含“()”
私有可选[][]地图使用;
公共矩阵对象(整数nRows、整数NCOL){
map_use=(可选[][])新可选[nRows][nCols];
}
公共MyObjectClass getCellContents(整数行、整数列){
返回map_使用[row][col].get();
}
public void setCellContents(MyObject类e,整数行,整数列){
//删除了“return”关键字,因为您不从此方法返回任何内容
//使用正确的数组赋值+Optional.of()创建可选
map_use[行][列]=可选的(e);
}
public void emptyCellContents(整数行、整数列){
//与列表不同,数组没有“set()”方法。必须使用标准数组赋值
map_use[row][col]=可选。缺席();
}
使用公共布尔值(整数行、整数列){
返回映射\使用[row][col].isPresent();
}
}
以下是创建通用数组的几个备选方案:
请注意,如果您对Java如何处理泛型没有很好的理解,那么很难同时使用数组和泛型。使用集合通常是更好的方法
综上所述,我将使用Guava的表接口而不是“MatrixOfObjects”类。我修复了代码中的一些错误,并添加了注释来解释:
class MatrixOfObjects { // class declaration should not have a "()"
private Optional<MyObjectClass>[][] map_use;
public MatrixOfObjects(Integer nRows, Integer nCols) {
map_use = (Optional<MyObjectClass>[][]) new Optional[nRows][nCols];
}
public MyObjectClass getCellContents(Integer row, Integer col) {
return map_use[row][col].get();
}
public void setCellContents(MyObjectClass e, Integer row, Integer col) {
// removed "return" keyword, since you don't return anything from this method
// used correct array assignement + Optional.of() to create the Optional
map_use[row][col] = Optional.of(e);
}
public void emptyCellContents(Integer row, Integer col) {
// unlike lists, arrays do not have a "set()" method. You have to use standard array assignment
map_use[row][col] = Optional.absent();
}
public Boolean isCellUsed(Integer row, Integer col) {
return map_use[row][col].isPresent();
}
}
类MatrixOfObjects{//类声明不应包含“()”
私有可选[][]地图使用;
公共矩阵对象(整数nRows、整数NCOL){
map_use=(可选[][])新可选[nRows][nCols];
}
公共MyObjectClass getCellContents(整数行、整数列){
返回map_使用[row][col].get();
}
public void setCellContents(MyObject类e,整数行,整数列){
//删除了“return”关键字,因为您不从此方法返回任何内容
//使用正确的数组赋值+Optional.of()创建可选
map_use[行][列]=可选的(e);
}
public void emptyCellContents(整数行、整数列){
//与列表不同,数组没有“set()”方法。必须使用标准数组赋值
map_use[row][col]=可选。缺席();
}
使用公共布尔值(整数行、整数列){
返回映射\使用[row][col].isPresent();
}
}
以下是创建通用数组的几个备选方案:
请注意,如果您对Java如何处理泛型没有很好的理解,那么很难同时使用数组和泛型。使用集合通常是更好的方法
综上所述,我将使用Guava的表格界面,而不是您的“MatrixOfObjects”类。谢谢!,我将继续使用数组,因为a类似于方括号索引。。。有一天我可能不得不回去更新表格,但由于某种原因,我还是一如既往地喜欢相信我不会……:)谢谢我将继续使用数组,因为a类似于方括号索引。。。有一天我可能不得不回去更新表格,但由于某种原因,我还是一如既往地喜欢相信我不会……:)