用java实现矩阵

发布于 2024-11-02 16:58:45 字数 339 浏览 1 评论 0原文

嗨,我尝试用java实现一个矩阵...我想编写一个方法,它的参数是矩阵的行和列,并且该方法生成矩阵。 我写了这段代码,但它有错误......我的问题是什么?请帮我

public class Table {
int row ;
int column ;

Table (int rows , int columns ){
    row = rows;
    column = columns;
}

void makeTable(int row , int column){
    char[][] ;
    c.length = column ;
    c[0].length = row ;

}
}

hi i try to implement a matrix by java ... i want to write a method that it's argument is the row and the column of matrix and the method make the matrix.
i write this code but it have error .... what is my problem? please help me

public class Table {
int row ;
int column ;

Table (int rows , int columns ){
    row = rows;
    column = columns;
}

void makeTable(int row , int column){
    char[][] ;
    c.length = column ;
    c[0].length = row ;

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

提赋 2024-11-09 16:58:45
class Table {

    int row;
    int column;
    char[][] c;

    Table(int rows, int columns) {
        row = rows;
        column = columns;
        makeTable(rows, column);//calling initilizer method
    }

    private void makeTable(int row, int column) {
        c = new char[row][column];//initilizing c

    }
}
class Table {

    int row;
    int column;
    char[][] c;

    Table(int rows, int columns) {
        row = rows;
        column = columns;
        makeTable(rows, column);//calling initilizer method
    }

    private void makeTable(int row, int column) {
        c = new char[row][column];//initilizing c

    }
}
献世佛 2024-11-09 16:58:45

您没有提供具体的错误,但以下是现有代码中的一些错误:

void makeTable(int row , int column){
    // the next line is invalid - needs a variable name for the declaration
    char[][] ;
    // array length is not set as below, but is set at definition time
    c.length = column ;
    c[0].length = row ;

}

在 Java 中定义多维数组的正确方法是:

char[][] c = new char[row][column];

您还可以声明一个包含参差不齐的行的数组,每个行都有不同的列长度(或在你的情况下也是一样的,但为什么要把事情复杂化呢?)如下:

char[][] c = new char[row][];
for(int i = 0; i < row; i++) {
    c[row] = new char[column];
}

You don't provide a specific error, but here are a few in your existing code:

void makeTable(int row , int column){
    // the next line is invalid - needs a variable name for the declaration
    char[][] ;
    // array length is not set as below, but is set at definition time
    c.length = column ;
    c[0].length = row ;

}

The correct way to define a multidimensional array in Java is:

char[][] c = new char[row][column];

You can also declare an array with ragged rows each of whom has a different column length (or the same, in your case, but why complicate things?) as follows:

char[][] c = new char[row][];
for(int i = 0; i < row; i++) {
    c[row] = new char[column];
}
似最初 2024-11-09 16:58:45

您是否考虑过提供变量名称?或者甚至声明矩阵?

想象:

class Table {
char matrix[][];
public Table(int row, int column) {
    matrix=makeTable(row, column);
} 

char[][] makeTable(int row, int column) {
   return new char[row][column];
}
}

Have you considered providing variable names? Or even declaring the matrix?

Imagine:

class Table {
char matrix[][];
public Table(int row, int column) {
    matrix=makeTable(row, column);
} 

char[][] makeTable(int row, int column) {
   return new char[row][column];
}
}
行雁书 2024-11-09 16:58:45

除了存储矩阵的维度(行数和列数)之外,您还需要一个数组或类似的结构来存储数组的内容。这将是类级别(成员变量),makeTable() 中的 char[][] ; 是存储它的错误位置,这会赢无论如何都不会构建,因为您还没有指定名称。

As well as storing the dimensions of the matrix (number of rows and columns) you require an array or similar structure to store the contents of the array. This would be a the class level (a member variable), char[][] ; you have in makeTable() is the wrong place to store that, and this won't build anyway because you haven't specified a name.

虚拟世界 2024-11-09 16:58:45

我知道这可能是家庭作业,因此您需要推出自己的矩阵类。但对于其他人来说,Apache Commons 在其 Math 包中提供了一个运行良好的矩阵库。

http://commons.apache.org /math/api-2.0/org/apache/commons/math/线性/RealMatrix.html

I know this is probably homework and thus you're required to roll your own matrix class. But for others out there Apache Commons has a matrix library in their Math package that works very well.

http://commons.apache.org/math/api-2.0/org/apache/commons/math/linear/RealMatrix.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文