-1

At the moment my code looks like this:

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};

is there anyway i can define the strings i got in the array so that my code is not so long?

i don't think its necessary for me to define a string and then put it in a array cant i define a String into an array.

So basically my question is, is there a way for me to shorten my code?

Wilder Pereira
  • 2,039
  • 3
  • 21
  • 27
Jacques Celliers
  • 51
  • 1
  • 1
  • 8

5 Answers5

1

Do you mean like this?

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" } };

Or maybe like this?

public String[][] optionsarray = new String[6][7];
{
    for (String[] row : this.optionsarray)
        Arrays.fill(row, "oops");
}
Andreas
  • 138,167
  • 8
  • 112
  • 195
1

If you know the strings you want to initialize your 2D array with, then you could simply do

final String[][] example = { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };
Ravi Naik
  • 554
  • 5
  • 6
0

So basically my question is, is there a way for me to shorten my code?

yes, you can use loops if the string is all the same:

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";
Tom
  • 14,120
  • 16
  • 41
  • 47
Timothy Truckle
  • 12,232
  • 2
  • 22
  • 44
  • Main thing here is the variable names i want to keep it like that? – Jacques Celliers Jan 07 '17 at 21:56
  • @JacquesCelliers *"Will this still be in my array Q1o1,Q1o2,etc.."* no. if you need these variables you have to write them as you did. But I'm pretty confident that you don't need them. This is because if you change the value in the array the value in the variable stays the same and vice versa... – Timothy Truckle Jan 07 '17 at 22:15
0

You can shorten it dramatically. For example:

public String[][] optionsarray = new String[6][7];

{
    for (String[] row : optionsarray) {
        Arrays.fill(row, "oops");
    }
}

That uses an instance initializer block, but you could instead have the initialization loop in a constructor (or elsewhere).

John Bollinger
  • 121,924
  • 8
  • 64
  • 118
0

It's time to use 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");
    }
}

then somewhere else

List<Question> questions=new ArrayList<>();
questions.add(new Question());

now you can send this questions object wherever you want

this approach will be more maintainable and less redundant.