二维数组的两种遍历方式
public class Draft{
public static void main(String... args){
int [] a[]=new int[100][100];
//赋值
long t1=System.currentTimeMillis();
for(int i=0;i<100;i++){
for(int j=0;j<100;j++){
a[i][j]=i+j;
}
}
long t2=System.currentTimeMillis();
//第一种遍历方式
long t3=System.currentTimeMillis();
for(int i=0;i<100;i++){
for(int j=0;j<100;j++){
System.out.println(a[i][j]);
}
}
long t4=System.currentTimeMillis();
//第二种遍历方式
long t5=System.currentTimeMillis();
for(int j=0;j<100;j++){
for(int i=0;i<100;i++){
System.out.println(a[i][j]);
}
}
long t6 =System.currentTimeMillis();
System.out.println("...............over..........");
System.out.println("赋值的时间:"+(t2-t1)+"\n"+"第一种方法的时间:"+(t4-t3)+"\n"+"第二种方法的时间:"+(t6-t5));
}
}
........................................
赋值的时间:0
第一种方法的时间:199
第二种方法的时间:63
求解释?为什么遍历时,先列后行比先行后列速度快?