Creating Two-Dimensional String Array for Plane Seats I'm creating a ticket reservation system in Java, I want to use a Two-dimensional String array for managing the seats. I have enums that hold the plane model and the amount of seats available so the array pulls the rows and columns from there. I just want to see efficient my code is and how can I improve it.
    public String[][] createSeatArray() {
    String[] alphabet = {&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;,&quot;i&quot;,&quot;j&quot;,&quot;k&quot;,&quot;l&quot;,&quot;m&quot;,&quot;n&quot;,&quot;o&quot;,&quot;p&quot;,&quot;q&quot;,&quot;r&quot;
            ,&quot;s&quot;,&quot;t&quot;,&quot;u&quot;,&quot;v&quot;,&quot;w&quot;,&quot;x&quot;,&quot;y&quot;,&quot;z&quot;,&quot;aa&quot;,&quot;bb&quot;,&quot;cc&quot;,&quot;dd&quot;,&quot;ee&quot;,&quot;ff&quot;,&quot;gg&quot;,&quot;hh&quot;,&quot;ii&quot;,&quot;jj&quot;,&quot;kk&quot;,&quot;ll&quot;,&quot;mm&quot;,&quot;nn&quot;,&quot;oo&quot;,&quot;pp&quot;};

    String[][] seatArray = new String[this.getNumOfRows()][this.getNumOfColumns()];
    int alphabetPos = 0;
    for(int i = 0; i &lt; this.getNumOfRows(); i++) {
        for (int j = 1; j &lt;= this.getNumOfColumns(); j++){
            String columnToString = String.valueOf(j);
            seatArray[i][j-1] = alphabet[alphabetPos] + columnToString;

            if (this.getNumOfColumns() == 4) {
                if (j == 2) {
                    System.out.print(seatArray[i][j-1] + &quot;       &quot;);
                } else {
                    System.out.print(seatArray[i][j-1] + &quot; &quot;);
                }
            } else {
                if (j == 3) {
                    System.out.print(seatArray[i][j-1] + &quot;       &quot;);
                } else {
                    System.out.print(seatArray[i][j-1] + &quot; &quot;);
                }
            }
        }
        System.out.println(&quot;\n&quot;);
        alphabetPos++;
    }
    return seatArray;
}

Thank you for any feedback! also keep in mind that if the indentation is off it may be due to bringing it over to stack overflow, on intellij it's perfect.
Here are a few optimization suggestions:

You can get rid of alphabetPos because it is always equals to i.
In fact, while you are at it, your code will be more readable if you rename i to row and j to col or column.

You can also get rid of columnToString and just rely on Java's string concatenation overloads.


 seatArray[i][j-1] = alphabet[alphabetPos] + j;

Note that you use j multiple times as index, subtracting one, and only once as string.
It would make your code more readable if you used zero based index, and only added 1 for the string composition.

You may want to use string formatting.



for (int col = 0; col &lt; this.getNumOfColumns(); col++) {
        seatArray[row][col] = String.format(&quot;%s%d&quot;, alphabet[row], col + 1);

        if (this.getNumOfColumns() == 4) {
            if (col == 1) {
                System.out.print(seatArray[row][col] + &quot;       &quot;);
            } else {
                System.out.print(seatArray[row][col] + &quot; &quot;);
            }
        } else {
            if (col == 2) {
                System.out.print(seatArray[row][col] + &quot;       &quot;);
            } else {
                System.out.print(seatArray[row][col] + &quot; &quot;);
            }
        }
}


You can simplify your array printing by splitting it in to two parts: printing the seat number and printing the spacer.
The first part is always the same, so no need to repeat it 4 times.
The second part changes only when col and row have certain values, so one condition is enough.


System.out.print(seatArray[row][col] + &quot; &quot;); //this part is always printed the same

//just add a few more spaces if this is an isle seat
if ((getNumOfColumns() == 4 &amp;&amp; col == 1) || (getNumOfColumns() != 4 &amp;&amp; col == 2)) {
    System.out.print(&quot;      &quot;);
}


Finally, to save memory, you can get rid of the alphabet array all together, since the seat letters are sequential and rows 26 and above just have the same letter twice:


String rowStr = String.format(&quot;%c&quot;, 'a' + (row % 26));
if (row &gt; 25) rowStr += rowStr;
seatArray[row][col] = String.format(&quot;%s%d&quot;, rowStr, col + 1);