/**
 * @(#)c1.java
 *
 *
 * @author 
 * @version 1.00 2009/2/28
 */public class c1 {
        
    
    /**
     * Creates a new instance of <code>c1</code>.
     */
    public c1() {
    }
    
    
   static void m1(int b[]){
     int c[]={7,8,9,10};
     b=c;
    

}   
static void p1(int t[]){
int i=0;
for(i=0;i<t.length;i++)
{
System.out.println(t[i]);
}
}
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int a[]={1,2,3,4};
        p1(a);
        System.out.println("----------");
        m1(a);
        p1(a);
        
    }
}
结果:
1,2,3,4
1,2,3,4/**
 * @(#)c1.java
 *
 *
 * @author 
 * @version 1.00 2009/2/28
 */public class c1 {
        
    
    /**
     * Creates a new instance of <code>c1</code>.
     */
    public c1() {
    }
    
    
   static void m1(int b[]){
     int c[]={7,8,9,10};
    // b=c;
    for(int i=0;i<b.length;i++){
     b[i]=c[i];    
    }
}   
static void p1(int t[]){
int i=0;
for(i=0;i<t.length;i++)
{
System.out.println(t[i]);
}
}
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int a[]={1,2,3,4};
        p1(a);
        System.out.println("----------");
        m1(a);
        p1(a);
        
    }
}结果:
1,2,3,4
7,8,9,10
----------------------------------------------请问红色代码中
a=c

b[i]=c[i];    
有什么区别的-----------------------------------------------------------------------

解决方案 »

  1.   

    就是考你JAVA中有几种参数传递方式
      

  2.   

    b=c是传对象的引用,即数组在内存中的地址使得连个数组的首地址指向相同的位置,b[i]=c[i]是指值传递。将c数组中的相应元素传到b数组中   
      

  3.   

    楼上说的没错,你要明白这个就好:
    java中数组也是引用类型,所以直接写b或者c都是它本身的引用,
      

  4.   

    首先说一下,java里所有的传值都是传递的副本。
    我举一个例子,把内存空间比作一个房间,而引用就相当于打开这个房间的钥匙。
    值传递是这么一回事,把此内存空间连大小和值,复制一份和原始内存中一模一样的,就像你新开了一个和原始房间一模一样的房间,但是你在新房间里做任何事都不会影响到原始房间。引用传递是这么一回事,他相当于复制一把钥匙,也就是引用的副本给某人,也就是多个引用指向同一个对象,这样其中一个引用改变了对象的状态会反应到其他引用身上。在你第一个例子中:static void m1(int b[]){ 
        int c[]={7,8,9,10}; 
        b=c; 
    }
    他只是复制一个引用b,但是此时你并没有通过b引用对对象进行任何操作,也就是你只是拿了房间钥匙但是没有打开房间,改变房间里的状态。此时,你把b引用指向了一个新的对象,相当于把之前那把钥匙丢掉,换了另一个房间的钥匙,引用虽然改变了,但是对象的状态并没有改变,所以也不会影响到其他的引用身上。第二个例子中:static void m1(int b[]){ 
        int c[]={7,8,9,10}; 
        // b=c; 
        for(int i=0;i <b.length;i++){ 
           b[i]=c[i];    
        } 
    } 这里你已经通过引用b对数组对象进行了操作,相当于你进入了房间,改变了房间的状态,那么当另一个引用或者说另一个人进入这个房间时。当然看到的就是改变后的房间的状态。
      

  5.   

    其实b=c应该是数组的引用,等于说是复制了一次;而b[i]=c[i]是数组里面数值的传递,只是把某个数值传递到对方相同的位置上
      

  6.   

    public class c1 { 
            
        
        /** 
        * Creates a new instance of <code>c1 </code>. 
        */ 
        public c1() { 
        } 
        
        
      static int[] m1(int b[]){ 
        int c[]={7,8,9,10}; 
        b=c; 
        return b;

        
    }  
    static void p1(int t[]){ 
    int i=0; 
    for(i=0;i <t.length;i++) 

    System.out.println(t[i]); 


        /** 
        * @param args the command line arguments 
        */ 
        public static void main(String[] args) { 
            // TODO code application logic here 
            int a[]={1,2,3,4}; 
            p1(a); 
            System.out.println("----------"); 
            int[] c=m1(a); 
            p1(c);

            
        } 

    结果
    1 2 3 4
    5 6 7 8
    知道为什么么
      

  7.   

    public class c1 { 
            
        
        /** 
        * Creates a new instance of <code>c1 </code>. 
        */ 
        public c1() { 
        } 
        
        
      static int[] m1(int b[]){ 
        int c[]={7,8,9,10}; 
        b=c; 
        return b; 
        
    }  
    static void p1(int t[]){ 
    int i=0; 
    for(i=0;i <t.length;i++) 

    System.out.println(t[i]); 


        /** 
        * @param args the command line arguments 
        */ 
        public static void main(String[] args) { 
            // TODO code application logic here 
            int a[]={1,2,3,4}; 
            p1(a); 
            System.out.println("----------"); 
            int[] c=m1(a); 
            p1(c); 
            
        } 
    } 对比
    public class c1 { 
            
        
        /** 
        * Creates a new instance of <code>c1 </code>. 
        */ 
        public c1() { 
        } 
        
        
      static void m1(int b[]){ 
        int c[]={7,8,9,10}; 
        b=c; 
        

    }  
    static void p1(int t[]){ 
    int i=0; 
    for(i=0;i <t.length;i++) 

    System.out.println(t[i]); 


        /** 
        * @param args the command line arguments 
        */ 
        public static void main(String[] args) { 
            // TODO code application logic here 
            int a[]={1,2,3,4}; 
            p1(a); 
            System.out.println("----------"); 
            m1(a); 
            p1(a); 
            
        } 
    } 结果为什么不同