As java method parameter, basic data type is passed by value, but object is passed by address.
这就是我说的变相考虑,自己封装一个类。 开始LZ的问题里多了一段话,LZ现在修改过来了,开始多了一段比如的代码 int a = 1; int b = 2; change1(a, b); System.out.println(a); 结果打印出 2如果题目本身没有上面这段 比如 提示,那么可以这么考虑,如果有比如这段,也就是说程序框架有了,只是让你写出change1(a, b),那么是不可能的。
http://topic.csdn.net/u/20090325/20/c1b1db0d-3175-4d4a-9ab5-794fdf1c5862.html
如果没有 比如 这段,那么就可以变相考虑,比如自己封装一个类,或者2个int本身是static,等等
private static Integer[] a={4,5};
private static int[] b={3,1};
public static void main(String[] args) {
Integer[] x=data();
int[] y=data1();
System.out.println(Arrays.toString(x));
System.out.println(Arrays.toString(y));
}
public static Integer[] data(){
int i=a[0]+a[1];
int j=a[0]-a[1];
a[1]=i-a[1];
a[0]=a[0]-j;
return a;
}
public static int[] data1(){
int i=b[0]+b[1];
int j=b[0]-b[1];
b[1]=i-b[1];
b[0]=b[0]-j;
return b;
}
}交换了但是不知这样算不算符合题目呢??
public static void main(String[] args) {
SwapTest st = new SwapTest();
st.a = 5;
st.b = 6;
st.c = 7;
st.d = 8;
changeInt(st);
changeInteger(st);
System.out.println("a = " + st.a);
System.out.println("b = " + st.b);
System.out.println("c = " + st.c);
System.out.println("d = " + st.d);
} private static void changeInt(SwapTest st) {
int temp = st.a;
st.a = st.b;
st.b = temp;
}
private static void changeInteger(SwapTest st) {
int temp = st.c;
st.c = st.d;
st.d = temp;
}
}class SwapTest {
public int a;
public int b;
public Integer c;
public Integer d;
}
Integer temp = st.c;
st.c = st.d;
st.d = temp;
}
这就是我说的变相考虑,自己封装一个类。
开始LZ的问题里多了一段话,LZ现在修改过来了,开始多了一段比如的代码
int a = 1;
int b = 2;
change1(a, b);
System.out.println(a);
结果打印出 2如果题目本身没有上面这段 比如 提示,那么可以这么考虑,如果有比如这段,也就是说程序框架有了,只是让你写出change1(a, b),那么是不可能的。