In Google Guava (Java), how to bulk set values for ArrayTable?

后端 未结 2 626
忘掉有多难
忘掉有多难 2021-01-23 23:11

I have a two dimensional array of data, e.g., V[][], that I want to bulk set on an ArrayTable instance.

Must I repeatedly call ArrayTable.put(R rowKey

相关标签:
2条回答
  • 2021-01-23 23:46

    There is not such a constructor.

    Your best option is:

    ArrayTable table = ArrayTable.create(
        ContiguousSet.create(Range.closedOpen(0, v.length), 
                           DiscreteDomain.integers()), 
        ContiguousSet.create(Range.closedOpen(0, v[0].length), 
                           DiscreteDomain.integers()));
    for(int i = 0 ; i < v.length ; ++i) {
        for(int j = 0 ; j < v[i].length ; ++j) {
            table.set(i, j, v[i][j]);
        }
    }
    
    0 讨论(0)
  • 2021-01-23 23:54

    There is no help in guava, as @JBNizet say, because the library can't know what you want to use as keys.

    You can use 2 nested loops as @JBNizet say or you can create your own Table implementation that wraps your array and then call putAll method to copy your new table to the ArrayTable.

    You don't need to implement every method on Table, you may throw UnsupportedOperationException on methods that otherwise should alter the content.

    0 讨论(0)
提交回复
热议问题