目前我的代码看起来像这样:
public String Q1o1 = "oops";
public String Q1o2 = "oops";
public String Q1o3 = "oops";
public String Q1o4 = "oops";
public String Q1o5 = "oops";
public String Q1o6 = "oops";
public String Q1o7 = "oops";
public String Q2o1 = "oops";
public String Q2o2 = "oops";
public String Q2o3 = "oops";
public String Q2o4 = "oops";
public String Q2o5 = "oops";
public String Q2o6 = "oops";
public String Q2o7 = "oops";
public String Q3o1 = "oops";
public String Q3o2 = "oops";
public String Q3o3 = "oops";
public String Q3o4 = "oops";
public String Q3o5 = "oops";
public String Q3o6 = "oops";
public String Q3o7 = "oops";
public String Q4o1 = "oops";
public String Q4o2 = "oops";
public String Q4o3 = "oops";
public String Q4o4 = "oops";
public String Q4o5 = "oops";
public String Q4o6 = "oops";
public String Q4o7 = "oops";
public String Q5o1 = "oops";
public String Q5o2 = "oops";
public String Q5o3 = "oops";
public String Q5o4 = "oops";
public String Q5o5 = "oops";
public String Q5o6 = "oops";
public String Q5o7 = "oops";
public String Q6o1 = "oops";
public String Q6o2 = "oops";
public String Q6o3 = "oops";
public String Q6o4 = "oops";
public String Q6o5 = "oops";
public String Q6o6 = "oops";
public String Q6o7 = "oops";
public String[] optionvalues1 = {Q1o1, Q1o2, Q1o3, Q1o4, Q1o5, Q1o6, Q1o7};
public String[] optionvalues2 = {Q2o1, Q2o2, Q2o3, Q2o4, Q2o5, Q2o6, Q2o7};
public String[] optionvalues3 = {Q3o1, Q3o2, Q3o3, Q3o4, Q3o5, Q3o6, Q3o7};
public String[] optionvalues4 = {Q4o1, Q4o2, Q4o3, Q4o4, Q4o5, Q4o6, Q4o7};
public String[] optionvalues5 = {Q5o1, Q5o2, Q5o3, Q5o4, Q5o5, Q5o6, Q5o7};
public String[] optionvalues6 = {Q6o1, Q6o2, Q6o3, Q6o4, Q6o5, Q6o6, Q6o7};
public String[][] optionsarray = {optionvalues1,optionvalues2,optionvalues3,optionvalues4,optionvalues5,optionvalues6};
无论如何我可以定义我在数组中得到的字符串,这样我的代码就不会那么长了吗?
我认为没有必要为我定义一个字符串,然后把它放在一个数组中,我不能将一个字符串定义为数组。
所以基本上我的问题是,有没有办法缩短我的代码?
答案 0 :(得分:1)
你的意思是这样吗?
public String[][] optionsarray = {
{ "oops", "oops", "oops", "oops", "oops", "oops", "oops" },
{ "oops", "oops", "oops", "oops", "oops", "oops", "oops" },
{ "oops", "oops", "oops", "oops", "oops", "oops", "oops" },
{ "oops", "oops", "oops", "oops", "oops", "oops", "oops" },
{ "oops", "oops", "oops", "oops", "oops", "oops", "oops" },
{ "oops", "oops", "oops", "oops", "oops", "oops", "oops" } };
或者也许是这样的?
public String[][] optionsarray = new String[6][7];
{
for (String[] row : this.optionsarray)
Arrays.fill(row, "oops");
}
答案 1 :(得分:1)
如果您知道要用其初始化2D数组的字符串,那么您可以简单地执行
final String[][] example = { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };
答案 2 :(得分:0)
基本上我的问题是,有没有办法缩短我的代码?
是的,如果字符串完全相同,你可以使用循环:
String[][] optionsarray = new String[6][7];
for (int i = 0; i < optionsarray.length; i++)
for (int j = 0 ; j < optionsarray[i].length; j++)
optionsarray[i][j] = "oops";
答案 3 :(得分:0)
你可以大大缩短它。例如:
public String[][] optionsarray = new String[6][7];
{
for (String[] row : optionsarray) {
Arrays.fill(row, "oops");
}
}
它使用实例初始化块,但您可以在构造函数(或其他地方)中使用初始化循环。
答案 4 :(得分:0)
是时候使用Classes and Collections了
class Question{
public List<String> options;
public Question(){
options=new ArrayList<>();
options.add("oops");
options.add("oops");
options.add("oops");
options.add("oops");
}
}
然后在其他地方
List<Question> questions=new ArrayList<>();
questions.add(new Question());
现在您可以在任何地方发送此questions
对象
这种方法更易于维护,更少冗余。