java数组 方法参数传递问题
publicclassTest{publicstaticvoidmain(String[]args){inta[]=newint[3];intb[]=newint[3];...
public class Test {
public static void main(String[] args) {
int a[]=new int[3];int b[]=new int[3];
// TODO Auto-generated method stub
Test test=new Test();
a[0]=a[1]=a[2]=4;b[0]=b[1]=b[2]=5;
test.fun(a,b);
// b=a;
System.out.println("b "+b[0]+" "+b[1]+" "+b[2]);
}
public void fun(int a[],int b[]){
b=a;
}
输出的是
b 5 5 5
而不是 b 4 4 4 也就是fun方法没有用,但是如果在main方法里使用b=a,则输出
b 4 4 4,请问为什么fun方法没有起作用 展开
public static void main(String[] args) {
int a[]=new int[3];int b[]=new int[3];
// TODO Auto-generated method stub
Test test=new Test();
a[0]=a[1]=a[2]=4;b[0]=b[1]=b[2]=5;
test.fun(a,b);
// b=a;
System.out.println("b "+b[0]+" "+b[1]+" "+b[2]);
}
public void fun(int a[],int b[]){
b=a;
}
输出的是
b 5 5 5
而不是 b 4 4 4 也就是fun方法没有用,但是如果在main方法里使用b=a,则输出
b 4 4 4,请问为什么fun方法没有起作用 展开
3个回答
展开全部
public int[] fun(int a[] ,int b[] ){
b=a;
return b;
}
b=test.fun(a,b);
System.out.println("b "+b[0]+" "+b[1]+" "+b[2]);
值传递问题
b=a;
return b;
}
b=test.fun(a,b);
System.out.println("b "+b[0]+" "+b[1]+" "+b[2]);
值传递问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int是java的基本数据类型,在进行方法传递参数的时候,方法参数只是复制了原来变量的值,方法的参数变量的原来的变量是相对独立的存在,而在main方法中b = a属于直接赋值就不说了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
因为你这个fun方法是把数组a[]的引用拿去指向数组b[]了,也就是说有两个引用指向了b[],当然,原来的a[]没引用了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
更多回答(1)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询