2012-11-25 6 views
7

Ich habe noch eine andere Java Frage :)Kopieren von zwei zweidimensionalen Arrays an einen anderen bidimensional Array Java

ich this Thread gelesen haben, wo es klar erklärt, aber ich habe zwei bidimensional Arrays, die ich möchte Kopieren.

Ich verstehe, dass dieses Stück Code

int[] array1and2 = new int[array1.length + array2.length]; 
System.arraycopy(array1, 0, array1and2, 0, array1.length); 
System.arraycopy(array2, 0, array1and2, array1.length, array2.length); 

Aber meine Frage ist, wie kann ich es verschmelzen mit zwei Arrays, wo

int a1[][] = new int [3][3]; 
int b1[][] = new int [3][3]; 
int c1[][] = new int [3][6]; 

Wo c1 die Verschmelzung von oben genannten Arrays ist?

+3

Verwenden Sie einfach 'for' loop und verwenden Sie' arraycopy' darin. Hast du sie probiert? –

+0

Stellen Sie sich vor, die 2. Dimension Ihres Arrays ist das eindimensionale Array, das Sie kombinieren möchten. Also Schleife über die erste Dimension und mach das Zeug, das du bereits gefunden hast, in die zweite Dimension. – timaschew

+0

Da gibt es zwei Dimensionen, es ist nicht klar, wie sie zusammengeführt werden sollen. Ein Beispiel wäre gut. –

Antwort

3

Verwenden Lösung von task, dass Sie in der Frage erwähnt haben. Beispiel:

import java.util.Arrays; 

public class ArrayProgram { 

    public static void main(String[] args) { 
     int[][] array1 = { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } }; 
     int[][] array2 = { { 4, 5, 6 }, { 7, 8, 9 }, { 0, 1, 2 } }; 
     int[][] result = ArrayCopier.joinSecondDimension(array1, array2); 
     for (int[] array : result) { 
      System.out.println(Arrays.toString(array)); 
     } 
    } 
} 

class ArrayCopier { 

    public static int[][] joinSecondDimension(int[][] array1, int[][] array2) { 
     int[][] array1and2 = new int[array1.length][]; 
     for (int index = 0; index < array1.length; index++) { 
      array1and2[index] = join(array1[index], array2[index]); 
     } 
     return array1and2; 
    } 

    public static int[] join(int[] array1, int[] array2) { 
     int[] array1and2 = new int[array1.length + array2.length]; 
     System.arraycopy(array1, 0, array1and2, 0, array1.length); 
     System.arraycopy(array2, 0, array1and2, array1.length, array2.length); 
     return array1and2; 
    } 
} 

Drucke:

[1, 2, 3, 4, 5, 6] 
[1, 2, 3, 7, 8, 9] 
[1, 2, 3, 0, 1, 2] 

EDIT
Implementierung für alle Argumente Nummer (Variable-Length Argument Lists):

import java.util.Arrays; 

public class ArrayProgram { 

    public static void main(String[] args) { 
     int[][] array1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; 
     int[][] array2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; 
     int[][] array3 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; 
     test(array1); 
     test(array1, array2); 
     test(array1, array2, array3); 
    } 

    private static void test(int[][]... arrays) { 
     int[][] result = ArrayCopier.joinSecondDimension(arrays); 
     for (int[] array : result) { 
      System.out.println(Arrays.toString(array)); 
     } 
     System.out.println(); 
    } 
} 

class ArrayCopier { 

    public static int[][] joinSecondDimension(int[][]... arrays) { 
     int firstArrayLength = arrays[0].length; 
     int[][] result = new int[firstArrayLength][]; 
     for (int index = 0; index < firstArrayLength; index++) { 
      result[index] = join(getSecondDimArrays(index, arrays)); 
     } 
     return result; 
    } 

    public static int[] join(int[]... arrays) { 
     int[] result = new int[getTotalLength(arrays)]; 
     int destPos = 0; 
     for (int[] array : arrays) { 
      System.arraycopy(array, 0, result, destPos, array.length); 
      destPos += array.length; 
     } 
     return result; 
    } 

    private static int getTotalLength(int[]... arrays) { 
     int length = 0; 
     for (int[] array : arrays) { 
      length += array.length; 
     } 
     return length; 
    } 

    private static int[][] getSecondDimArrays(int index, int[][]... arrays) { 
     int[][] result = new int[arrays.length][]; 
     int resultIndex = 0; 
     for (int[][] array : arrays) { 
      result[resultIndex++] = array[index]; 
     } 
     return result; 
    } 
} 

Drucke:

[1, 2, 3] 
[4, 5, 6] 
[7, 8, 9] 

[1, 2, 3, 1, 2, 3] 
[4, 5, 6, 4, 5, 6] 
[7, 8, 9, 7, 8, 9] 

[1, 2, 3, 1, 2, 3, 1, 2, 3] 
[4, 5, 6, 4, 5, 6, 4, 5, 6] 
[7, 8, 9, 7, 8, 9, 7, 8, 9] 
+0

Das ist genau das, was ich brauche .... Edit: Aber warum String BTW ??? Diese Zeile: System.out.println (Arrays.toString (Array)); –

+0

Wenn ich die Methode "Arrays.toString" nicht verwendet habe, könnte die Anwendung folgende Ausgabe ausgeben: [I @ 63e68a2b [I @ 3479404a [I @ 46bd530. Dokumentation: "Gibt eine Zeichenfolgendarstellung des Inhalts des angegebenen Arrays zurück. Die Zeichenfolgendarstellung besteht aus einer Liste der Elemente des Arrays, eingeschlossen in eckigen Klammern (" [] ") ..." Ohne es könnte nur der Verweis auf angezeigt werden eine Anordnung. Wenn diese Antwort für Sie in Ordnung ist, markieren Sie sie bitte als "akzeptierte Antwort". –

+0

Danke. Es funktionierte auch so gut.Ich werde versuchen, herauszufinden, wie Sie 9 Arrays von je 3x3 drucken können. Die Sache ist, ich mache ein Sudoku und brauche jedes Array isoliert, wie ich seine Summe pro Array überprüfen muss, ob sie übereinstimmen oder nicht. –

1

So etwas sollte perfekt funktionieren?

int a1[][] = new int [3][3]; 
int b1[][] = new int [3][3]; 
int c1[][] = new int [3][6]; 

for (int i = 0; i < a1.length; i++) { 
    System.arraycopy(a1[i], 0, c1[i], 0, a1[i].length); 
    System.arraycopy(a2[i], 0, c1[i], a1[i].length, a2[i].length); 
} 

Und übrigens, nahm ich von Ihren Dimensionen, die c wie folgt aussehen:

[ a, a, a, , , ] 
[ a, a, a, , b, ] 
[ a, a, a, , , ] 
Verwandte Themen