diff --git a/w4/Test.java b/w4/test.java similarity index 100% rename from w4/Test.java rename to w4/test.java diff --git a/w7/Raw.java b/w7/Raw.java deleted file mode 100644 index 1de8e3b..0000000 --- a/w7/Raw.java +++ /dev/null @@ -1,15 +0,0 @@ -import java.io.*; - -public class Raw { - public static void main(String[] args){ - String filename="w7/Score.txt"; - int sum=0; - int count=0; - BufferedReader br=new BufferedReader(new FileReader(filename)); - String line; - while((line= br.readLine())!=null){ - sum+=Integer.parseInt(line); - count++; - } - } -} diff --git a/w8/Cache.java b/w8/Cache.java new file mode 100644 index 0000000..b16ab20 --- /dev/null +++ b/w8/Cache.java @@ -0,0 +1,50 @@ +import java.util.HashMap; +import java.util.Map; +public class Cache{ + private Map cache; + public Cache(){ + this.cache=new HashMap<>(); + } + //存储键值对到缓存 + public void put(K key,V value){ + cache.put(key,value); + } + //根据键获取值 + public V get(K key){ + return cache.get(key); + } + //检查某个缓存是否包含某个键 + public boolean containsKey(K key){ + return cache.containsKey(key); + } + //移除指定键的值 + public V remove(K key){ + return cache.remove(key); + } + //清空缓存 + public void clear(){ + cache.clear(); + } + //获取缓存大小 + public int size(){ + return cache.size(); + } + //检查缓存是否为空 + public boolean isEmpty(){ + return cache.isEmpty(); + } + @Override + public String toString(){ + return "Cache"+cache.toString(); + } + public static void main (String []args){ + Cache ageCache=new Cache<>(); + ageCache.put("张三",14); + ageCache.put("李四",13); + ageCache.put("王五",16); + System.out.println("年龄缓存:"+ageCache); + System.out.println("获取张三年龄"+ageCache.get("张三")); + System.out.println("缓存大小:"+ageCache.size()); + System.out.println("是否存在赵强的信息:"+ageCache.containsKey("赵强")); + } +} diff --git a/w8/Pair.java b/w8/Pair.java new file mode 100644 index 0000000..eb7bf90 --- /dev/null +++ b/w8/Pair.java @@ -0,0 +1,33 @@ +public class Pair { + private K key; + private V value; + public Pair(K key,V value){ + this.key=key; + this.value=value; + } + public K getKey(){ + return key; + } + public V getValue(){ + return value; + } + public void setKey(K key){ + this.key=key; + } + public void setValue(V value){ + this.value=value; + } + public Pair swap(){ + return new Pair<>(value,key); + } + @Override + public String toString(){ + return "("+key+","+value+")"; + } + public static void main (String []args){ + Pair pair=new Pair<>("小红",18); + System.out.println("交换前:"+pair); + Pair swapped=pair.swap(); + System.out.println("交换后:"+swapped); + } +}