11.集合
Posted wlxslsb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了11.集合相关的知识,希望对你有一定的参考价值。
一.集合分类
1.迭代器Itertor:遍历集合数据
2.映射Map:存放键值对的集合
二.Collection集合接口,其父接口是Iterable
1.作用:是可以动态的存放数据
2.特点:无序,可以重复
3.子接口:List,Set
4.List:是有序,不唯一。动态数组
5.Set:是无序,唯一。
三.List接口
1.实现类:ArrayList,LinkedList
2.ArrayList:动态数组。常用API有:
优点:做随机访问,和遍历访问有优势
add(Object o):添加元素
get(int index):获取元素
size():返回数组的有效长度
remove(int index):根据下标指定移除元素
contains(Object o):判断数组中是否包含该对象
3.LinkedList:链表动态数组
特点:双向链表结构,是每一个元素都会记录相邻的其他元素的位置信息。
有点:就是在做数据的增删改速度更快
4.Vector:动态数组。
特点:和ArrayList的特点一样的。但是Vector是线程安全,而ArrayList是线程非安全的
四.Set接口:
1.常用实现类:HashSet,TreeSet
2.HashSet常用API:
add(Object o):添加元素
size:返回有效长度
iterator:得到迭代器
remove(Object o):移除对象
3.迭代器的使用
hasNext:用来判断迭代器中是否还有下一条数据
next:取出下一条数据
4.TreeSet:
特点:相对于hashset有一个自然排序的过程,升序
五.Map接口:键值对的映射 Key=value
1.常用实现类:HashMap;TreeMap;
2.HashMap常用API:
put(Object key,Object value):Key=value的方式存入集合中
get(Object key):根据key来获取值。
size():有效长度
Set keySet():返回当前集合中的所有key
3.TreeMap:
特点:相对于hashmap而言,实现类key的自然升序排序
六.例子
1.创建学员对象,在集合中存放5个学员对象,求他们的平均年龄?
2.学员信息管理,用List重做
3.在set集合中插入5个数字,求最大值是谁?
/* * 用list集合,定义学生类,求5个学员的平均年龄 */ package com.class1110; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Class1 { public static void main(String[] args) { // TODO Auto-generated method stub List list=new ArrayList(); Scanner sc=new Scanner(System.in); for(int i=0;i<5;i++) { Student1 stu=new Student1(); System.out.println("请输入第"+(i+1)+"个学员的年龄"); stu.setAge(sc.nextInt()); list.add(stu); } System.out.println(list.size()); double sum=0; for(int i=0;i<5;i++) { Student1 s=(Student1)list.get(i); sum+=s.getAge(); } System.out.println(sum/5); } } class Student1{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
/* * 学生管理系统 */ package com.class1110; import java.util.List; import java.util.ArrayList; import java.util.Scanner; public class Class2 { public static void main(String[] args) { // TODO Auto-generated method stub //菜单各种询问,各种调用 Start st = new Start(); int s1; String s2; StuManager sm = new StuManager(); do { st.select(); Scanner sc = new Scanner(System.in); System.out.print("请选择:"); s1 = sc.nextInt(); switch (s1) { case 1: System.out.println("添加学员:"); sm.addStu(); System.out.println("添加成功!"); break; case 2: sm.queryStu(); break; case 3: sm.deleteStu(); break; case 4: sm.updateStu(); break; default: System.out.println("输入错误"); break; } System.out.print("是否继续?y/n:"); s2 = sc.next(); System.out.println(); } while (s2.equals("y")); } } class Start{ public Start() { for(int i=0;i<25;i++) { System.out.print("-"); } System.out.println(); System.out.println(" 欢迎使用学员管理"); for(int i=0;i<25;i++) { System.out.print("-"); } System.out.println(); } public void select() { System.out.println("功能选择:"); System.out.println(" 1.添加学员"); System.out.println(" 2.查看学员信息"); System.out.println(" 3.删除学员"); System.out.println(" 4.修改学员"); } } class Student{ // 各种属性。要求封装 private String name; private int age; private String sex; private String phone; private String place; public void setName(String name) { this.name=name; } public String getName() { return name; } public void setAge(int age) { this.age=age; } public int getAge() { return age; } public void setSex(String sex) { this.sex=sex; } public String getSex() { return sex; } public void setPhone(String phone) { this.phone=phone; } public String getPhone() { return phone; } public void setPlace(String place) { this.place=place; } public String getPlace() { return place; } } //学员管理类 class StuManager{ List list=new ArrayList(); Scanner sc1=new Scanner(System.in); public void addStu() { Student stu1=new Student(); list.add(stu1); System.out.print("姓名:"); stu1.setName(sc1.next()); System.out.print("年龄:"); stu1.setAge(sc1.nextInt()); System.out.print("性别:"); stu1.setSex(sc1.next()); System.out.print("电话:"); stu1.setPhone(sc1.next()); System.out.print("籍贯:"); stu1.setPlace(sc1.next()); } public void queryStu() { System.out.print("姓名"+" "); System.out.print("年龄"+" "); System.out.print("性别"+" "); System.out.print("电话"+" "); System.out.print("籍贯"+" "); for(int i=0;i<list.size();i++) { Student stu2=(Student)list.get(i); System.out.println(); System.out.print(stu2.getName() + " "); System.out.print(stu2.getAge() + " "); System.out.print(stu2.getSex() + " "); System.out.print(stu2.getPhone() + " "); System.out.print(stu2.getPlace() + " "); } } public void deleteStu() { System.out.print("请输入要删除的学员姓名:"); String name1=sc1.next(); boolean b1=false; for(int i=0;i<list.size();i++) { Student stu3=(Student)list.get(i); if (stu3.getName().equals(name1)) { b1 = true; list.remove(i); } } if(b1) { System.out.println("删除成功!"); }else{ System.out.println("未找到该学员!"); } } public void updateStu() { System.out.print("请输入要修改的学员姓名:"); String name2=sc1.next(); boolean b2=false; for(int i=0;i<list.size();i++) { Student stu4=(Student)list.get(i); if (stu4.getName().equals(name2)) { b2 = true; System.out.print("姓名:"); stu4.setName(sc1.next()); System.out.print("年龄:"); stu4.setAge(sc1.nextInt()); System.out.print("性别:"); stu4.setSex(sc1.next()); System.out.print("电话:"); stu4.setPhone(sc1.next()); System.out.print("籍贯:"); stu4.setPlace(sc1.next()); } } if(b2) { System.out.println("修改成功!"); }else{ System.out.println("未找到该学员!"); } } }
/* * 3.在set集合中插入5个数字,求最大值是谁? */ package com.class1110; import java.util.HashSet; import java.util.Set; import java.util.Iterator; public class Class3 { public static void main(String[] args) { // TODO Auto-generated method stub Set hs=new HashSet(); hs.add(13); hs.add(34); hs.add(43); hs.add(16); hs.add(5); Iterator it=hs.iterator(); int i=0; int j; while(it.hasNext()){ if((j=(int)it.next())>i){ i=j; } } System.out.println(i); } }
package com.demo1110; import java.util.ArrayList; import java.util.List; public class ListDemo { public static void main(String[] args) { Student stu1 = new Student("哈哈", 19); Student stu2 = new Student("呵呵", 23); Student stu3 = new Student("嘿嘿", 33); Student stu4 = new Student("霍霍", 44); Student stu5 = new Student("嘻嘻", 66); List list = new ArrayList(); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); double sum=0; for (int i = 0; i < list.size(); i++) { Student stu = (Student) list.get(i); sum += stu.age; } System.out.println(sum/list.size()); } } class Student{ String name; int age; public Student(String name,int age){ this.name = name; this.age = age; } }
package com.demo1110; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class MapDemo { public static void main(String[] args) { Map map = new HashMap(); map.put(1, "张三"); map.put(2, "梁柳"); map.put("3", "近平"); map.put("4", "近平"); // System.out.println(map.get("3")); Set keys = map.keySet(); for (Iterator iterator = keys.iterator(); iterator.hasNext();) { Object key = iterator.next(); System.out.println(map.get(key)); } } }
package com.demo1110; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; public class SetDemo { public static void main(String[] args) { Set set = new HashSet(); set.add("数据的"); set.add("放到付款公司会计法人"); Iterator it = set.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } }
package com.demo1110; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; public class Test { public static void main(String[] args) { Set set= new TreeSet(); set.add(100); set.add(10); set.add(122); set.add(54); set.add(66); for (Object obj : set) { System.out.println(obj); } } }
七.作业
一.创建hashmap对象,保存我国的省份简称和名字的键值对
要求:1.输入简称 打印输出对应的省份名
2.打印输出全部键值对的值
二.自己写一个ArrayList类,实现动态数组的存放和取出数据的功能
思路:
1.ArrayList:
属性:Object[] ,size, CAPACITY初始的容量
方法:add:1.判断当前存放数据是否要将原数组扩容,如果需要扩容将原数组长度扩展一半,并将原数组的数据复制到新数组中
get(i):根据下标获取对象
size:返回当前的有效长度
三.写一个双色球购买系统
1.彩票对象
属性:篮球1个,红球6个
2.系统对象
属性:彩票数组(集合,用来存放用户多次购买的结果),当期摇奖彩票
方法:
摇奖方法给当期彩票赋值
购买彩票方法,给彩票数组赋值
兑奖方法,显示中奖结果
/* * 一.创建hashmap对象,保存我国的省份简称和名字的键值对 要求:1.输入简称 打印输出对应的省份名 2.打印输出全部键值对的值 */ package com.class1110; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Test1 { public static void main(String[] args) { // TODO Auto-generated method stub Map mp=new HashMap(); Scanner sc=new Scanner(System.in); mp.put("京", "北京市"); mp.put("津", "天津市"); mp.put("冀", "河北省"); mp.put("晋", "山西省"); mp.put("内蒙古", "内蒙古自治区"); mp.put("辽", "辽宁省"); mp.put("吉", "吉林省"); mp.put("黑", "黑龙江省 "); mp.put("沪", "上海市"); mp.put("苏", "江苏省"); mp.put("浙", "浙江省"); mp.put("皖", "安徽省"); mp.put("闽", "福建省"); mp.put("赣", "江西省"); mp.put("鲁", "山东省"); mp.put("豫", "河南省"); mp.put("鄂", "湖北省"); mp.put("湘", "湖南省"); mp.put("粤", "广东省"); mp.put("桂", "广西壮族自治区"); mp.put("琼", "海南省"); mp.put("渝", "重庆市"); mp.put("蜀", "四川省"); mp.put("贵", "贵州省"); mp.put("云", "云南省"); mp.put("藏", "西藏自治区"); mp.put("陕", "陕西省"); mp.put("陇", "甘肃省"); mp.put("青", "青海省"); mp.put("宁", "宁夏回族自治区"); mp.put("新", "新疆维吾尔族自治区"); mp.put("香", "香港特别行政区"); mp.put("澳", "澳门特别行政区"); mp.put("台", "台湾省"); //打印输出全部键值对的值 Set keys=mp.keySet(); Iterator it=keys.iterator(); while(it.hasNext()){ System.out.println(mp.get(it.next())); } //输入简称 打印输出对应的省份名 System.out.println("请输入我国省份的简称"); System.out.println("对应的省份是:"+mp.get(sc.next())); } }
/* * 二.自己写一个ArrayList类,实现动态数组的存放和取出数据的功能 思路: 1.ArrayList: 属性:Object[] ,size, CAPACITY初始的容量 方法:add:1.判断当前存放数据是否要将原数组扩容,如果需要扩容将原数组长度扩展一半,并将原数组的数据复制到新数组中 get(i):根据下标获取对象 size:返回当前的有效长度 */ package com.class1110; public class Test2 { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList al=new ArrayList(); System.out.println("集合的长度为:"+al.size()); for(int i=0;i<17;i++){ al.add(i); } System.out.println("集合的长度为:"+al.size()); for(int i=0;i<al.size();i++){ System.out.println(al.get(i)); } //System.out.println(al.get(0)); } } class ArrayList{ private Object object[]=new Object[10]; private int size; private int capacity=10; public Object[] getObject() { return object; } public void setObject(Object[] object) { this.object = object; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public boolean add(Object object){ //判断数组前10位是否放满 if(this.object[this.object.length-1]==null) { //System.out.println(111); for(int i=0;i<this.object.length;i++){ if(this.object[i]==null){ this.object[i]=object; //System.out.println(this.object[i]); break; } } }else{ //System.out.println(222); capacity+=(capacity>>1); //创建新数组,长度为原数组长度的一倍半,并将原数组的值赋给新数组 Object[] object1=new Object[capacity]; for(int i=0;i<this.object.length;i++){ object1[i]=this.object[i]; } object1[this.object.length]=object; this.object=object1; } return true; } public Object get(int index){ Object o=null; if(size>index) { o=object[index]; }else{ throw new ArrayIndexOutOfBoundsException("index out of size"); } return o; } public int size(){ int i; for(i=0;i<object.length;i++) { if(object[i]==null){ break; } } return i; } }
/* * 三.写一个双色球购买系统 1.彩票对象 属性:篮球1个,红球6个 2.系统对象 属性:彩票数组(集合,用来存放用户多次购买的结果),当期摇奖彩票 方法: 摇奖方法给当期彩票赋值 购买彩票方法,给彩票数组赋值 兑奖方法,显示中奖结果 */ package com.class1110; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.Scanner; import java.util.Set; public class Test3 { public static void main(String[] args) { // TODO Auto-generated method stub LotterySystem ls=new LotterySystem(); //购买彩票 ls.buyLottery(); //打印出购买的号码 System.out.println("您本期购买的号码依次为:"); for(int i=0;i<ls.getLotterys().size();i++){ Lottery lts=(Lottery)ls.getLotterys().get(i); Set sets=lts.getRedball(); Iterator its=sets.iterator(); System.out.print("第"+(i+1)+"注:"); while(its.hasNext()){ System.out.print(its.next()+" "); } System.out.print(lts.getBlueball()); System.out.println(); } //摇奖 ls.drawLottery(); Set set=ls.getCurrentlottery().getRedball(); Iterator ite=set.iterator(); System.out.print("本期中奖号码为:"); while(ite.hasNext()){ System.out.print(ite.next()+" "); } System.out.print(ls.getCurrentlottery().getBlueball()); System.out.println(); //打印出中奖结果 String[] s=ls.queryLottery(); for(int i=0;i<s.length;i++){ System.out.println(s[i]); } } } class Lottery{ private int blueball; private Set redball=new HashSet(); public int getBlueball() { return blueball; } public void setBlueball(int blueball) { this.blueball = blueball; } public Set getRedball() { return redball; } public void setRedball(Set redball) { this.redball = redball; } } class LotterySystem{ private List lotterys=new ArrayList(); private Lottery currentlottery=new Lottery(); public List getLotterys() { return lotterys; } public void setLotterys(List lotterys) { this.lotterys = lotterys; } public Lottery getCurrentlottery() { return currentlottery; } public void setCurrentlottery(Lottery currentlottery) { this.currentlottery = currentlottery; } //摇出中奖号码 public void drawLottery(){ Random rd=new Random(); currentlottery.setBlueball(rd.nextInt(16)+1); Set s1=new HashSet(); while(s1.size()!=6){ s1.add(rd.nextInt(33)+1); } currentlottery.setRedball(s1); } //用户购买彩票 public void buyLottery(){ Scanner sc=new Scanner(System.in); String q; do{ Lottery lt=new Lottery(); while(true){ System.out.println("请输入1个蓝球号码:"); int b1=sc.nextInt(); if(b1>=1&&b1<=16){ lt.setBlueball(b1); break; }else{ System.out.println("您输入的蓝球号码有误,请重新输入!"); } } Set s2=new HashSet(); System.out.println("请输入6个红球号码:"); while(s2.size()!=6){ while(true){ int b2=sc.nextInt(); if(b2>=1&&b2<=33){ s2.add(b2); break; }else{ System.out.println("您输入的红球号码有误,请重新输入!"); } } } lt.setRedball(s2); lotterys.add(lt); System.out.println("请问您是否继续购买:y/n"); q=sc.next(); }while(q.equals("y")); } //查询用户是否中奖 public String[] queryLottery(){ String[] res=new String[lotterys.size()]; //用每一组购买的彩票和摇奖彩票相比较 for(int i=0;i<lotterys.size();i++) { int grade=0; Lottery lot=(Lottery)lotterys.get(i); //判断红球 Set s3 = lot.getRedball(); Iterator it3 = s3.iterator(); Set s4 = currentlottery.getRedball(); Iterator it4 = s4.iterator(); // 通过迭代器取出购买的某一张彩票中的每个红球和摇奖彩票的红球相比较 int count = 0; while (it3.hasNext()) { int rednum = (int) it3.next(); while (it4.hasNext()) { if (rednum == (int) it4.next()) { count++; } } } //判断蓝球 boolean b=false; if(lot.getBlueball()==currentlottery.getBlueball()){ b=true; } switch (count) { case 1: case 2: if(b){ grade = 6; } break; case 3: if(b){ grade = 5; } break; case 4: if(b){ grade = 4; }else{ grade = 5; } break; case 5: if(b){ grade = 3; }else{ grade = 4; } break; case 6: if(b){ grade = 1; }else{ grade = 2; } break; default: if(b){ grade = 6; } break; } switch(grade){ case 1: res[i]="恭喜您第"+(i+1)+"注彩票中了一等奖,奖金500万!"; break; case 2: res[i]="恭喜您第"+(i+1)+"注彩票中了二等奖,奖金30万!"; break; case 3: res[i]="恭喜您第"+(i+1)+"注彩票中了三等奖,奖金3000元!"; break; case 4: res[i]="恭喜您第"+(i+1)+"注彩票中了四等奖,奖金200元!"; break; case 5: res[i]="恭喜您第"+(i+1)+"注彩票中了五等奖,奖金10元!"; break; case 6: res[i]="恭喜您第"+(i+1)+"注彩票中了六等奖,奖金5元!"; break; default: res[i]="很遗憾,您没有中奖!"; break; } } return res; } }
/* * 三.写一个双色球购买系统 1.彩票对象 属性:篮球1个,红球6个 2.系统对象 属性:彩票数组(集合,用来存放用户多次购买的结果),当期摇奖彩票 方法: 摇奖方法给当期彩票赋值 购买彩票方法,给彩票数组赋值 兑奖方法,显示中奖结果 */ package com.class1110; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.Scanner; import java.util.Set; public class Test3 { public static void main(String[] args) { // TODO Auto-generated method stub LotterySystem ls=new LotterySystem(); //购买彩票 ls.buyLottery(); //打印出购买的号码 System.out.println("您本期购买的号码依次为:"); for(int i=0;i<ls.getLotterys().size();i++){ Lottery lts=(Lottery)ls.getLotterys().get(i); Set sets=lts.getRedball(); Iterator its=sets.iterator(); System.out.print("第"+(i+1)+"注:"); while(its.hasNext()){ System.out.print(its.next()+" "); } System.out.print(lts.getBlueball()); System.out.println(); } //摇奖 ls.drawLottery(); Set set=ls.getCurrentlottery().getRedball(); Iterator ite=set.iterator(); System.out.print("本期中奖号码为:"); while(ite.hasNext()){ System.out.print(ite.next()+" "); } System.out.print(ls.getCurrentlottery().getBlueball()); System.out.println(); //打印出中奖结果 String[] s=ls.queryLottery(); for(int i=0;i<s.length;i++){ System.out.println(s[i]); } } } class Lottery{ private int blueball; private Set redball=new HashSet(); public int getBlueball() { return blueball; } public void setBlueball(int blueball) { this.blueball = blueball; } public Set getRedball() { return redball; } public void setRedball(Set redball) { this.redball = redball; } } class LotterySystem{ private List lotterys=new ArrayList(); private Lottery currentlottery=new Lottery(); public List getLotterys() { return lotterys; } public void setLotterys(List lotterys) { this.lotterys = lotterys; } public Lottery getCurrentlottery() { return currentlottery; } public void setCurrentlottery(Lottery currentlottery) { this.currentlottery = currentlottery; } //摇出中奖号码 public void drawLottery(){ Random rd=new Random(); currentlottery.setBlueball(rd.nextInt(16)+1); Set s1=new HashSet(); while(s1.size()!=6){ s1.add(rd.nextInt(33)+1); } currentlottery.setRedball(s1); } //用户购买彩票 public void buyLottery(){ Scanner sc=new Scanner(System.in); String q; do{ Lottery lt=new Lottery(); while(true){ System.out.println("请输入1个蓝球号码:"); int b1=sc.nextInt(); if(b1>=1&&b1<=16){ lt.setBlueball(b1); break; }else{ System.out.println("您输入的蓝球号码有误,请重新输入!"); } } Set s2=new HashSet(); System.out.println("请输入6个红球号码:"); while(s2.size()!=6){ while(true){ int b2=sc.nextInt(); if(b2>=1&&b2<=33){ s2.add(b2); break; }else{ System.out.println("您输入的红球号码有误,请重新输入!"); } } } lt.setRedball(s2); lotterys.add(lt); System.out.println("请问您是否继续购买:y/n"); q=sc.next(); }while(q.equals("y")); } //查询用户是否中奖 public String[] queryLottery(){ String[] res=new String[lotterys.size()]; //用每一组购买的彩票和摇奖彩票相比较 for(int i=0;i<lotterys.size();i++) { int grade=0; Lottery lot=(Lottery)lotterys.get(i); //判断红球 Set s3 = lot.getRedball(); Iterator it3 = s3.iterator(); Set s4 = currentlottery.getRedball(); Iterator it4 = s4.iterator(); // 通过迭代器取出购买的某一张彩票中的每个红球和摇奖彩票的红球相比较 int count = 0; while (it3.hasNext()) { int rednum = (int) it3.next(); while (it4.hasNext()) { if (rednum == (int) it4.next()) { count++; } } } //判断蓝球 boolean b=false; if(lot.getBlueball()==currentlottery.getBlueball()){ b=true; } switch (count) { case 1: case 2: if(b){ grade = 6; } break; case 3: if(b){ grade = 5; } break; case 4: if(b){ grade = 4; }else{ grade = 5; } break; case 5: if(b){ grade = 3; }else{ grade = 4; } break; case 6: if(b){ grade = 1; }else{ grade = 2; } break; default: if(b){ grade = 6; } break; } switch(grade){ case 1: res[i]="恭喜您第"+(i+1)+"注彩票中了一等奖,奖金500万!"; break; case 2: res[i]="恭喜您第"+(i+1)+"注彩票中了二等奖,奖金30万!"; break; case 3: res[i]="恭喜您第"+(i+1)+"注彩票中了三等奖,奖金3000元!"; break; case 4: res[i]="恭喜您第"+(i+1)+"注彩票中了四等奖,奖金200元!"; break; case 5: res[i]="恭喜您第"+(i+1)+"注彩票中了五等奖,奖金10元!"; break; case 6: res[i]="恭喜您第"+(i+1)+"注彩票中了六等奖,奖金5元!"; break; default: res[i]="很遗憾,您没有中奖!"; break; } } return res; } }
以上是关于11.集合的主要内容,如果未能解决你的问题,请参考以下文章