4

I can't seem to figure out how to get Eclipse to format multidimensional arrays the way I want it to...

int firstarray[][] =
{
    { 1, 2, 3, 4 },
    { 5, 6, 7, 8 },
    { 9, 10, 11, 12 },
};

That's what I would like, but despite changing line wrapping, brace positions and new lines for array initializers, I can't seem to get it to look anything like that. The closest I've been able to get is where none of it is indented, and that's really bothering me. :(

4
  • 1
    Tried asking on Eclipse forums instead? Commented Jun 23, 2013 at 14:28
  • What do you want to do? Commented Jun 25, 2013 at 18:46
  • Remove the comma after the third group. Commented Jun 28, 2013 at 19:54
  • 1
    While this question tangentially involves Eclipse, it is really more about the particular formatting vagaries of an IDE and not about the actual programming functions provided by the tool.
    – Thorn G
    Commented Jul 9, 2013 at 3:23

2 Answers 2

3

I couldn't get exactly what you wanted, but I got something kinda close to hopefully this helps.

int firstarray[][] =
{
    {
        1, 2, 3, 4
    },
    {
        5, 6, 7, 8
    },
    {
        9, 10, 11, 12
    }
};

First go to window -> preferences, and under java -> formatter, you can edit the active profile.

Under the braces tab for array initializer set it to "next line"

Under the new lines tab for array ititializers check both "insert new line after opening brace of array initializer" and "insert new line before closing brace of array initializer"

Finally in the line wrapping tab under expressions click on the array initializers and set it to "wrap all elements, every element on a new line" and indentation policy set to "indent by one"

Now just source -> format to get the result and you're done.

0

Unfortunately I couldn't find a great solution either... What you can do is:

{ { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
//
{ { 0, -1 }, { 0, 0 }, { -1, 0 }, { -1, 1 } },
//
{ { 0, -1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } },
//
{ { 0, -1 }, { 0, 0 }, { 0, 1 }, { 0, 2 } } };

(just comment out the lines in between all sub-arrays)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.