最简单的交换两个值得方法就是 用数组对象存储,然后交换数组对象中的元素位置 //ChangeTheNumber1 public class ChangeTheNumber1{ public static void main(String args[]){ int[] a=new int[5]; initize(a); int i=1,j=2; Swap(a,i,j); System.out.println(a[i]+","+a[j]);} public static void Swap(int[] x,int i,int j)//用数组可以实现交换2个元素的值 { int temp=x[i]; x[i]=x[j]; x[j]=temp; } public static void initize(int[] x){ for(int i=1;i<=5;i++) { x[i-1]=i; } }
}
看API,有: java.util.Collections.swap(List<?> list, int i, int j); 这个Collections是集合框架里的一个类,它把实现了List接口的数据中的i元素和j元素互换。 你所谓的是个类似的功能吧。以此来看,建议你实现自己的swap方法,它是泛型的,可以接受任何类型。 为了实现这个swap,需要准备一个容器来装载需要交换的两个数据, 因为JAVA得方法调用中,参数的传递是 --值传递-- 所以不能用类似C的办法来实现。 当然了,应用泛型有个好处,避免错误的类型转换。 代码如下(大部分来自《JAVA核心技术》,用《Effective Java》优化过):/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mytester;/** * * @author Rains.C */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Pair<Integer> p = new Pair(1, 2); System.out.println(p); System.out.println("After swap:"); System.out.println(p.swap()); } }class Pair<T> { private Pair() throws CreationException{throw new CreationException();} public Pair(T first, T second) {this.first = first;this.second = second;} public void setFirst(T newValue) {first = newValue;} public T getFirst() {return first;} public void setSecond(T newValue) {second = newValue;} public T getSecond() {return second;} public Pair swap() { T temp = second; second = first; first = temp; return this; } @Override public String toString() { return first + " | " + second; } private T first; private T second; private static class CreationException extends Exception { public CreationException() { super("Don't use this Creator"); } } }
y = y^x;
x = x^y;
a=b;
b=temp;over....
int y = 2;x = x+y;
y = x-y;
x = x-y;
int y = 2;x = x+y;
y = x-y;
x = x-y;
int y = 2;x = x+y;
y = x-y;
x = x-y;
int b=4;
a=b+(b=a)*0;
一 int x = 1; int y = 2;
x = x + y;
y = x - y; y变为x
x = x - y; x变为y二 int x = 1; int y = 2; int temp = 0;
temp = x;
x = y; x变为y
y = temp; y变为x
a=b;
b=temp;
先将两个数存进去,在pop出来就是对换之后的了
?LZ不会说的是这个吧
只是单纯的交换,而不是什么写个方法调用什么的还是可以的吧比如这个int a=1, b=2;
a^=b;b^=a;a^=b;但是下面这样是不可以int a=1, b=2;
swap(a,b);void swap(int a, int b) {
a^=b;b^=a;a^=b;
}
如果是 Java 里面的 Method,那没有,“办法”倒是有。上面的 + / - 的办法不好,可能超出精度范围,不如用 ^.^
//ChangeTheNumber1
public class ChangeTheNumber1{
public static void main(String args[]){
int[] a=new int[5];
initize(a);
int i=1,j=2;
Swap(a,i,j);
System.out.println(a[i]+","+a[j]);}
public static void Swap(int[] x,int i,int j)//用数组可以实现交换2个元素的值
{
int temp=x[i];
x[i]=x[j];
x[j]=temp;
}
public static void initize(int[] x){
for(int i=1;i<=5;i++)
{
x[i-1]=i;
}
}
}
java.util.Collections.swap(List<?> list, int i, int j);
这个Collections是集合框架里的一个类,它把实现了List接口的数据中的i元素和j元素互换。
你所谓的是个类似的功能吧。以此来看,建议你实现自己的swap方法,它是泛型的,可以接受任何类型。
为了实现这个swap,需要准备一个容器来装载需要交换的两个数据,
因为JAVA得方法调用中,参数的传递是 --值传递-- 所以不能用类似C的办法来实现。
当然了,应用泛型有个好处,避免错误的类型转换。
代码如下(大部分来自《JAVA核心技术》,用《Effective Java》优化过):/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mytester;/**
*
* @author Rains.C
*/
public class Main { /**
* @param args the command line arguments
*/
public static void main(String[] args) {
Pair<Integer> p = new Pair(1, 2);
System.out.println(p);
System.out.println("After swap:");
System.out.println(p.swap());
}
}class Pair<T> {
private Pair() throws CreationException{throw new CreationException();}
public Pair(T first, T second) {this.first = first;this.second = second;} public void setFirst(T newValue) {first = newValue;}
public T getFirst() {return first;} public void setSecond(T newValue) {second = newValue;}
public T getSecond() {return second;} public Pair swap() {
T temp = second;
second = first;
first = temp;
return this;
} @Override
public String toString() {
return first + " | " + second;
} private T first;
private T second; private static class CreationException extends Exception {
public CreationException() {
super("Don't use this Creator");
}
}
}