Getting the array length of a 2D array in Java

前端 未结 12 2313
耶瑟儿~
耶瑟儿~ 2020-11-27 09:27

I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:

public class MyClass {

 public s         


        
12条回答
  •  旧巷少年郎
    2020-11-27 09:45

    import java.util.Arrays;
    
    public class Main {
    
        public static void main(String[] args) 
        {
    
            double[][] test = { {100}, {200}, {300}, {400}, {500}, {600}, {700}, {800}, {900}, {1000}};
    
            int [][] removeRow = { {0}, {1}, {3}, {4}, };
    
            double[][] newTest = new double[test.length - removeRow.length][test[0].length];
    
            for (int i = 0, j = 0, k = 0; i < test.length; i++) {
                if (j < removeRow.length) {
                    if (i == removeRow[j][0]) {
                        j++;
                        continue;
                    }
                }
                newTest[k][0] = test[i][0];
                k++;
            }
    
            System.out.println(Arrays.deepToString(newTest));   
        }
    }
    

提交回复
热议问题