Java 多维数组列表和整数替换

Java 多维数组列表和整数替换,java,arrays,get,arraylist,Java,Arrays,Get,Arraylist,通过一个二维ArrayList的最佳方法是什么(或者说,实际上是什么),对于每一个Int等于1的值,你就离开它,否则就从中减去1。 i、 e.如果arrayList.get(i).get(j)=3它现在将是2等等,但是如果它是1,它将停留在arrayList的特定列中,只有一种方法可以做到这一点。迭代所有行中的所有列: // example declaration only - initially all zeros until you set them. // assumes nrows an

通过一个二维
ArrayList
的最佳方法是什么(或者说,实际上是什么),对于每一个
Int
等于
1
的值,你就离开它,否则就从中减去
1

i、 e.
如果arrayList.get(i).get(j)=3
它现在将是
2
等等,但是如果它是
1
,它将停留在
arrayList
的特定列中,只有一种方法可以做到这一点。迭代所有行中的所有列:

// 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;
        }
    }
}
//仅示例声明-最初为全零,直到您设置它们为止。
//假设nrows和NCOL在别处初始化和声明
int[][]矩阵=新的int[nrows][ncols];
对于(int i=0;i
如果您有一个列表,它如下所示:

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);
        }
    }
}
List matrix=new ArrayList();
List columnIdsToTransform=Arrays.asList({0,4,6});
//您必须初始化引用;现在所有的都是空的。
对于(列表行:矩阵){
对于(int j=0;j
更新:


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

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

// 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;
        }
    }
}
//仅示例声明-最初为全零,直到您设置它们为止。
//假设nrows和NCOL在别处初始化和声明
int[][]矩阵=新的int[nrows][ncols];
对于(int i=0;i
如果您有一个列表,它如下所示:

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);
        }
    }
}
List matrix=new ArrayList();
List columnIdsToTransform=Arrays.asList({0,4,6});
//您必须初始化引用;现在所有的都是空的。
对于(列表行:矩阵){
对于(int j=0;j
更新:


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

迭代所有值,并使用您的条件语句对
true
案例执行操作

for (int i=0;i<Arraylist.size();i++) {
    for (int j=0;j<Arraylist[i].size();j++) {    
         if (arrayList.get(i).get(j) != 1) 
             arrayList.get(i).get(j) -= 1;         
    } 
}

for(int i=0;i迭代所有值,并使用您的条件语句对
true
案例执行操作

for (int i=0;i<Arraylist.size();i++) {
    for (int j=0;j<Arraylist[i].size();j++) {    
         if (arrayList.get(i).get(j) != 1) 
             arrayList.get(i).get(j) -= 1;         
    } 
}

for(int i=0;iSorry duffymo刚刚编辑了这个问题,因为我在发布它时显然有一个脑屁,谢谢!抱歉duffymo刚刚编辑了这个问题,因为我在发布它时显然有一个脑屁,谢谢!