需要帮助来理解和修复NumberFormatException和ArrayIndexOutOfBoundsException

时间:2019-02-19 02:24:01

标签: java arrays

我有一个家庭作业,在这里我需要实现两个可以展平两个2d数组的方法。一个是int类型,另一个是string类型。当我运行程序时,会遇到两个异常,一个NumberFormatException尝试打印String数组,一个ArrayIndexOutOfBoundsException尝试打印int数组。我唯一可以更改的方法是名为flatten的方法。我有两个问题。首先是我不明白为什么我得到了ArrayIndexOutOfBoundsException或如何解决它。第二个是,虽然我确实理解为什么会收到NumberFormatException,但不确定如何解决。

public class Flatten {
    public static void main(String[] argv) {

        int[][] x = {
                {},
                {1,},
                {1, 2,},
                {1, 2, 3,},
                {1, 2, 3, 4,},
                {1, 2, 3, 4, 5,},
                {1, 2, 3, 4, 5, 6,},
        };
        print(flatten(x));

        String[][] y = {
                {"if", "else", "switch",},
                {"while", "do while", "for",},
                {"break", "continue",},
                {"*", "/", "%", "+", "-",},
                {">", ">=", "<=", "<", "==", "!=",},
                {"&&", "||", "!",},
                {"<<", ">>", ">>>", "&", "|", "^", "~",},
        };
        print(flatten(y));
    }

    /**
     * Prints a "flattened" 1d-array of ints.
     *
     * @param a the flattened array to print
     */
    static void print(int[] a) {
      for (int i = 0; i < a.length; i++) {
         System.out.print("{ ");
         for (int j = 0, k = a[i]; j < k; j++)
            System.out.print(a[++i] + ", ");
         System.out.println("}");
      }
   }

    /**
     * Prints a "flattened" 1d-array of String objects.
     *
     * @param a the flattened array to print
     */
      static void print(String[] a) {
      for (int i = 0; i < a.length; i++) {
         System.out.print("{ ");
         for (int j = 0, k = Integer.parseInt(a[i]); j < k; j++)
            System.out.print("\"" + a[++i] + "\", ");
         System.out.println("}");
      }
   }

    /**
     * TBI (To Be Implemented)...
     * <p>
     * This method "flattens" a 2-dimensional int-array
     * into a 1-dimensional int-array.  The algorithm
     * for flattening the 2d array is defined by code
     * in the print(int[]) method.
     *
     * @param x 2-dimensional int-array to "flatten"
     * @return the "flattened" 1-dimensional int-array
     */
    static int[] flatten(int[][] x) {
        int count = 0;
        for (int i = 0; i < x.length; i++) {
            count += x[i].length;
        }
        int[] arr = new int[count];
        int index = 0;
        for (int row = 0; row < x.length; row++) {
            for (int col = 0; col < x[row].length; col++) {
                arr[index] = x[row][col];
                index++;
            }
        }
        return arr;
    }

    /**
     * TBI (To Be Implemented)...
     * <p>
     * This method "flattens" a 2-dimensional String-array
     * into a 1-dimensional String-array.  The algorithm
     * for flattening the 2d array is defined by code
     * in the print(String[]) method.
     *
     * @param x 2-dimensional String-array to "flatten"
     * @return the "flattened" 1-dimensional String-array
     */
    static String[] flatten(String[][] x) {
        int count = 0;
        for (int i = 0; i < x.length; i++) {
            count += x[i].length;
        }
        String[] arr = new String[count];
        int index = 0;
        for (int row = 0; row < x.length; row++) {
            for (int column = 0; column < x[row].length; column++) {
                arr[index] = x[row][column];
                index++;
            }
        }
        return arr;
    }

}

我希望输出如下所示:https://i.imgur.com/W0lD2wO.png

任何有关如何解决这些错误并在将来避免它们的想法都将受到赞赏。

编辑:这是引发异常的地方 https://i.imgur.com/T2VzCc8.png https://i.imgur.com/8HS0Vxt.png

0 个答案:

没有答案
相关问题