如何打印2d阵列时间表?

时间:2016-08-30 10:38:28

标签: java arraylist

package javaapplication18;

public class JavaApplication18 {


   public static void main(String[] args) {
       String weekdays[][] = new String[7][0];
       weekdays[1][0] = "mon";
       weekdays[2][0] = "tue";
       weekdays[3][0] = "wed";
       weekdays[4][0] = "thur";
       weekdays[5][0] = "fri";
       weekdays[6][0] = "sat";
       String hours[][] = new String[0][5];
       hours[0][1] = "9:30";
       hours[0][2] = "10:30";
       hours[0][3] = "11:30";
       hours[0][4] = "12:30";
       System.out.println(weekdays[7][0]);
   }

}

当我编译它时,它会在线程“main”java.lang.ArrayIndexOutOfBoundsException中给出错误说错误:0     在javaapplication18.JavaApplication18.main(JavaApplication18.java:8) 我该怎么纠正这个?

4 个答案:

答案 0 :(得分:0)

更改数组声明中的数组大小。大小0应为1.

当您声明一个数组时,索引应该至少为一个。因为它告诉它应该在内存中分配多少空间。但是当你使用数组后者时,索引是基于0的。如果您声明a[6][0],则最后一个数组元素为 public static void main(String[] args) { String weekdays[][] = new String[7][1]; weekdays[0][0] = "sun"; weekdays[1][0] = "mon"; weekdays[2][0] = "tue"; weekdays[3][0] = "wed"; weekdays[4][0] = "thur"; weekdays[5][0] = "fri"; weekdays[6][0] = "sat"; String hours[][] = new String[1][5]; hours[0][1] = "9:30"; hours[0][2] = "10:30"; hours[0][3] = "11:30"; hours[0][4] = "12:30"; System.out.println(weekdays[6][0]); // sat }

private Label label;
    private Label sign;
    private String lblValue;
    private String REQUIRED_SIGN = " *";
    private boolean mandatory;


    public SignLabelCustom() 
        {
        label = new Label();
        label.setSclass("form-label");
        appendChild(label);
        sign = new Label();
        if(mandatory=true){
         sign.setValue(REQUIRED_SIGN);   
         sign.setStyle("color: red");
         appendChild(sign); 
        }
        else{
         sign.setValue("");   
         sign.setStyle("color: red");
        removeChild(sign); 
        }

    }

    public String getValue() {
        return lblValue;
    }

    public boolean isMandatory() {
        return mandatory;
    }

    public void setMandatory(boolean mandatory) {
        this.mandatory = mandatory;
    }



    public void setValue(String lblValue) {
        label.setValue(lblValue);
        this.lblValue = lblValue;
    }

答案 1 :(得分:0)

试试这个

public static void main(String[] args) {
    //way 1
    String weekdays[][] = new String[7][];
    weekdays[0] = new String[] { "sun" };
    weekdays[1] = new String[] { "mon" };
    weekdays[2] = new String[] { "tue" };
    weekdays[3] = new String[] { "wed" };
    weekdays[4] = new String[] { "thur" };
    weekdays[5] = new String[] { "fri" };
    weekdays[6] = new String[] { "sat" };

    //way 2
    String hours[][] = new String[1][5];
    hours[0][0] = "8:30";
    hours[0][1] = "9:30";
    hours[0][2] = "10:30";
    hours[0][3] = "11:30";
    hours[0][4] = "12:30";
    System.out.println(weekdays[1][0]);
}

答案 2 :(得分:0)

尝试在工作日数组中更改数组大小以及6而不是7

public static void main(String[] args) {

        String weekdays[][] = new String[7][1];
        weekdays[0][0] = "sun";
        weekdays[1][0] = "mon";
        weekdays[2][0] = "tue";
        weekdays[3][0] = "wed";
        weekdays[4][0] = "thur";
        weekdays[5][0] = "fri";
        weekdays[6][0] = "sat";

        String hours[][] = new String[1][5];
        hours[0][1] = "9:30";
        hours[0][2] = "10:30";
        hours[0][3] = "11:30";
        hours[0][4] = "12:30";
        System.out.println(weekdays[6][0]);
    }

//在OP评论后回答

public static void main(String[] args) {

        String weekdays[][] = new String[7][7];
        weekdays[0][0] = "sun";

        weekdays[0][1] = "9:30";
        weekdays[0][2] = "10:30";
        weekdays[0][3] = "11:30";

        weekdays[1][0] = "mon";

        weekdays[1][1] = "9:30";
        weekdays[1][2] = "10:30";
        weekdays[1][3] = "11:30";

        weekdays[2][0] = "tue";

        weekdays[2][1] = "9:30";
        weekdays[2][2] = "10:30";
        weekdays[2][3] = "11:30";

        weekdays[3][0] = "wed";

        weekdays[3][1] = "9:30";
        weekdays[3][2] = "10:30";
        weekdays[3][3] = "11:30";

        weekdays[4][0] = "thur";

        weekdays[4][1] = "9:30";
        weekdays[4][2] = "10:30";
        weekdays[4][3] = "11:30";

        weekdays[5][0] = "fri";

        weekdays[5][1] = "9:30";
        weekdays[5][2] = "10:30";
        weekdays[5][3] = "11:30";

        weekdays[6][0] = "sat";

        weekdays[6][1] = "9:30";
        weekdays[6][2] = "10:30";
        weekdays[6][3] = "11:30";

        String hours[][] = new String[1][5];
        hours[0][1] = "9:30";
        hours[0][2] = "10:30";
        hours[0][3] = "11:30";
        hours[0][4] = "12:30";

        int j = 0;
        int i = 0;
        while (i < 7) {
            System.out.print("\t" + weekdays[i][j]);
            j++;
            if (j > 3) {
                j = 0;
                i++;
                System.out.println();
            }
        }
    }

答案 3 :(得分:0)

首先,您获得的错误是运行时错误,在编译期间不应该发生。 其次,为了打印一个数组,你需要迭代它,对于2d数组也是如此。

您的代码应该是这样的:

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