多维数组
打印二维数组
package array ;
public class Demo5 {
public static void main ( String [ ] args) {
int [ ] [ ] array = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } } ;
printArray ( array[ 0 ] ) ;
}
public static void printArray ( int [ ] arrays) {
for ( int i = 0 ; i < arrays. length; i++ ) {
System . out. print ( arrays[ i] + " " ) ;
}
}
}
打印二维数组元素
package array ;
public class Demo5 {
public static void main ( String [ ] args) {
int [ ] [ ] array = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } } ;
System . out. println ( array[ 0 ] [ 0 ] ) ;
System . out. println ( array[ 2 ] [ 1 ] ) ;
}
public static void printArray ( int [ ] arrays) {
for ( int i = 0 ; i < arrays. length; i++ ) {
System . out. print ( arrays[ i] + " " ) ;
}
}
}
package array ;
public class Demo5 {
public static void main ( String [ ] args) {
int [ ] [ ] array = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } } ;
for ( int i = 0 ; i < array. length; i++ ) {
for ( int j = 0 ; j < array[ i] . length ; j++ ) {
System . out. println ( array[ i] [ j] ) ;
}
}
}
public static void printArray ( int [ ] arrays) {
for ( int i = 0 ; i < arrays. length; i++ ) {
System . out. print ( arrays[ i] + " " ) ;
}
}
}
获取数组长度
package array ;
public class Demo5 {
public static void main ( String [ ] args) {
int [ ] [ ] array = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } } ;
System . out. println ( array. length) ;
System . out. println ( array[ 0 ] . length) ;
}
public static void printArray ( int [ ] arrays) {
for ( int i = 0 ; i < arrays. length; i++ ) {
System . out. print ( arrays[ i] + " " ) ;
}
}
}