0
String[][] aS= new String[16][3];

String[] s0 ={"FIELD0", "FIELD1", "FIELD2"};
String[] s1 ={"FIELD0", "FIELD1", "FIELD2"};
String[] s2 ={"FIELD0", "FIELD1", "FIELD2"}; ...
String[] s15 ={"FIELD0", "FIELD1", "FIELD2"};

for(int i=0;i<aS.length;i++)
{
    for(int j=0;j<3;j++)
    {
        //error!
        aS[i][j]= s+"i"+[j];   //s0[0],s0[1]...s15[3]
    }
}

Im familiar with multidimensional arrays, im just not abot to figure out how this part can be fixed: " s+"i"+[j]; "

Edit:[error] Syntax error on token "+", Expression expected after this token

2
  • Which error do you get? Commented Feb 16, 2014 at 14:05
  • Syntax error on token "+", Expression expected after this token Commented Feb 16, 2014 at 14:06

3 Answers 3

1

You can't do that in Java (and in most programming languages), it doesn't support dynamic naming.

If you want to use s0, s1 or any other array, you should write it, for example:

aS[i][j]= s0[j];
Sign up to request clarification or add additional context in comments.

1 Comment

yup, but then i'd have to keep chanding s0,s1,s2; manually. i'd labelled them numbers hoping i could change 0,1,2... within a loop
1

First of all in Java you cannot create dynamic names of variables. So

aS[i][j]= s+"i"+[j];   //s0[0],s0[1]...s15[3]

is incorrect

String[][] aS= new String[16][3];

This means you can have 16 1D String arrays each of size 3 i.e 3 Strings in each array.

for(int i=0;i<aS.length;i++)
{
        aS[i]= yourArray  //s0[0],s0[1]...s15[3]
}

Here yourArray should be String[] with size 3 similar to your S0 - S15.

or you can do

for(int i=0;i<aS.length;i++)
{
    for(int j=0;j<3;j++)
    {
        aS[i][j]= "FIELD" + j;
    }
}

1 Comment

i tried doing this: aS[i][j]= "s"+i[j]; now the error is: "The type of the expression must be an array type but it resolved to int"
1

If you want to initialize your multidimensional array you can do it like this:

String[][] aS = { {"FIELD0", "FIELD1", "FIELD2"},
                  {"FIELD0", "FIELD1", "FIELD2"}, 
                  {"FIELD0", "FIELD1", "FIELD2"},
                  ...
                  {"FIELD0", "FIELD1", "FIELD2"} };

1 Comment

yup,creating separate arrays just looked neat, i guess i'd go with this if looping isnt possible :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.