使用Guava对矩阵的实现, 做一些矩阵存贮等操作将大大提高效率, 而不是自己用JDK的Array去实现, 可能带来不少bug, 而且增加代码复杂度。
package com.wenniuwuren.collections; import java.util.Map; import com.google.common.collect.HashBasedTable; /** * Table: 矩阵的实现 * @author wenniuwuren * */ public class TableTest { public static void main(String[] args) { HashBasedTable<Integer,Integer,String> table = HashBasedTable.create(); table.put(1, 1, "11"); table.put(1, 2, "12"); table.put(1, 3, "13"); table.put(2, 1, "21"); table.put(2, 2, "22"); table.put(2, 3, "23"); boolean contains11 = table.contains(1,1); boolean containColumn2 = table.containsColumn(2); boolean containsRow1 = table.containsRow(1); boolean containsValue11 = table.containsValue("11"); table.remove(1,3); table.get(3,4); Map<Integer,String> columnMap = table.column(1); Map<Integer,String> rowMap = table.row(2); System.out.println(table + ", contains11:" + contains11 + ", containColumn2:" + containColumn2 + ", containsRow1:" + containsRow1 + ", containsValue11:" + containsValue11 + ", columnMap" + columnMap +", rowMap" + rowMap); } }
参考资料:
《Getting Started with Google Guava》
时间: 2024-11-25 15:25:17