“商苑面馆:Java HashSet”的版本间的差异
来自CloudWiki
(创建页面,内容为“==HashMap== ===HashMap基本操作=== <nowiki>import java.util.*; →本类对应实训:: public class MainClass { public static void main(String[] arg…”) |
|||
第32行: | 第32行: | ||
} | } | ||
+ | }</nowiki> | ||
+ | |||
+ | ==面馆实训== | ||
+ | 源代码: | ||
+ | |||
+ | <nowiki>import java.util.*; | ||
+ | |||
+ | import entity.*; | ||
+ | |||
+ | public class FoodDao4 { | ||
+ | //private Food[] n;//食物数组 | ||
+ | private HashSet<Food> n;//食物数组 | ||
+ | |||
+ | public FoodDao4(HashSet<Food> n ){ | ||
+ | this.n = n; | ||
+ | |||
+ | } | ||
+ | //按名称搜 | ||
+ | public Food search(String s){ | ||
+ | for(Food f:n){ | ||
+ | if(f.toString().equals(s)) | ||
+ | return f; | ||
+ | } | ||
+ | return null; | ||
+ | //return new Noodle(); | ||
+ | } | ||
+ | |||
+ | public String toString(){ | ||
+ | String s=""; | ||
+ | for(Food f:n){ | ||
+ | s += f.toString()+"\r\n"; | ||
+ | } | ||
+ | return s; | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | // TODO Auto-generated method stub | ||
+ | |||
+ | |||
+ | //数组初始化 | ||
+ | HashSet<Food> al = new HashSet<Food>();//新建一个数组列表 | ||
+ | Food f1 = new Noodle("炸酱面",12.0,3,"不辣"); | ||
+ | Food f2 =new Noodle("臊子面",14.0,2,"麻辣"); | ||
+ | //Food f2 = new Rice("牛肉盖浇饭",3,2); | ||
+ | Food f3 = new Noodle("刀削面",10.0,5,"微辣"); | ||
+ | al.add(f1); | ||
+ | al.add(f2); | ||
+ | al.add(f3); | ||
+ | |||
+ | FoodDao4 f = new FoodDao4(al);//用数组列表f初始化食物仓库foodset | ||
+ | System.out.println(f.toString());//调用toString方法 | ||
+ | |||
+ | //接收用户输入 | ||
+ | Scanner scan = new Scanner(System.in); | ||
+ | System.out.println("您想点什么餐品?"); | ||
+ | String meal = scan.next(); | ||
+ | System.out.println("输入的数据为:" + meal); | ||
+ | |||
+ | //按名称搜索,调用search方法 | ||
+ | Food n01 = f.search(meal); | ||
+ | if(n01 != null) { | ||
+ | System.out.println("餐品详细信息为:"+n01.toString()); | ||
+ | System.out.println("请付钱!"); | ||
+ | }else { | ||
+ | System.out.println("对不起,我们店现在没有这种面"); | ||
+ | } | ||
+ | |||
+ | scan.close(); | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
}</nowiki> | }</nowiki> |
2019年4月22日 (一) 03:29的版本
HashMap
HashMap基本操作
import java.util.*; /*本类对应实训: */ public class MainClass { public static void main(String[] args) { HashSet<Integer> hashSet = new HashSet<>(); //增加元素 hashSet.add(5); //向集合中添加一个整数 hashSet.add(4); //向集合中添加一个 hashSet.add(3); //向集合中添加一个 hashSet.add(2); //向集合中添加一个 hashSet.add(1); //向集合中添加一个 //删除元素 hashSet.remove(1); //查找元素 if(hashSet.contains(4)){ System.out.println("4存在于集合之中"); }else{ System.out.println("4不存在于集合之中"); } //遍历集合 System.out.println("遍历集合:"); for(Integer i:hashSet) System.out.println(i); } }
面馆实训
源代码:
import java.util.*; import entity.*; public class FoodDao4 { //private Food[] n;//食物数组 private HashSet<Food> n;//食物数组 public FoodDao4(HashSet<Food> n ){ this.n = n; } //按名称搜 public Food search(String s){ for(Food f:n){ if(f.toString().equals(s)) return f; } return null; //return new Noodle(); } public String toString(){ String s=""; for(Food f:n){ s += f.toString()+"\r\n"; } return s; } public static void main(String[] args) { // TODO Auto-generated method stub //数组初始化 HashSet<Food> al = new HashSet<Food>();//新建一个数组列表 Food f1 = new Noodle("炸酱面",12.0,3,"不辣"); Food f2 =new Noodle("臊子面",14.0,2,"麻辣"); //Food f2 = new Rice("牛肉盖浇饭",3,2); Food f3 = new Noodle("刀削面",10.0,5,"微辣"); al.add(f1); al.add(f2); al.add(f3); FoodDao4 f = new FoodDao4(al);//用数组列表f初始化食物仓库foodset System.out.println(f.toString());//调用toString方法 //接收用户输入 Scanner scan = new Scanner(System.in); System.out.println("您想点什么餐品?"); String meal = scan.next(); System.out.println("输入的数据为:" + meal); //按名称搜索,调用search方法 Food n01 = f.search(meal); if(n01 != null) { System.out.println("餐品详细信息为:"+n01.toString()); System.out.println("请付钱!"); }else { System.out.println("对不起,我们店现在没有这种面"); } scan.close(); } }