简体   繁体   中英

Eclipse formatter - Multidimensional array initialization

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. :(

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM