Getting the array length of a 2D array in Java

前端 未结 12 2247
耶瑟儿~
耶瑟儿~ 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:58
    public class Array_2D {
    int arr[][];
    public Array_2D() {
        Random r=new Random(10);
         arr = new int[5][10];
         for(int i=0;i<5;i++)
         {
             for(int j=0;j<10;j++)
             {
                 arr[i][j]=(int)r.nextInt(10);
             }
         }
     }
      public void display()
      {
             for(int i=0;i<5;i++)
    
             {
                 for(int j=0;j<10;j++)
                 {
                     System.out.print(arr[i][j]+" "); 
                 }
                 System.out.println("");
             }
       }
         public static void main(String[] args) {
         Array_2D s=new Array_2D();
         s.display();
       }  
      }
    
    0 讨论(0)
  • 2020-11-27 10:01

    Try this following program for 2d array in java:

    public class ArrayTwo2 {
        public static void main(String[] args) throws  IOException,NumberFormatException{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int[][] a;
            int sum=0;
            a=new int[3][2];
            System.out.println("Enter array with 5 elements");
            for(int i=0;i<a.length;i++)
            {
                for(int j=0;j<a[0].length;j++)
                {
                a[i][j]=Integer.parseInt(br.readLine());
                }
            }
            for(int i=0;i<a.length;i++)
            {
                for(int j=0;j<a[0].length;j++)
                {
                System.out.print(a[i][j]+"  ");
                sum=sum+a[i][j];
                }
            System.out.println();   
            //System.out.println("Array Sum: "+sum);
            sum=0;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 10:04

    .length = number of rows / column length

    [0].length = number of columns / row length

    0 讨论(0)
  • 2020-11-27 10:05

    A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.

    import java.util.Arrays;
    
    public class Main {
      public static void main(String args[]) {
    
        int[][] test; 
        test = new int[5][];//'2D array'
        for (int i=0;i<test.length;i++)
          test[i] = new int[i];
    
        System.out.println(Arrays.deepToString(test));
    
        Object[] test2; 
        test2 = new Object[5];//array of objects
        for (int i=0;i<test2.length;i++)
          test2[i] = new int[i];//array is a object too
    
        System.out.println(Arrays.deepToString(test2));
      }
    }
    

    Outputs

    [[], [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
    [[], [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
    

    The arrays test and test2 are (more or less) the same.

    0 讨论(0)
  • 2020-11-27 10:06

    With Java 8, allow you doing something more elegant like this:

    int[][] foo = new int[][] {
            new int[] { 1, 2, 3 },
            new int[] { 1, 2, 3, 4},
        };
    
    int length = Arrays.stream(array).max(Comparator.comparingInt(ArrayUtils::getLength)).get().length
    
    0 讨论(0)
  • 2020-11-27 10:07

    Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:

    if (row > 0) col = test[0].length;
    
    0 讨论(0)
提交回复
热议问题