【问题标题】:Out of bounds exception despite loops being in range of array length尽管循环在数组长度范围内,但超出范围异常
【发布时间】:2019-09-08 08:18:01
【问题描述】:

所以程序应该根据用户输入创建一个大小介于 3 和 11 之间的奇数数组,然后在特定位置用字符填充该板以获取模式。一切都很顺利,直到我尝试返回给我 2 个超出范围异常的数组,即使我将循环设置为小于维度。我在这里以 5 为例来尝试获得一个 5 x 5 的数组。这里是主要的。

public static void main (String [] args) {

    int dimension = findDimension();
    char [] [] array2d = new char [dimension] [dimension];

    char star = '*';

    array2d = leftDiagonal(star, dimension); // Get out of bounds here
    print(array2d); 
} 

要求用户输入“findDimension()”的方法

public static int findDimension() {
    int dimension = 0;
    Scanner keybd = new Scanner(System.in); 
    do {
        System.out.print("Enter an odd integer between 3 and 11 please: ");
        dimension = keybd.nextInt();
    } while (dimension%2 == 0);
    return dimension;            // Everything seems fine here, no errors
}

打印数组的方法

public static void print(char [] [] arrayParam) {
    System.out.println("-----------");
    System.out.println(arrayParam);
    System.out.println("-----------");
}

设置模式“leftDiagonal”的方法

public static char [] [] leftDiagonal(char starParam, int dimenParam) {
    char [] [] leftD = new char [dimenParam] [dimenParam];
    for (int i = 0; i < dimenParam; i++){ 
        for (int j = 0; i < dimenParam; j++) {
            leftD [i][j] = starParam;  // Gets error here
        }
    }
    return leftD;
}

输出应该是

-----------                             
 * * * * *
 * * * * *
 * * * * *
 * * * * *
 * * * * *
----------- 

技术上应该是这样的

 -----------                             
  *    
    *   
      *  
        * 
          *
 -----------  

但目前我只想获得任何输出。我最初打算用空格 ' ' 填充所有空格,然后用字符填充我需要的空格,但我什至无法先打印出数组。感谢任何愿意提供帮助的人。

【问题讨论】:

  • 判断答案是否有帮助,然后……接受! If you want to say "thank you," vote on or accept that person's answer, or simply pay it forward by providing a great answer to someone else's question.

标签: java arrays char 2d indexoutofboundsexception


【解决方案1】:

由于内部循环条件而发生错误。

public static char[][] leftDiagonal(char starParam, int dimenParam) {
    char[][] leftD = new char[dimenParam][dimenParam];
    for (int i = 0; i < dimenParam; i++) {
        for (int j = 0; j < dimenParam; j++) { // i -> j
            leftD[i][j] = starParam;  // Gets error here
        }
    }
    return leftD;
}

有很多方法可以解决这个问题。 你可以直接打印数组而不初始化它。

public static char[][] leftDiagonal(char starParam, int dimenParam) {
    char[][] leftD = new char[dimenParam][dimenParam];
    for (int i = 0; i < dimenParam; i++) {
        for (int j = 0; j < dimenParam; j++) {
            if(i==j) {
                System.out.print(starParam);
            } else {
                System.out.print("  ");
            }
        }
        System.out.println();
    }
    return leftD;
}

【讨论】:

  • 谢谢你,没早点注意到这一点我觉得很傻。我现在打印了一些东西,但我得到了内存地址而不是任何预期的输出。我将数组“array2d”设置为等于我的 leftDiagonal 方法,为什么会发生这种情况?我需要一个循环来打印出内容吗?
  • 编辑了我的答案。 Do I need a loop to print out the contents? 没错!
  • 哦,对不起,我又住院了。这是我的一个坏习惯,谢谢你的帮助。
  • @ConfusedStudent 是的。很开心跟你聊天。祝你今天过得愉快! :D
猜你喜欢
  • 2022-01-13
  • 2013-09-28
  • 2020-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
  • 2016-04-14
相关资源
最近更新 更多