这两天写了一个小的JAVA游戏.当然做游戏一般都会用到数组.遇到了很多问题,记录一下!
int a[][]=new int[4][4];
int b[][]=new int[4][4];
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
a[i][j]=i+j;
}
}
a=b;
这里面有两个数组a,b但只有一个数组对象.虽然在开始a,b都是初始化了的.在a=b时,就是数组的引用;所以说后面对a或b的操作.都会影响另一个的结果;
还要讲一下的就是数据在函数中的传递是引用传递。不像其它对象在传递时会复制本身
public class Test {
public static void main(String args[]){
int a[][]=new int[4][4];
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
a[i][j]=i+j;
}
}
int[][] b= new Test().testArray(a);
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(a[i][j]+" ");
}
System.out.println("\n");
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(b[i][j]+" ");
}
System.out.println("\n");
}
//test INT;
int i=20;
int Y=new Test().testInt(i);
System.out.print(i+"##"+Y);
}
public int[][] testArray(int[][] b){
int[][] a;
a=b;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
a[i][j]=i*j;
}
}
return a;
}
public int testInt(int b){
int a=10;
b=a*b;
return b;
}
}
下面是输出结果:
0 0 0 0
0 1 2 3
0 2 4 6
0 3 6 9
0 0 0 0
0 1 2 3
0 2 4 6
0 3 6 9
20##200