集 合
一、 Collectiong
1、 list:有序集合,数据可重复;
(1)、ArryList:动态数组。
特点:数组的存储结构空间连续;
运行逻辑:每次操作时,都要改变数组的大小。(添加、移除操作时,会新建数组,然后将数组内容复制到新建数组中,增加了时间、空间的复杂度),但是读取、查询的效率高;
新增:add(“黎明”);
插入:add(2,“黎明”);
移除:remove(2);
例题: ArrayList<String> sp = new ArrayList<String>(); System.out.println("=========手机卖场========"); System.out.println("----------增加-----------"); sp.add("oppo手机"); sp.add("vivo手机"); sp.add("苹果手机"); sp.add("金立手机"); sp.add("魅族手机"); for (int i = 0; i < sp.size(); i++) { System.out.println(sp.get(i)); } System.out.println("----------插入------------"); sp.add(3, "小米手机"); for (int i = 0; i < sp.size(); i++) { System.out.println(sp.get(i)); } System.out.println("----------修改------------"); sp.remove(4); sp.add(4, "华为手机"); for (int i = 0; i < sp.size(); i++) { System.out.println(sp.get(i)); } System.out.println("----------删除------------"); sp.remove(3); for (int i = 0; i < sp.size(); i++) { System.out.println(sp.get(i)); }
(2)、LinkedList:链表结构;
对内存空间没有特殊要求;
运行逻辑:
新增:将尾元素连接到新增元素,新增元素作为新的尾元素;
插入:找到指定位置的元素新增元素,将指定元素前一个连接到新的元素上,而新的元素连接到指定元素;
移除:找到指定元素,将指定元素连接到下一个元素;
缺点:不适合数据的查找读取;
addFrist();增加头部;
addLast();增加尾部;
removeFirst();删除头部;
removeLast();删除尾部;
例题: LinkedList<String> sp = new LinkedList<String>(); System.out.println("=========动物园========"); System.out.println("----------增加-----------"); sp.add("熊猫"); sp.add("老虎"); sp.add("狮子"); sp.add("大象"); sp.add("狼"); for (int i = 0; i < sp.size(); i++) { System.out.println(sp.get(i)); } System.out.println("----------插入------------"); sp.add(3, "豹子"); for (int i = 0; i < sp.size(); i++) { System.out.println(sp.get(i)); } System.out.println("----------修改------------"); sp.remove(4); sp.add(4, "斑马"); for (int i = 0; i < sp.size(); i++) { System.out.println(sp.get(i)); } System.out.println("----------删除------------"); sp.remove(3); for (int i = 0; i < sp.size(); i++) { System.out.println(sp.get(i)); } System.out.println("----------增加头部与尾部---------"); sp.addFirst("泰迪"); sp.addLast("蟒蛇"); for (int i = 0; i < sp.size(); i++) { System.out.println(sp.get(i)); } System.out.println("----------删除头部与尾部-----------"); sp.removeFirst(); sp.removeLast(); for (int i = 0; i < sp.size(); i++) { System.out.println(sp.get(i)); }
2、 set:无序,数据不可重复;
添加重复数据覆盖原有的数据;
例题: HashSet<String> ha = new HashSet<String>(); System.out.println("===========4S旗舰店======="); System.out.println("===========添加==========="); ha.add("宝马"); ha.add("法拉利"); ha.add("奥迪"); ha.add("捷豹"); ha.add("宝马"); //Iterator是叠带器;读取数据 Iterator<String> it = ha.iterator(); while(it.hasNext()){ String s = it.next(); System.out.println(s); } System.out.println("===========删除==========="); ha.remove("宝马"); Iterator<String> te = ha.iterator(); while(te.hasNext()){ String s1 = te.next(); System.out.println(s1); }
二、 Map:k、v模式集合;
K—>key(键);具有唯一性;
V—>value(值);
类中:
例题: 类中: private String worknum; private String workname; private String phone; public Work(String worknum, String workname, String phone) { super(); this.worknum = worknum; this.workname = workname; this.phone = phone; } public String getWorknum() { return worknum; } public void setWorknum(String worknum) { this.worknum = worknum; } public String getWorkname() { return workname; } public void setWorkname(String workname) { this.workname = workname; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; }
程序入口:
Work person1 = new Work("13112", "黎明", "18354202161"); Work person2 = new Work("13113", "炜婷", "12394358945"); Work person3 = new Work("13114", "雅婧", "15847589494"); Work person4 = new Work("13115", "小明", "14855840044"); HashMap<String, Work> ha = new HashMap<String,Work>(); //增加数据;person1.getWorknum()是键,person1是值; ha.put(person1.getWorknum(), person1); ha.put(person2.getWorknum(), person2); ha.put(person3.getWorknum(), person3); ha.put(person4.getWorknum(), person4); //键盘输入; Scanner input = new Scanner(System.in); System.out.println("请输入一个员工工号:"); String st = input.next(); //get(键);获取数据 Work person = ha.get(st); System.out.println("员工是:"+person.getWorkname()); System.out.println("===================================="); //根据map集合中的keyset()遍历value(); Set<String> keyset = ha.keySet(); //显示数据,固定格式; Iterator<String> it1 = keyset.iterator(); while(it1.hasNext()){ String key = it1.next(); Work persons1 = ha.get(key); System.out.println("员工是:"+persons1.getWorkname()); } System.out.println("====================================="); //根据map集合中的value()遍历value(); Collection<Work> coll_work = ha.values(); Iterator<Work> it2 = coll_work.iterator(); while(it2.hasNext()){ Work persons2 = it2.next(); System.out.println("员工是:"+persons2.getWorknum()); }