2008-03-06
java数组
这两天写了一个小的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
发表评论
最近加入圈子
最新评论
-
mysql乱码
"JSP的request 默认为ISO8859_1,所以在处理中文的时候, "何 ...
-- by senbao18 -
对synchronized的一点认识
怎么回复了,还删不了?路过,路过,不好意思
-- by dennis_zane -
对synchronized的一点认识
呵呵,JAVA新人。初次发贴!
-- by heshencao







评论排行榜