How can i make this code shorter? *

Well, in that case it could be reduced very simply:

// largest length of asterisks wanted
final int count = 9;
// copy stores the current length of asterisks 
final StringBuilder copy = new StringBuilder(count);

// the total amount of lengths to print
final int end = 2 * count - 1;
final String[] lengths = new String[end];

// iterate over half the array including the middle
for (int i = 0; i < count; i++) {
    // grow the length by 1
    copy.append('*');

    // the array is mirrored, so a single
    // length can be applied to two areas
    lengths[i] = copy.toString();
    lengths[end - i - 1] = copy.toString();
}

// print out the array lengths
for (final String length : lengths) {
    System.out.println(length);
}

This works, as we can assume that given some function to return the string of n asterisks:

copy(0) = ""
copy(1) = "*"
copy(2) = "**"
...
copy(n) = copy(n - 1) + "*"

So instead of completely regenerating the entire length of asterisks (as your inner-most loop did), we can use the last value.

We can calculate the number of lengths needed fairly simply. To print out n asterisks, we need to print out 1 asterisk, then 2 asterisks, then 3 asterisks,... all the way to n asterisks. This is a total of n lines. This gives us that the number of lines we need will be 2 * n - 1. The - 1 coming from the fact we don't need to print the middle one twice.

This is because the array is mirrored. So the first and last elements are the same. The second and second-last elements are the same. The third and third-last elements are the same, ...etc.

We can use the mirror property to generate the array from both sides. So printing it out at certain steps would give you:

[null, null, null, null, null]
["*", null, null, null, "*"]
["*", "**", null, "**", "*"]
["*", "**", "***", "**", "*"]

You could of course reduce from here to even less, however you start to risk obscuring your code too much. This is probably one of the last readable reductions you could make.

If you have any questions about this, please do ask. This is by no means something a beginner should be entirely comfortable with, so I doubt it will make immediate sense.

/r/javahelp Thread Parent