views:

95

answers:

4

I have the following matrix

 1 2 3 4 5 6
 2 3 4 5 6 1
 3 4 5 6 1 2
 4 5 6 1 2 3
 5 6 1 2 3 4
 6 1 2 3 4 5

...and I want to have it in a table

 | 1 2 3 4 5 6
---------------
1| 1 2 3 4 5 6
2| 2 3 4 5 6 1
3| 3 4 5 6 1 2
4| 4 5 6 1 2 3
5| 5 6 1 2 3 4
6| 6 1 2 3 4 5

Having n elements is it possible to print such a table dynamically?

    public static void main ( String [ ] args ) {
        int n = Integer.parseInt(args[0]);
        for(int w = 1; w <= n; w++){
            System.out.println("");
            for(int p = w; p <= n + w - 1; p++){
                System.out.print((p-1)%n+1 + " ");
            }           
        }
    }
A: 

If I were you, I would take a look at libraries like OpenCSV. Although not specially fitted to your need, they will be of great use.

Indeed, your table display can be seen as a way to print a CSV file. As a consequence, building the structure fitted to this CSV file and printing it to System.out could do the job with a clean layout (although absolutely not optimal in terms of dependencies).

Riduidel
+1  A: 

Crude, But should work.

 //This method adds required number spaces for alignment.
 private static String formatString(String s, int in) {
        return String.format("%1$-" + (in + 1) + "s", s);
    }

    public static void main(String[] args) {    

        int n = Integer.parseInt(args[0]);          
        int in = args[0].length();
        System.out.print(formatString(" ", in));
        System.out.print("| ");

        for (int w = 1; w <= n; w++) {              
            System.out.print(formatString(w + "", in));
        }
        System.out.println("");
        for (int w = 1; w <= n + 2; w++) {
            System.out.print(formatString("-", in));
        }

        for (int w = 1; w <= n; w++) {
            System.out.println("");
            System.out.print(formatString((w + ""), in));
            System.out.print("| ");
            for (int p = w; p <= n + w - 1; p++) {

                System.out.print(formatString(((p - 1) % n + 1) + "", in));
            }
        }

    }

EDIT: Edited the code to consider alignment issues.

johnbk
+1  A: 

This can take pretty much anything you can throw at it:

public static void main(String[] args) {
    int[][] matrix = {
        { 1, 2, 3, 4, 5, 6 },
        { 2, 3, 4, 5, 6, 1 },
        { 3, 4, 5, 6, 1, 2 },
        { 4, 5, 6, 11, 2, 3 },
        { 5, 116, 1, 2, 3, 4 },
        { 5, 116, 1, 2, 3, 4 },
        { 5, 116, 1, 2, 3, 4 },
        { 5, 116, 1, 2, 3, 4 },
        { 5, 223523526, 1, 2, 3, 4 },
        { 5, 116, 1, 2, 3, 4 },
        { 5, 116, 1, 2, 3, 4 },
        { 5, 116, 1, 2, 3, 4 },
        { 5, 116, 1, 2, 3, 4 },
        { 6, 1, 2, 3, 4, 5 }
    };

    //compute the maximum header width (the number of columns)
    int rows = matrix.length;
    int cols = rows > 0 ? matrix[0].length : 0;
    int maxRowHeaderWidth = Integer.toString(rows).length();

    int maxCellWidth = 1;
    //compute the maximum cell width
    for ( int[] row : matrix ) {
        for ( int cell : row ) {
            maxCellWidth = Math.max(Integer.toString(cell).length(), maxCellWidth);
        }
    }
    maxCellWidth = Math.max(maxCellWidth, Integer.toString(cols).length());

    StringBuilder patternBuilder = new StringBuilder();

    //the header
    for ( int i = 0; i < maxRowHeaderWidth; i++ ) {
        patternBuilder.append(' ');
    }
    patternBuilder.append('|');
    for ( int i = 0; i < cols; i++ ) {
        patternBuilder.append(String.format("%" + (maxCellWidth+1) + "d", i+1));
    }
    patternBuilder.append('\n');


    //the line of dashes
    int rowWidth = patternBuilder.length();
    for ( int i = 0; i < rowWidth; i++ ) {
        patternBuilder.append('-');
    }
    patternBuilder.append('\n');

    //each row
    for ( int i = 0; i < matrix.length; i++ ) {
        patternBuilder.append(String.format("%" + (maxRowHeaderWidth) + "d|", i+1));
        for ( Integer cell : matrix[i] ) {
            patternBuilder.append(String.format("%" + (maxCellWidth+1) + "d", cell));
        }
        patternBuilder.append('\n');
    }

    System.out.println(patternBuilder);
}

It sizes every cell equally, regardless of the length of the number in the cell. It can take an arbitrary number of rows or columns.

Mark Peters
+1  A: 

Depends on the array, assuming that you know the array size beforehand sure. In Java arrays can be jigged aka subarrays do not have to be same size, what in mind:

For test case:

public static void main(String[] args) {
    System.out.println(toTableString(new int[][] { { 1, 2, 3, 4, 5, 6 },
            { 2, 3, 4, 5, 6, 1 }, { 3, 4, 5, 6, 1, 2 },
            { 4, 5, 6, 1, 2, 3 }, { 5, 6, 1, 2, 3, 4 },
            { 6, 1, 2, 3, 4, 5 } }));
    System.out.println();
    System.out.println(toTableString(new int[][] { { 1 }, { 2, 3 },
            { 3, 4, 5 }, { 4, 5, 6, 1 }, { 5, 6, 1, 2, 3 },
            { 6, 1, 2, 3, 4, 5 } }));
    System.out.println();
    System.out.println(toTableString(new int[][] { { 1 }, { 20, 300 },
            { 3000, 40000, 50000 }}));
}

Output:

 | 0 1 2 3 4 5 
——————————————
0| 1 2 3 4 5 6 
1| 2 3 4 5 6 1 
2| 3 4 5 6 1 2 
3| 4 5 6 1 2 3 
4| 5 6 1 2 3 4 
5| 6 1 2 3 4 5 

 | 0 1 2 3 4 5 
——————————————
0| 1 
1| 2 3 
2| 3 4 5 
3| 4 5 6 1 
4| 5 6 1 2 3 
5| 6 1 2 3 4 5 

     | 00000 00001 00002 
————————————————————————
00000| 00001 
00001| 00020 00300 
00002| 03000 40000 50000 

Run online: link

Margus
but `toTableString` is not a Java standard method...
Carlos Heuberger
You can see it under "Run online: link".
Margus