62,623
社区成员
发帖
与我相关
我的任务
分享java.util.Collections.swap(List<?> list, int i, int j);/*
* 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");
}
}
}//ChangeTheNumber1
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;
}