假设我的a b sentiment.text.content sentiment.text.beginOffset sentiment.sentiment.magnitude sentiment.sentiment.score
1 -1.546765 s the normal flow of supply chain activities is interrupted, -1 0.3 -0.3
看起来像这样:array
。
我应该如何在Java中创建一个与此完全相同的新数组,除了删除了一行和一列?
我可以使用偶数大小的阵列执行此任务,但锯齿状阵列给我带来了一些麻烦。我想首先创建一个具有未指定列数的新数组,但是从哪里开始呢?
{{1,3,5,7},{2,4,6,8,10,12},{2,3,5,7,11,13,17}}
运行测试用例时,例如:
removeRowAndCol(new int [] [] {{1,2},{3,4}},1,1),该方法返回正确的 /**
* Creates a new array that is a copy of the input matrix, except that one
* row and one column have been altered.
* Precondition: the row index is between 0 (inclusive) and the number of
* rows of matrix (not inclusive)
* @param matrix the input two dimensional array
* @param row the index of the row to remove
* @param col the index of the column to remove
*/
public static int[][] removeRowAndCol(int[][] matrix, int row, int col) {
int[][] altered = new int[(matrix.length - 1)][];
int x = 0;
for(int i = 0; i < matrix.length; i++){
if(matrix[i].length < col + 1 && i != row){
altered[x] = new int[matrix[i].length];
for(int j = 0; j < altered[x].length; j++){
altered[x][j] = matrix[i][j];
}
if(x < matrix.length - 1){
x++;
}
}
else if(matrix[i].length > col && i != row){
altered[x] = new int[matrix[i].length - 1];
int y = 0;
for(int z = 0; z < matrix[i].length - 1; z++){
if(z != col){
altered[x][y] = matrix[i][z];
y++;
}
else{
z--;
}
}
if(x < matrix.length - 1){
x++;
}
}
}
return altered;
}
}
。
但是,有这样的事情: int [] [] array = {{1,2,3,4},{11,12,13,14,15,16},{21,22,23,24},{31,32,33}} ; removeRowAndCol(array,0,0) removeRowAndCol(array,2,3) 该方法会冻结。
有人可以看看代码并告诉我我做错了吗?
答案 0 :(得分:0)
一个二维数组,无论是否锯齿,都是一个数组数组,而不是其他任何数组。您必须手动创建每一行,因此,您可以为每一行选择任何大小。
import java.util.Arrays;
public class Temp {
public static void main(String[] args) {
int[][] jagged = {{1, 2, 3}, {4, 5, 6, 7, 8}, {9, 10, 11, 12, 13, 14, 15, 16}};
System.out.println("Jagged: " + Arrays.deepToString(jagged));
System.out.println("Smaller 1: " + Arrays.deepToString(removeRowAndCol(jagged, 0, 0)));
System.out.println("Smaller 2: " + Arrays.deepToString(removeRowAndCol(jagged, 1, 1)));
System.out.println("Smaller 3: " + Arrays.deepToString(removeRowAndCol(jagged, 2, 2)));
}
private static int[][] removeRowAndCol(int[][] jagged, int i, int j) {
int[][] smaller = new int[jagged.length - 1][];
// WARN: outofbounds checks are not implemented!
for (int smallerI = 0; smallerI < smaller.length; smallerI++) {
int sourcedI = smallerI;
if (smallerI >= i) {
sourcedI++;
}
smaller[smallerI] = new int[jagged[sourcedI].length - 1];
for (int smallerJ = 0; smallerJ < smaller[smallerI].length; smallerJ++) {
int sourcedJ = smallerJ;
if (smallerJ >= j) {
sourcedJ++;
}
smaller[smallerI][smallerJ] = jagged[sourcedI][sourcedJ];
}
}
return smaller;
}
}
哪个输出:
Jagged: [[1, 2, 3], [4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16]]
Smaller 1: [[5, 6, 7, 8], [10, 11, 12, 13, 14, 15, 16]]
Smaller 2: [[1, 3], [9, 11, 12, 13, 14, 15, 16]]
Smaller 3: [[1, 2], [4, 5, 7, 8]]
答案 1 :(得分:0)
您可以使用 streams 重新排列二维数组的行和数组本身,而不是一行一列。这个数组是锯齿状的还是矩形的都没有关系。并且可以去掉需要删除的行和列应该存在的不必要条件。
/**
* Creates a new array that is a copy of the input matrix,
* except that one row and one column were removed if present.
*
* @param matrix the input two-dimensional array.
* @param row the index of the row to remove.
* @param col the index of the column to remove.
* @return new two-dimensional array.
*/
public static int[][] removeRowAndCol(int[][] matrix, int row, int col) {
return IntStream
// iterate over the indexes
// of rows of the matrix
.range(0, matrix.length)
// filter out the row to remove
.filter(i -> i != row)
// rearrange the remaining rows
.mapToObj(i -> IntStream
// iterate over the indexes
// of columns of the matrix
.range(0, matrix[i].length)
// filter out the column to remove
.filter(j -> j != col)
// take the cell value
.map(j -> matrix[i][j])
// rearrange the row
.toArray())
// rearrange the matrix
.toArray(int[][]::new);
}
public static void main(String[] args) {
int[][] arr1 = {{1,2},{3,4}};
int[][] arr2 = {{1,3,5,7},{2,4,6,8,10,12},{2,3,5,7,11,13,17}};
int[][] arr3 = {{1,2,3,4},{11,12,13,14,15,16},{21,22,23,24},{31,32,33}};
System.out.println(Arrays.deepToString(removeRowAndCol(arr1, -1, 1)));
System.out.println(Arrays.deepToString(removeRowAndCol(arr2, 2, 3)));
System.out.println(Arrays.deepToString(removeRowAndCol(arr3, 3, -1)));
}
输出:
[[1], [3]]
[[1, 3, 5], [2, 4, 6, 10, 12]]
[[1, 2, 3, 4], [11, 12, 13, 14, 15, 16], [21, 22, 23, 24]]