0Pricing
Java Academy · Lektion

Zeilen-/Spaltenmaxima, Transponieren und Formatierung

Ermitteln Sie sicher Zeilen- und Spaltenmaxima (auch für gezackte Arrays) und erstellen Sie transponierte Matrizen (rechteckig und gezackt). Üben Sie die ausgerichtete Ausgabe.

Zeilen-/Spaltenmaxima, Transponieren und Formatierung ist eine kostenlose Java Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 6. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Java Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Java Academy-Kurs umfasst insgesamt 6 Lektionen.

Zeilenmaxima

Zeilenmaxima: Berechnen Sie das Maximum jeder Zeile, indem Sie ihre Elemente einmal durchsuchen.

public class Main {
  public static void main(String[] args) {
    // Example jagged array
    int[][] m = {
      {1, 7, 3},
      {4, 9, 2, 11},
      {6}
    };

    int r = 1; // choose which row to check (row index 1 → {4, 9, 2, 11})

    // Max of a single row r
    int max = Integer.MIN_VALUE; // start with very small number
    for (int c = 0; c < m[r].length; c = c + 1) {
      if (m[r][c] > max) {
        max = m[r][c]; // update if current value is larger
      }
    }

    System.out.println("Max value in row " + r + " = " + max);
  }
}

Spaltenmaxima

Spaltenmaxima in unregelmäßigen Arrays: Prüfen Sie vor dem Lesen der Spalte c die Länge jeder Zeile.

public class Main {
  public static void main(String[] args) {
    // Example jagged array (rows with different lengths)
    int[][] m = {
      {1, 7, 3},
      {4, 9},       // shorter row (only 2 columns)
      {6, 2, 11, 5} // longer row (4 columns)
    };

    int c = 2; // column index to check (3rd column)

    // Max of column c across all rows
    int colMax = Integer.MIN_VALUE;
    for (int r = 0; r < m.length; r = r + 1) {
      // Guard: only access if this row has column c
      if (c < m[r].length) {
        if (m[r][c] > colMax) {
          colMax = m[r][c]; // update maximum
        }
      }
    }

    System.out.println("Max value in column " + c + " = " + colMax);
  }
}

Rechteckige Transponierung

Transponierung (rechteckig): Vertauschen Sie Zeilen und Spalten, wenn alle Zeilen dieselbe Länge haben.

public class Main {
  public static void main(String[] args) {
    // Example 2D rectangular matrix (3 rows × 2 columns)
    int[][] m = {
      {1, 2},
      {3, 4},
      {5, 6}
    };

    // Dimensions
    int rows = m.length;       // number of rows in m
    int cols = m[0].length;    // number of columns in m

    // Transposed matrix (C × R)
    int[][] t = new int[cols][rows];

    // Fill transposed matrix
    for (int r = 0; r < rows; r = r + 1) {
      for (int c = 0; c < cols; c = c + 1) {
        t[c][r] = m[r][c];  // swap row/column
      }
    }

    // Print original matrix
    System.out.println("Original matrix:");
    for (int r = 0; r < rows; r++) {
      for (int c = 0; c < cols; c++) {
        System.out.print(m[r][c] + " ");
      }
      System.out.println();
    }

    // Print transposed matrix
    System.out.println("Transposed matrix:");
    for (int r = 0; r < t.length; r++) {
      for (int c = 0; c < t[r].length; c++) {
        System.out.print(t[r][c] + " ");
      }
      System.out.println();
    }
  }
}

Transponierung unregelmäßiger Arrays

Transponierung (unregelmäßig): Erstellen Sie Spalten als Zeilen, bestimmen Sie deren Größe anhand der längsten Zeile und sichern Sie die Lesezugriffe ab.

public class Main {
  public static void main(String[] args) {
    // Example jagged array (rows of different lengths)
    int[][] m = {
      {1, 2, 3},
      {4, 5},
      {6}
    };

    // 1) Find the maximum number of columns across all rows
    int maxCols = 0;
    for (int r = 0; r < m.length; r = r + 1) {
      if (m[r].length > maxCols) {
        maxCols = m[r].length;
      }
    }

    // 2) Create transpose: it will have maxCols rows
    int[][] t = new int[maxCols][];

    // 3) Fill the transposed matrix
    for (int c = 0; c < maxCols; c = c + 1) {
      // Each transposed row has as many elements as original rows
      t[c] = new int[m.length];
      for (int r = 0; r < m.length; r = r + 1) {
        // If row is too short, pad with 0
        t[c][r] = (c < m[r].length) ? m[r][c] : 0;
      }
    }

    // Print original jagged array
    System.out.println("Original jagged array:");
    for (int r = 0; r < m.length; r++) {
      for (int c = 0; c < m[r].length; c++) {
        System.out.print(m[r][c] + " ");
      }
      System.out.println();
    }

    // Print transposed array
    System.out.println("Jagged-safe transpose:");
    for (int r = 0; r < t.length; r++) {
      for (int c = 0; c < t[r].length; c++) {
        System.out.print(t[r][c] + " ");
      }
      System.out.println();
    }
  }
}

Formatierungshilfen

Verwenden Sie Hilfsmethoden, um Spaltenbreiten zu berechnen und ausgerichtete Matrizen zum Vergleich auszugeben.

public class Main {

  // Compute per-column widths for a (possibly jagged) 2D int array.
  // For each column index c, we find the longest string length among m[r][c] (for rows that have that column).
  static int[] widths(int[][] m) {
    // 1) Find the maximum number of columns across all rows
    int maxCols = 0;
    for (int r = 0; r < m.length; r = r + 1) {
      if (m[r].length > maxCols) maxCols = m[r].length;
    }

    // 2) Prepare width array (initialized to 0)
    int[] w = new int[maxCols];

    // 3) For each existing cell, compute the string length and keep the max per column
    for (int r = 0; r < m.length; r = r + 1) {
      for (int c = 0; c < m[r].length; c = c + 1) {
        int len = Integer.toString(m[r][c]).length();
        if (len > w[c]) w[c] = len;
      }
    }

    // 4) Ensure each column has at least width 1
    for (int c = 0; c < w.length; c = c + 1) {
      if (w[c] < 1) w[c] = 1;
    }
    return w;
  }

  // Print a (possibly jagged) matrix so that each column is right-aligned using the widths[] computed above.
  static void printAligned(int[][] m) {
    int[] w = widths(m); // column widths

    for (int r = 0; r < m.length; r = r + 1) {
      String line = "";

      for (int c = 0; c < m[r].length; c = c + 1) {
        // Convert number to string
        String s = Integer.toString(m[r][c]);

        // Left-pad with spaces until it matches the target column width
        while (s.length() < w[c]) s = " " + s;

        // Append to the line; add a single space between columns (but not after the last one)
        line = line + s + (c + 1 < m[r].length ? " " : "");
      }

      // Print the fully assembled row
      System.out.println(line);
    }
  }

  public static void main(String[] args) {
    // Demo with a jagged matrix (different row lengths)
    int[][] m = {
      {1, 200, 3},
      {45, 6},
      {7, 89, 1000, 11}
    };

    System.out.println("Aligned output:");
    printAligned(m);
  }
}

Transponierungs-Demo

Führen Sie es aus: Vergleichen Sie die ausgerichteten Ausgaben einer rechteckigen Matrix und ihrer Transponierung. Sehen Sie anschließend eine unregelmäßige Matrix, ihre Spaltenmaxima und eine aufgefüllte Transponierung.

public class Main {
  // Compute column maxima (jagged-safe)
  static int[] colMaxima(int[][] m) {
    int maxCols = 0;
    for (int r = 0; r < m.length; r = r + 1) if (m[r].length > maxCols) maxCols = m[r].length;
    int[] max = new int[maxCols];
    for (int c = 0; c < maxCols; c = c + 1) {
      int best = Integer.MIN_VALUE;
      boolean seen = false;
      for (int r = 0; r < m.length; r = r + 1) {
        if (c < m[r].length) {
          if (!seen || m[r][c] > best) { best = m[r][c]; seen = true; }
        }
      }
      max[c] = seen ? best : 0;
    }
    return max;
  }

  // Transpose rectangular
  static int[][] transposeRect(int[][] m) {
    int rows = m.length;
    int cols = m[0].length;
    int[][] t = new int[cols][rows];
    for (int r = 0; r < rows; r = r + 1) {
      for (int c = 0; c < cols; c = c + 1) t[c][r] = m[r][c];
    }
    return t;
  }

  // Jagged-safe transpose (pads missing with 0)
  static int[][] transposeJagged(int[][] m) {
    int maxCols = 0;
    for (int r = 0; r < m.length; r = r + 1) if (m[r].length > maxCols) maxCols = m[r].length;
    int[][] t = new int[maxCols][];
    for (int c = 0; c < maxCols; c = c + 1) {
      t[c] = new int[m.length];
      for (int r = 0; r < m.length; r = r + 1) t[c][r] = (c < m[r].length) ? m[r][c] : 0;
    }
    return t;
  }

  // Helpers
  static int[] widths(int[][] m) {
    int maxCols = 0;
    for (int r = 0; r < m.length; r = r + 1) if (m[r].length > maxCols) maxCols = m[r].length;
    int[] w = new int[maxCols];
    for (int r = 0; r < m.length; r = r + 1) {
      for (int c = 0; c < m[r].length; c = c + 1) {
        int len = Integer.toString(m[r][c]).length();
        if (len > w[c]) w[c] = len;
      }
    }
    for (int c = 0; c < w.length; c = c + 1) if (w[c] < 1) w[c] = 1;
    return w;
  }

  static void printAligned(int[][] m) {
    int[] w = widths(m);
    for (int r = 0; r < m.length; r = r + 1) {
      String line = "";
      for (int c = 0; c < m[r].length; c = c + 1) {
        String s = Integer.toString(m[r][c]);
        while (s.length() < w[c]) s = " " + s;
        line = line + s + (c + 1 < m[r].length ? " " : "");
      }
      System.out.println(line);
    }
  }

  public static void main(String[] args) {
    // Rectangular example for transpose
    int[][] rect = {
      {1, 2, 3},
      {4, 5, 6}
    };

    // Jagged example for maxima and jagged transpose
    int[][] jag = {
      {9, 12, 7},
      {5},
      {8, 10}
    };

    System.out.println("rect:");
    printAligned(rect);
    System.out.println("transpose(rect):");
    printAligned(transposeRect(rect));

    System.out.println("jag (jagged):");
    printAligned(jag);
    int[] cm = colMaxima(jag);
    System.out.print("col maxima: ");
    for (int i = 0; i < cm.length; i = i + 1) System.out.print(cm[i] + (i + 1 < cm.length ? " " : ""));
    System.out.println();

    System.out.println("transpose(jag, padded):");
    printAligned(transposeJagged(jag));
  }
}

Prüfung: Spaltenmaximum

Kurzprüfung: Wie berechnen Sie für eine unregelmäßige Matrix das Maximum der Spalte c?

Zusammenfassung & Nächste Schritte

Zusammenfassung: Sie haben Zeilen- und Spaltenmaxima berechnet, rechteckige Matrizen transponiert und eine für unregelmäßige Arrays geeignete Transponierung mit Auffüllung erstellt. Außerdem haben Sie die Ausgabe ausgerichtet formatiert.

Häufig gestellte Fragen

Ist die Lektion „Zeilen-/Spaltenmaxima, Transponieren und Formatierung“ kostenlos?

Ja — der vollständige Text von „Zeilen-/Spaltenmaxima, Transponieren und Formatierung“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Java Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Java Academy-Kurs umfasst insgesamt 6 Lektionen.

Was lerne ich in „Zeilen-/Spaltenmaxima, Transponieren und Formatierung“?

Ermitteln Sie sicher Zeilen- und Spaltenmaxima (auch für gezackte Arrays) und erstellen Sie transponierte Matrizen (rechteckig und gezackt). Üben Sie die ausgerichtete Ausgabe. Du übst Java Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Java Academy zu starten?

Keine Vorkenntnisse erforderlich. Java Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 6.

Wie lange dauert die Lektion „Zeilen-/Spaltenmaxima, Transponieren und Formatierung“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Java Academy-Lektion Code schreiben und ausführen?

Ja. Jede Java Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Grundlagen zweidimensionaler Arrays und verschachtelte Iteration
  2. Gezackte Arrays und Tabellenformatierung
  3. Zeilen-/Spaltenmaxima, Transponieren und Formatierung
  4. 2D-Mini-Projekt: Notenverwaltung
  5. 2D-Hilfsfunktionen als Hilfsklasse (Refactoring)
  6. Bereinigung und Validierung von 2D-Daten
← Zurück zu Java Academy