JAVA pass column of 2d array as 1d array

情到浓时终转凉″ 提交于 2021-02-10 06:16:53

问题


I need to pass a column of 2d array as 1d array.

I have made this example to explain, and i don't know how to call print function.

public static void main(String[] args) {

    double[][] matrix = {{ 0,1,2},
                        {3,4,5},
                        };
}

public void print(double[] vector){

    for(int i = 0; i< vector.length ; i++){
        System.out.print(vector[i]);
    }
}

how can i do?


回答1:


I need to pass a column of 2d array as 1d array.

No directly you can not call method with 2D array as your method takes parameter double[] and not double[][]. 2D array can have 1D arrays as elements in it. So, matrix[int index] will give you 1D array to pass it to the method.

So either change method declaration to print(double[][] vectors) or pass matrix[index] as method parameter.

You can call the method by instance of you class.

YourClass obj = YourClass();
obj.print(matrix[i]);

As your method takes vector,so matrix[index] should be used.

You can build column vector from your matrix by iterating over it and than pass vector to the method.

EDIT:

You need to loop over your matrix and collect first element of all 1D arrays to another 1D array and pass it as an argument to the method.

double[] vector = new double[matrix.length];
for(int i=0;i<matrix.length;i++) {
    double oneDArray[] = matrix[i];//Only for example
    if(oneDArray != null && oneDArray.length >0 ) {
       vector[i] = oneDArray[0];
    }
}



回答2:


You forgot double square brackets in your method header and didn't declare method as static.So: public void print(double[] vector) should be: public static void print(double[][]vector). To call print method: print(matrix);. Also, your for loop is wrong. You have to have double square brackets. As it is now, it will print something similar to this: [D@48e61e. That's because you need to use double brackets, so if you want to print a row it will be:

for(int i = 0; i< vector[1].length ; i++){
    System.out.print(vector[0][i]);
}

To print a column do this:

int j = 1;//or whatever column you want to print    
for(int i=0; i<vector.length; i++){
    System.out.println(vector[i][j]);
}



回答3:


Is this the kind of solution you're looking for?

public static void main(String[] args) {
    double[][] matrix = { { 0, 1, 2 }, { 3, 4, 5 }, };
    for (int i = 0; i < matrix.length; i++) {
        print(matrix[i]);
        System.out.println();//Prints a new line
    }
}

public static void print(double[] vector) {
    for (int i = 0; i < vector.length; i++) {
        System.out.print(vector[i] + " ");
    }
}

Iterate over the array & get the 1D array sub-elements & then pass them to the printing function individually.

EDIT:

There isn't any easy way to do it. You'll have to change your data structures accordingly. So here is one primitive way of doing it:

Again iterating over the arrays & extracting the elements column-wise. Then storing them in another array (cols) & printing that.

    double[][] matrix = { { 0, 1, 2 }, { 3, 4, 5 }, };
    for (int j = 0; j < matrix[0].length; j++) {
        double[] cols = new double[matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            cols[i] = matrix[i][j];// Iterate column-wise
        }
        print(cols);// Call the 1D array print method
        System.out.println();// Prints new line
    }



回答4:


If you are ok with using third party library then may be you can have a look at Common Maths Library

And you can get the column in just three lines of code:

double[][] twoDimData = new double[10][10];
BigMatrix matrix = MatrixUtils.createBigMatrix(twoDimData);
matrix.getColumnAsDoubleArray(0);

And if you want to have a low level solution then may be you can try:

double[][] matrix = {{ 0,1,2},{3,4,5}};
int columnToTake = 2;
double[] colArray;
for(int row = 0; row < numRows; row++)
{
    colArray[row] = matrix[row][columnToTake];
}
print(colArray)


来源:https://stackoverflow.com/questions/30140861/java-pass-column-of-2d-array-as-1d-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!