gpt4 book ai didi

Java - 在构造函数中初始化数组

转载 作者:行者123 更新时间:2023-12-01 17:58:14 26 4
gpt4 key购买 nike

我想在构造函数中初始化二维数组。但是,当我在类中声明数组的实例变量时遇到问题。如果我这样做会出错:

public class Data {
private String [][] tabel;
public Data(){
tabel = {{"ID", "NAME"},
{"101", "Max"},
{"102", "Mark"},
{"103", "Downey"},
{"104", "Matthew"},
{"105", "Richard"}};
}

我该如何解决这个问题?

最佳答案

您需要在数组初始值设定项前面编写new Type[],如下所示:

tabel = new String[][]{
new String[]{"ID", "NAME"},
new String[]{"101", "Max"},
new String[]{"102", "Mark"},
new String[]{"103", "Downey"},
new String[]{"104", "Matthew"},
new String[]{"105", "Richard"}};

关于Java - 在构造函数中初始化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42978834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com