java - 多维ArrayList和整数替换

标签 java arrays get arraylist

经历二维的最好的(或者无论如何,真的)是什么ArrayList<ArrayList<Integer>>对于每个 Int等于 1你保留它,否则你减去 1从中。 if arrayList.get(i).get(j) == 3现在将是 2依此类推,但如果是 1它保持1 ) 仅在 ARRAYLIST<ARRAYLIST<INTEGER>> 的特定列中.

最佳答案

拿把铲子 - 只有一种方法可以做到这一点。迭代所有行中的所有列:

// example declaration only - initially all zeros until you set them.
// assumes nrows and ncols are initialized and declared elsewhere
int [][] matrix = new int[nrows][ncols];
for (int i = 0; i < matrix.length; ++i) {
    for (int j = 0; j < matrix[i].length; ++j) {
        // operate on the values here.
        if (matrix[i][j] != 1) {
            matrix[i][j] -= 1;
        }
    }
}

如果你有一个列表列表,它看起来像这样:

List<List<Integer>> matrix = new ArrayList<List<Integer>>();
List<Integer> columnIdsToTransform = Arrays.asList({0, 4, 6 });
// You have to initialize the references; all are null right now.
for (List<Integer> row : matrix) {
    for (int j = 0; j < row.size(); ++j) {            
        // operate on the values here.
        value = row.get(j);
        if (columnsIdsToTransform.contains(j) && (value != 1)) {
            row.set(value-1, j);
        }
    }
}

更新:

根据您的编辑,您应该添加要执行此转换的数组或列列表。我已在第二个片段中添加了一个示例。

关于java - 多维ArrayList和整数替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9089752/

相关文章:

java - ColdFusion 中 java.util.ArrayList 的用法

ruby-on-rails - Rails4 + 回形针 : Url and Path not matching

java - 监听套接字请求的 EAR (Java EE) 应用程序

java - 在/WEB-INF/lib中使用JBDC 4.0驱动程序,但仍然需要Class#forName()来加载它

java - 使用 Spring Boot 设置 DynamoDB

java - Android 我怎样才能制作这个 EditText 的渐变

jquery 打印出数组值并更改其 CSS

php - PHP 数组中的数据库数据

c# - 如何根据内部 json 对象值在 DocumentDB 中查询?

php - 如何使用 jQuery 从 php 文件中获取某些内容?