A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 从未有过的晴天 中级黑马   /  2014-6-20 20:56  /  1161 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 从未有过的晴天 于 2014-6-21 21:34 编辑

class ArgsDemo {
                public static void main(String[] args) {
                        int a = 10;
                        int b = 20;
                        System.out.println(a+"---"+b);
                        change(a,b);
                        System.out.println(a+"---"+b);

                        int[] arr = {1,2,3,4,5};
                        change(arr);
                        System.out.println(arr[1]);
                }

                public static void change(int a,int b) {
                        System.out.println(a+"---"+b);
                        a = b;
                        b = a + b;
                        System.out.println(a+"---"+b);
                }

                public static void change(int[] arr) {
                        for(int x=0; x<arr.length; x++) {
                                if(arr[x]%2==0) {
                                        arr[x]*=2;
                                }
                        }
                }
        }

9 个回复

倒序浏览
你想表达什么呢?????????????????、
回复 使用道具 举报
alive 来自手机 中级黑马 2014-6-20 21:52:16
藤椅
不要太水了大家都在水
回复 使用道具 举报
要不要这样啊.....:o
回复 使用道具 举报
结果知道,想知道为什么
回复 使用道具 举报
你发表是为了解决什么问题?
回复 使用道具 举报
不知道  你想问什么,所以把结果的原因都弄出来了,不懂的再问好了
  1. import java.util.*;

  2. class ArgsDemo {
  3.     public static void main(String[] args) {
  4.             int a = 10;
  5.             int b = 20;
  6.             System.out.println(a+"---"+b);//10---20
  7.             change(a,b);
  8.             System.out.println(a+"---"+b);

  9.             int[] arr = {1,2,3,4,5};
  10.             change(arr);
  11.             System.out.println(arr[1]);                //arr[1]=4
  12.     }

  13.     public static void change(int a,int b) {
  14.             System.out.println(a+"---"+b);        //10---20
  15.             a = b;                                                        //b覆盖a ,a=20 b=20
  16.             b = a + b;                                                //b=40
  17.             System.out.println(a+"---"+b);        //20---40
  18.     }

  19.     public static void change(int[] arr) {
  20.             for(int x=0; x<arr.length; x++) {
  21.                     if(arr[x]%2==0) {
  22.                             arr[x]*=2;                //arr[1] = arr[1] * 2  ,得到4
  23.                     }
  24.             }
  25.     }
  26. }
复制代码
回复 使用道具 举报
你是在问为什么一个函数可以交换,另一个可以不改变吗?
回复 使用道具 举报
是啊,为什么前面没改变,后面却改变了
回复 使用道具 举报
change是静态方法,作用于局部,a和b不是全局变量,没被改变
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马