You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
959 B
48 lines
959 B
package com.example.W8;
|
|
|
|
// 文件名:Pair.java
|
|
public class Pair<K, V> {
|
|
private K key;
|
|
private V value;
|
|
|
|
// 构造方法
|
|
public Pair(K key, V value) {
|
|
this.key = key;
|
|
this.value = value;
|
|
}
|
|
|
|
// getter & setter
|
|
public K getKey() {
|
|
return key;
|
|
}
|
|
|
|
public void setKey(K key) {
|
|
this.key = key;
|
|
}
|
|
|
|
public V getValue() {
|
|
return value;
|
|
}
|
|
|
|
public void setValue(V value) {
|
|
this.value = value;
|
|
}
|
|
|
|
// 交换两个Pair
|
|
public static <K, V> void swap(Pair<K, V> p1, Pair<K, V> p2) {
|
|
K tempKey = p1.key;
|
|
V tempValue = p1.value;
|
|
|
|
p1.setKey(p2.getKey());
|
|
p1.setValue(p2.getValue());
|
|
|
|
p2.setKey(tempKey);
|
|
p2.setValue(tempValue);
|
|
}
|
|
|
|
// 打印用
|
|
@Override
|
|
public String toString() {
|
|
return "Pair{key=" + key + ", value=" + value + "}";
|
|
}
|
|
}
|