2016-06-26 5 views
-4

einfacher 2D-Array aus 4x6die Elemente des 2D-Array

public class arraytest { 

public static void main(String[] args) { 
int array[][] = new int[4][6]; 
int row = array.length; 
int col = array[0].length; 

System.out.println("Row length " + row); 
for (int i = 0; i < row; i++) 
    for (int j = 0; j < col; j++) { 
    array[1][1] = 5; 
    System.out.println("Array elements " + array[i][j]); 
    } 
} 

} 

angezeigten wie Array ist [1] [1] = 5 Funktionen: Der Ausgang ist wie folgt:

Row length 4 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 5 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 

wie kommen "Array-Elemente 5" ist in der 8. Position, i konnte die Logik nicht verstehen. > kann jemand die Logik erklären.

Antwort

1

Wenn mit Arrays arbeiten Sie, dass das erste Element wissen muss immer in der gespeichert wird 0 Index. Ich nehme an, dass Sie mit der Eingabe von Array [1] [1] den Wert 5 in der ersten Zeile, der ersten Spalte, speichern wollten. Allerdings bezieht sich [1] [1] aus dem oben genannten Grund tatsächlich auf die zweite Spalte der zweiten Zeile.

Daher Array wie folgt aussehen:

0 0 0 0 0 0 
0 5 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 

Schließlich, weil Ihre für Schleifen Spalten wie die innerste Schleife hat, wird es aus jeder Spalte in der ersten Zeile gedruckt werden, bevor zu der zweiten Reihe bewegt. Die erste Zeile gibt also sechs Nullen aus und die zweite Zeile gibt eine weitere 0 aus, bevor Sie schließlich den Wert erreichen, den Sie als achte Ausgabe festgelegt haben.

0

Array [1] [1] repräsentiert den Schnittpunkt der zweiten und der zweiten Spalte. Daher druckt die Schleife zuerst die erste Zeile (die 6 Elemente enthält) und in der zweiten Zeile die Arrays [1] [0] und Array [1] [1]. Also, Array [1] [1] ist in der 8. Position.

0

Sie setzen das Element 1,1 bis 5 ... der Rest wird den gleichen Wert besitzen ...

Row length 4 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 0 
Array elements 5 <--- here is he!! 
Array elements 0 
Array elements ... 
0

Ich glaube, dass Sie vergessen haben, dass der Index der Arrays bei beginnen!

dass ein 4 x 6 mehrdimensionales Array bedeutet, sieht wie folgt aus:

[0] [0] [0] [1] [0] [2] [0] [3] [0 ] [4] [0] [5] [1] [0] [1] [1] [1] [2] [1] [3] [1] [4] [1] [5] [2 [0] [2] [1] [2] [2] [2] [3] [2] [4] [2] [5] [3] [0] [3] [1] [3] [2] [3] [3] [3] [4] [3] [5]

Und wie Sie sehen können, ist der eine [1] [1] in der achten Position.

Wenn Sie deklarieren ein mehrdimensionales Array wie dieses Array [4] [6] es von 0 bis 3 und in diesen Zeilen von 0 bis 5 geht, weil der Array-Index beginnt bei 0 und nicht bei 1.

0

Wenn Sie neu in Java sind, beginnen Arrays mit einem Index von 0; genau wie das erste Zeichen eines Strings 0 ist. Betrachten Sie Längen oder den endgültigen Index eines Arrays oder String als, n-1. Diese 0-indexierte Logik gilt auch für die Spalten/Zeilen eines 2D-Arrays.

0

Siehe die Ausgabe dieses Beispiels wird Ihre Zweifel klar:

public class arraytest { 

public static void main(String[] args) { 
int array[][] = new int[4][6]; 
int row = array.length; 
int col = array[0].length; 

System.out.println("Row length " + row); 
System.out.println("Col length " + col); 
for (int i = 0; i < row; i++) 
    for (int j = 0; j < col; j++) { 
    array[1][1] = 5; 
    System.out.println("Array elements at row "+i+" and column "+j+" is "+ array[i][j]); 
    } 
} 

} 

Ausgang:

Row length 4                                          
Col length 6                                          
Array elements at row 0 and column 0 is 0                                  
Array elements at row 0 and column 1 is 0                                  
Array elements at row 0 and column 2 is 0                                  
Array elements at row 0 and column 3 is 0                                  
Array elements at row 0 and column 4 is 0                                  
Array elements at row 0 and column 5 is 0                                  
Array elements at row 1 and column 0 is 0                                  
Array elements at row 1 and column 1 is 5                                  
Array elements at row 1 and column 2 is 0                                  
Array elements at row 1 and column 3 is 0                                  
Array elements at row 1 and column 4 is 0                                  
Array elements at row 1 and column 5 is 0                                  
Array elements at row 2 and column 0 is 0                                  
Array elements at row 2 and column 1 is 0                                  
Array elements at row 2 and column 2 is 0                                  
Array elements at row 2 and column 3 is 0                                  
Array elements at row 2 and column 4 is 0                                  
Array elements at row 2 and column 5 is 0                                  
Array elements at row 3 and column 0 is 0                                  
Array elements at row 3 and column 1 is 0                                  
Array elements at row 3 and column 2 is 0                                  
Array elements at row 3 and column 3 is 0                                  
Array elements at row 3 and column 4 is 0                                  
Array elements at row 3 and column 5 is 0 
Verwandte Themen