If I have a method that accepts an int[][] , a row number to remove, and a column number to remove, how would I remove that specific row and column from the Array and return the new reduced Array?
如果我有一个接受int[][][]、要删除的行号和要删除的列号的方法,我如何从数组中删除特定的行和列并返回新的减少的数组?
I want to do it by taking everything except the row/column I want to remove and then putting it into two temporary ArrayLists, then constructing a new Array to return from the values in the two Arrays. I think I can remove a specific row just fine, however I don't know how to remove the column as well.
我想要做的是除去我想要删除的行/列,然后将它放入两个临时的arraylist中,然后构造一个新的数组,从两个数组中的值返回。我认为我可以删除特定的行,但是我也不知道如何删除列。
2 个解决方案
#1
5
I think the best approach is create a new array of
我认为最好的方法是创建一个新的数组
int[xsize-1][ysize-1]
int[xsize-1][ysize-1]
Have a nested for loop to copy from source array to destination. And skip for a specific i and j
有一个嵌套的for循环从源数组复制到目标。跳过特定的i和j
static void TestFunction()
{
int rows = 5;
int columns = 6;
int sourcearr[][] = new int[rows][columns];
int destinationarr[][] = new int[rows-1][columns-1];
int REMOVE_ROW = 2;
int REMOVE_COLUMN = 3;
int p = 0;
for( int i = 0; i < rows; ++i)
{
if ( i == REMOVE_ROW)
continue;
int q = 0;
for( int j = 0; j < columns; ++j)
{
if ( j == REMOVE_COLUMN)
continue;
destinationarr[p][q] = sourcearr[i][j];
++q;
}
++p;
}
}
#2
0
I'm trying to remove both 3 in two columns, under the index 2, how to achieve it?
我试着在索引2的两列中删除这三个,如何实现它?
public class For_petlja {
public static void main(String[] args) {
int[][] niz = {
{9, 2, 3, 6, 7},
{4, 7, 3, 5, 1}
};
int j;
for (int i = 0; i < niz.length; i++) {
for (j = 0; j < niz[i].length; j++) {
if (i == 2) {
continue;
}
} // for 'j'
System.out.println(Arrays.toString(niz[j]));
} // for 'i'
} // main method.
} // class
#1
5
I think the best approach is create a new array of
我认为最好的方法是创建一个新的数组
int[xsize-1][ysize-1]
int[xsize-1][ysize-1]
Have a nested for loop to copy from source array to destination. And skip for a specific i and j
有一个嵌套的for循环从源数组复制到目标。跳过特定的i和j
static void TestFunction()
{
int rows = 5;
int columns = 6;
int sourcearr[][] = new int[rows][columns];
int destinationarr[][] = new int[rows-1][columns-1];
int REMOVE_ROW = 2;
int REMOVE_COLUMN = 3;
int p = 0;
for( int i = 0; i < rows; ++i)
{
if ( i == REMOVE_ROW)
continue;
int q = 0;
for( int j = 0; j < columns; ++j)
{
if ( j == REMOVE_COLUMN)
continue;
destinationarr[p][q] = sourcearr[i][j];
++q;
}
++p;
}
}
#2
0
I'm trying to remove both 3 in two columns, under the index 2, how to achieve it?
我试着在索引2的两列中删除这三个,如何实现它?
public class For_petlja {
public static void main(String[] args) {
int[][] niz = {
{9, 2, 3, 6, 7},
{4, 7, 3, 5, 1}
};
int j;
for (int i = 0; i < niz.length; i++) {
for (j = 0; j < niz[i].length; j++) {
if (i == 2) {
continue;
}
} // for 'j'
System.out.println(Arrays.toString(niz[j]));
} // for 'i'
} // main method.
} // class