簡體   English   中英

用Java打印二維構造函數

[英]Printing a 2 dimensional constructor in Java

我需要從main方法中在構造函數中打印2d數組的值。 每次調用mainSc數組時,二維數組中的每個值都會得到null值。 為什么會這樣,以及如何修復數組以從構造函數中調用值?

public class Main {
    public static void main(String[] args) {
        String[][] mainSc = new String[5][5];

        System.out.println(Arrays.deepToString(mainSc));
    }
}

import java.util.Arrays;

public class Schedule {
    private int numDays;
    private int numClasses;
    private String[][] Cal;

    public Schedule(String[][] array) {
        this.numDays = 5;
        this.numClasses = 4;
        this.Cal = array;
    }
    public String[][] Array() {
        for (int r = 0; r < numDays; r++){
            for (int j = 0; j <= numClasses; j++){
                this.Cal[0][0] = "Monday";
                this.Cal[1][0] = "Tuesday";
                this.Cal[2][0] = "Wednesday";
                this.Cal[3][0] = "Thursday";
                this.Cal[4][0] = "Friday";  
            }
        }
      return this.Cal;
    }
    public void printSchedule() {
        for (int r = 0; r <= numDays; r++){
            for (int j = 0; j <= numClasses; j++){
                System.out.println(this.Cal[r][j]);
            }
        }
    }

}

聲明數組引用變量不會創建數組。 該過程的下一步是使用new關鍵字創建一個數組並將其地址分配給該變量。 您還應該包括數組的尺寸。

private String[][] cal = new String[5][5];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM