Dieser Artikel beschreibt die Implementierung von Matrizen wie Addition, Subtraktion, Multiplikation und Aufteilung und Transformation in Java. Teilen Sie es für Ihre Referenz wie folgt weiter:
Anfänger in Java schreiben Matrix -Budget -Programme als Tools für die Verwendung beim Schreiben von Algorithmen in der Zukunft.
öffentliche Klasse Matrixoperation {public static int [] [] add (int [] [] matrix_a, int [] [] matrix_b) {int row = matrix_a.length; int col = matrix_a [0] .Length; int [] [] result = new int [row] [col]; if (row! = matrix_b.length || col! } else {for (int i = 0; i <row; i ++) {für (int j = 0; j <col; j ++) {result [i] [j] = matrix_a [i] [j]+matrix_b [i] [j]; }}} Rückgabeergebnis; } public static int [] [] sub (int [] [] matrix_a, int [] [] matrix_b) {int row = matrix_a.length; int col = matrix_a [0] .Length; int [] [] result = new int [row] [col]; if (row! = matrix_b.length || col! } else {for (int i = 0; i <row; i ++) {für (int j = 0; j <col; j ++) {result [i] [j] = matrix_a [i] [j] - matrix_b [i] [j]; }}} Rückgabeergebnis; } public static int [] [] dot (int [] [] matrix_a, int [] [] [] matrix_b) { /** matrix_as Dimension m* p matrix_bs Dimension p* n. Rückgabedimension *m *n */ int row = matrix_a.length; int col = matrix_a [0] .Length; int [] [] result = new int [row] [col]; if (col! = matrix_b.length) {System.out.println ("fehler"); } else {for (int i = 0; i <row; i ++) {für (int j = 0; j <col; j ++) {result [i] [j] = 0; für (int k = 0; k <col; k ++) {result [i] [j]+= matrix_a [i] [k] * matrix_b [k] [j]; }}}} Rückgabeergebnis; } public static int [] [] dot (int [] [] matrix_a, int b) {int row = matrix_a.length; int col = matrix_a [0] .Length; int [] [] result = new int [row] [col]; für (int i = 0; i <row; i ++) {für (int j = 0; j <col; j ++) {result [i] [j] = matrix_a [i] [j] * b; }} Rückgabeergebnis; } public static int [] [] mul (int [] [] matrix_a, int [] [] matrix_b) { /** matrix_as Dimension m* n matrix_bs Dimension m* n. Rückgabedimension *m *n */ int row = matrix_a.length; int col = matrix_a [0] .Length; int [] [] result = new int [row] [col]; if (row! = matrix_b.length || col! } else {for (int i = 0; i <row; i ++) {für (int j = 0; j <col; j ++) {result [i] [j] = matrix_a [i] [j] * matrix_b [i] [j]; }} Rückgabeergebnis; } public static int [] [] transport (int [] [] matrix_a) {int row = matrix_a.length; int col = matrix_a [0] .Length; int [] [] result = new int [row] [col]; für (int i = 0; i <row; i ++) {für (int j = 0; j <col; j ++) {result [j] [i] = matrix_a [i] [j]; }} Rückgabeergebnis; } public static void print (int [] [] matrix) {int row = matrix.length; int col = matrix [0] .Length; für (int i = 0; i <row; i ++) {System.out.print ("["); für (int j = 0; j <col; j ++) {System.out.print (matrix [i] [j]); if (j! = col - 1) {System.out.print (","); }} System.out.print ("]/n"); }} public static void main (string [] args) {int [] [] a = {{1, 2}, {3, 4}}; int [] [] b = {{7, 8}, {6, 5}}; int [] [] c = add (a, b); System.out.println ("Wulin.com -Testergebnisse sind wie folgt:"); System.out.println ("matrix a ="); Druck (a); System.out.println ("matrix b ="); Druck (b); System.out.println ("Matrix a + b ="); Druck (c); c = sub (a, b); System.out.println ("Matrix a - b ="); Druck (c); int [] [] d = dot (a, b); System.out.println ("Matrix a Punkt b ="); Druck (d); int [] [] e = dot (a, 3); System.out.println ("Matrix a * 3 ="); Druck (e); int [] [] f = Transport (a); System.out.println ("Matrix at ="); Druck (f); int [] [] g = mul (a, b); System.out.println ("Matrix a * b ="); Druck (g); }}Auslaufergebnisse:
Für weitere Informationen zu Java -Algorithmen können Leser, die an dieser Website interessiert sind, die Themen "Java -Datenstruktur und Algorithmus -Tutorial", "Zusammenfassung der Java -Operation DOM -Knoten -Tipps", "Zusammenfassung der Java -Datei- und Verzeichnisoperationstipps" und "Zusammenfassung der Java -Cache -Operation Tipps" anzeigen
Ich hoffe, dieser Artikel wird für Java -Programme aller hilfreich sein.