【问题标题】:Get Value at Specific Index using Row and Column使用行和列获取特定索引处的值
【发布时间】:2018-04-01 03:11:24
【问题描述】:

我有两个价值观:

private ArrayList<DataEntry> data;
private String title;

类内DataEntry 是:

private int nRow, nCol;
private double[] data;

我必须在给定的rowcolumn(如果有)、null 处返回项目的值,否则使用下面的函数:

public Double get(int row, int column) { }

我了解公式:data[(row * numberOfColumns) + Columns] 提供了非二维普通数组中的索引,但我不确定如何为an ArrayList 执行此操作,有什么建议吗?

【问题讨论】:

    标签: java arrays arraylist


    【解决方案1】:

    ArrayList 几乎完全相同,只需使用.get 语法:data.get((row * numberOfColumns) + Columns)

    基本上您可以将.get(int index) 视为等同于数组下标[index] 表示法用于索引目的。

    https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)

    但是,有一些注意事项,例如您可以使用 ArrayList 获取 null,因为它是对象的集合

    【讨论】:

    • 感谢您的建议,但如果尚未定义数组列表中的列数,您将如何准确找到它?
    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    相关资源
    最近更新 更多