2010-10-21 20 views
1

je la matrice suivanteImprimer une table cmd en java

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 

... et je veux l'avoir dans une 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 

Avoir n éléments est-il possible d'imprimer cette une table dynamiquement?

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 + " "); 
      }   
     } 
    } 
+0

oui je crois que votre bloc de code montre qu'il est dynamique, accordé je n'ai pas exécuté le code – Woot4Moo

+0

ya son propre, quel est le Q.? –

+0

comment envelopper dynamiquement le contenu de la table dans la disposition de table –

Répondre

1

Dépend de la matrice, en supposant que vous connaissiez la taille du tableau auparavant. Dans les tableaux Java peuvent être jigged alias sous-tableaux ne doivent pas être la même taille, ce qui à l'esprit:

Pour le cas de test:

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 }})); 
} 

Sortie:

| 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 en ligne: link

+0

mais 'toTableString' n'est pas une méthode standard Java ... –

+0

Vous pouvez le voir sous" Exécuter en ligne: lien ". – Margus

0

Si je vous, je prendrais un coup d'œil dans les bibliothèques comme OpenCSV. Bien que n'étant pas spécialement adapté à vos besoins, ils seront d'une grande utilité.

En effet, votre affichage de table peut être vu comme un moyen d'imprimer un fichier CSV. Par conséquent, la construction de la structure adaptée à ce fichier CSV et son impression dans System.out pourraient faire le travail avec une mise en page propre (bien que absolument pas optimale en termes de dépendances).

1

Crude, mais devrait fonctionner.

//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: a édité le code pour prendre en compte les problèmes d'alignement.

1

Cela peut prendre quoi que ce soit à peu près, vous pouvez jeter à elle:

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); 
} 

Il tailles toutes les cellules aussi, quelle que soit la longueur du numéro dans la cellule. Il peut prendre un nombre arbitraire de lignes ou de colonnes.