diff --git a/W8/AI 协助记录.docx b/W8/AI 协助记录.docx new file mode 100644 index 0000000..7096a6a Binary files /dev/null and b/W8/AI 协助记录.docx differ diff --git a/W8/Pair.java b/W8/Pair.java new file mode 100644 index 0000000..7242422 --- /dev/null +++ b/W8/Pair.java @@ -0,0 +1,48 @@ +package com.example.W8; + +// 文件名:Pair.java + public class Pair { + 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 void swap(Pair p1, Pair 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 + "}"; + } +} \ No newline at end of file diff --git a/W8/PairTest.java b/W8/PairTest.java new file mode 100644 index 0000000..51adac8 --- /dev/null +++ b/W8/PairTest.java @@ -0,0 +1,21 @@ +package com.example.W8; + +// 导入Pair类 +import com.example.W8.Pair; + +public class PairTest { + public static void main(String[] args) { + Pair p1 = new Pair<>("小明", 18); + Pair p2 = new Pair<>("小红", 19); + + System.out.println("交换前:"); + System.out.println("p1 = " + p1); + System.out.println("p2 = " + p2); + + Pair.swap(p1, p2); + + System.out.println("\n交换后:"); + System.out.println("p1 = " + p1); + System.out.println("p2 = " + p2); + } +} diff --git a/W8/屏幕截图 2026-05-11 144303.png b/W8/屏幕截图 2026-05-11 144303.png new file mode 100644 index 0000000..1981d78 Binary files /dev/null and b/W8/屏幕截图 2026-05-11 144303.png differ