java - Java 矩阵的异常

标签 java arrays matrix syntax

我使用此代码收到 ArrayIndexOutOfBoundsException,有人可以告诉我我做错了什么吗?

public class Test {
    public static void main(String args[]) {

        int[][] matrix = {{1,2,3,4},
                     {1,2,3,4}};

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

最佳答案

for (int j = 0; i < matrix[i].length; j++) {

您正在与内部循环中的错误归纳变量进行比较。应该是这样的:

for (int j = 0; j < matrix[i].length; j++) {
//              ^

关于java - Java 矩阵的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42306914/

相关文章:

Java Process 在 OutOfMemoryException 之后没有死

java - 在 Bottom DialogFragment 中,我将 Draggable 设置为 false 但它收到了警告消息

php - Cakephp 3+ 将数组保存在mysql的一个字段中

python - 将数组与文件进行比较,并根据数组元素形成组

java - Kruskal 算法 - 按升序对邻接矩阵进行排序

java - jMeter Slave - 服务器启动失败 : java. rmi.RemoteException : Cannot start. ip-10-142-111-66 is a loopback address

java.lang.UnsupportedOperationException : Can't convert to color: type=0x2 on setErrorEnabled for InputTextLayout 异常

javascript - 访问 Javascript 中对象数组内的所有属性

python - 当我尝试在 Python 上反转矩阵中的行时出错

javascript - 在javascript中查找多维数组的行和列