Home > Software design >  find number of columns in a 2D array Java
find number of columns in a 2D array Java

Time:02-05

I'm new to java and am trying to find the transpose of a matrix X with m rows and x columns. I'm not familiar with java syntax but I think I can access the number of rows using double[x.length] but how do I figure out the number of columns? Here is my code:

import stdlib.StdArrayIO;

public class Transpose {
    // Entry point
    public static void main(String[] args) {
        double[][] x = StdArrayIO.readDouble2D();
        StdArrayIO.print(transpose(x));
    }

    // Returns a new matrix that is the transpose of x.
    private static double[][] transpose(double[][] x) {
        // Create a new 2D matrix t (for transpose) with dimensions n x m, where m x n are the
        // dimensions of x.
        double[][] t = new double[x.length][x[1.length]]

        // For each 0 <= i < m and 0 <= j < n, set t[j][i] to x[i][j].
        


        // Return t.
    


    }
}

Also, I was trying to figure it out by printing a 2D list in a scrap file but It wouldn't work for some reason, where is what I had:

import stdlib.StdArrayIO;
import stdlib.StdOut;

public class scrap {
    public static void main(String[] args){
        double[][] x = new double[10][20];
        StdOut.println(x.length);
    }


}

Could you point out what I was doing wrong? It kept waiting on input. I could also use any other tips that would help, I am transitioning from Python.

CodePudding user response:

To find the size of the rows:

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int numRows = matrix.length;
System.out.println("Number of rows: "   numRows);

To find the size of the columns:

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int numColumns = matrix[0].length;
System.out.println("Number of columns: "   numColumns);

CodePudding user response:

class TwoDmArray {
    public static void main(String[] args) {
        
        int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 } };
        
        for(int i = 0; i < 2; i  ) { // It represents row
            for(int j = 0; j < 3; j  ) { // It represents column

               System.out.print(arr[i][j]);

               if(j == 2) { // When column index will be 2 then will print elements in new line
                   System.out.println();
               }
            }    
        }
    }
}

OUTPUT : 123 
         456

Explanation : Here array has 2 rows and 3 columns so, matrix will be in row and column form.

CodePudding user response:

In Java, a two-dimensional array is merely an array of arrays. This means the length of any of the array elements can be checked to find the number of columns. Using your example, this would be:

double[][] t = new double[x[0].length][x.length];

This should be sufficient for your matrix situation, where we can assume the outer array is not empty, and all inner arrays have the same length.

Note that this is not guaranteed to work in general. If the outer array is empty, there are no inner arrays to check, so the number of columns isn't defined and can't be determined. Secondly, there is no guarantee that all inner arrays of a given array of arrays in Java are all the same length. They will be using the new double[x][y] syntax, but the array could have been created using another mechanism or one of the inner arrays could have been replaced with an array of a different length.

  • Related