第八课 JAVA中的数据结构中的集合.
第一部分:下面的内容是百度中查到的:在Java的APL中也有介绍。
在编程时,可以使用数组来保存多个对象,但数组长度不可变化,一旦在初始化数组时指定了数组长度,这个数组长度就是不可变的。如果需要保存数量变化的数据,数组就有点无能为力了。而且数组无法保存具有映射关系的数据,如成绩表为语文——79,数学——80,这种数据看上去像两个数组,但这两个数组的元素之间有一定的关联关系。
为了保存数量不确定的数据,以及保存具有映射关系的数据(也被称为关联数组),Java 提供了集合类。集合类主要负责保存、盛装其他数据,因此集合类也被称为容器类。Java 所有的集合类都位于 java.util 包下,提供了一个表示和操作对象集合的统一构架,包含大量集合接口,以及这些接口的实现类和操作它们的算法。
集合类和数组不一样,数组元素既可以是基本类型的值,也可以是对象(实际上保存的是对象的引用变量),而集合里只能保存对象(实际上只是保存对象的引用变量,但通常习惯上认为集合里保存的是对象)。
第二部分代码实战。
package com.JavaBasicsDemo4; import java.util.ArrayList; public class Muster { public static void main(String[] args) { ArrayList<Integer> list =new <Integer> ArrayList(); list.add(56); list.add(10); list.add(58); list.add(890); list.add(786); list.add(89); list.add(565); list.add(45); list.add(1000); list.add(6573); //list.add(" l love you "); list.add(90); list.add(78); list.remove(2); list.remove(new Integer(58)); //删除指定内容元素 list.remove(" l love you"); list.remove(1); System.out.println(list.size()); //修改 list.set(2, 1); //查询数据 Object obj =list.get(0); System.out.println(list.get(0)); list.get(2); //System.out.println(list.get(2)); System.out.println(list); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
package com.JavaBasicsDemo4; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Muster1 { public static void main(String[] args) { // 创建对象 Collection collection =new ArrayList(); collection.add("zhangshan1"); collection.add("zhangsi2"); collection.add("zhangshan3"); collection.add("hubin"); collection.add("i like you me to see you"); //遍历fanxing /* Iterator iterator =collection.iterator(); while (iterator.hasNext()){ String element =(String)iterator.next(); System.out.println(element); } */ //改写代码 //泛型Collection<String> collection =new ArrayList (); Iterator<String > iterator =collection.iterator(); while (iterator.hasNext()){ String element =(String)iterator.next(); System.out.println(element); } } }
package com.JavaBasicsDemo4; import java.util.HashSet; public class Muster3 { public static void main(String[] args) { HashSet<String> hashSet = new HashSet<String>(); hashSet.add("BB<br>"); hashSet.add("CCC"); hashSet.add("DDDD"); hashSet.add("EEEEE"); hashSet.add("FFFFFFF"); hashSet.add("GGGGGGGGG"); hashSet.add("hhhhhhhhhhhhhh"); System.out.println("hashSet集合的内容 :"+hashSet); } }
package com.JavaBasicsDemo4; import java.util.HashSet; public class Muster4 { public static void main(String[] args) { // TODO Auto-generated method stub HashSet<Person> hashSet = new HashSet<>(); //1 Person p1 =new Person("shangshanA",18); Person p2 =new Person("shangshanB",19); Person p3 =new Person("shangshanC",20); Person p4 =new Person("shangshanD",20); Person p5 =new Person("shangshanE",22); Person p6 =new Person("shangshanF",20); //2 Person p7 =new Person("shangshanA",18); Person p8=new Person("shangshanB",19); Person p9=new Person("shangshanC",20); Person p10 =new Person("shangshanD",20); Person p11 =new Person("shangshanE",22); Person p12 =new Person("shangshanF",20); // hashSet.add(p1); hashSet.add(p2); hashSet.add(p3); hashSet.add(p4); hashSet.add(p5); hashSet.add(p6); // hashSet.add(p7); hashSet.add(p8); hashSet.add(p9); hashSet.add(p10); hashSet.add(p11); hashSet.add(p12); System.out.println(hashSet); } }
package com.JavaBasicsDemo4; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Muster5 { public static void main(String[] args) { // lterator集合 //创建对象 Collection collection =new ArrayList(); // collection.add("胡1"); collection.add("胡琪琪"); collection.add("胡文平"); collection.add("胡丽萍"); collection.add("胡生路"); collection.add("胡一鸣"); collection.add("胡子在"); // if(collection.isEmpty()){ System.out.println("集合 Collection为空"); }else{ System.out.println("必定不为空,元素个数"); collection.size(); } //遍历这个集合 okIterator Iterator iterator =collection.iterator(); System.out.println("第一次遍历ok"); while(iterator.hasNext()){ //遍历循化 //获得集合元素 String element=(String)iterator.next(); System.out.println(element+" "); } // System.out.println("第二段遍历ok"); Iterator iterator2 =collection.iterator(); System.out.println("第二次遍历ok"); while(iterator2.hasNext()){ //遍历循化 //获得集合元素 String element=(String)iterator2.next(); System.out.println(element+" "); //从集合中删除上一次nec()方法返回值元素 iterator2.remove(); } System.out.println(); //判断集合是否为空 if(collection.isEmpty()){ System.out.println("集合collection为空值"); } //Collect<参数化类型>collection =new ArrayLIst< 参数化类型>(); Collection<String>collection2 =new ArrayList< String>(); collection.add("胡明明明"); collection.add("小红坏里面"); collection.add("小菲菲UI开"); //bianli Iterator<String> iterator3 =collection.iterator(); while(iterator3.hasNext()){ String element=(String)iterator3.next(); System.out.println(element+" "); } System.out.println(); // System.out.println("l like zi you me to see you"); } }
package com.JavaBasicsDemo4; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Muster6 { public static void main(String[] args) { Collection<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.add("e"); list.add("f"); Iterator<String> it = list.iterator(); while (it.hasNext()) { String str = (String) it.next(); System.out.println(str); for (int i = 0; i < list.size(); i++) { System.out.println(str); System.out.println(list); } } } }
package com.JavaBasicsDemo4; import java.util.ArrayList; public class Muster7 { public static void main(String[] args) { ArrayList<String> list =new ArrayList<String>(); list.add("a数组下标0"); //数组下标 list.add("b数组下标1"); list.add("c数组下标2"); list.add("d数组下标3"); list.add("e数组下标4"); list.add("f数组下标5"); int i=(int)(Math.random()*list.size()); System.out.println("随机抽取数组的元素:"+list.get(i)); for (int j = 0; j < list.size(); j++) { System.out.println(list.get(j)); } list.remove(2); System.out.println("将数组下标为1的元素从数组中移除,元素为"); for (int g = 0; g < list.size(); g++) { System.out.println(list.get(i)); } } }
package com.JavaBasicsDemo4; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Muster9 { public static void main(String[] args) { //创建ArrayList类集合 ArrayList<String> arrayList=new ArrayList<String>(); //size(); 集合个数 System.out.println("arrayList的初始化大小"+arrayList.size()); arrayList.add("AAAA"); //0 arrayList.add("BBBB"); //1 arrayList.add("CCCC"); //2 arrayList.add("DDDD"); //3 arrayList.add("BBBBB");// 4 arrayList.add("EEEEEE");// 5 arrayList.add("BBBB");// 6 System.out.println("last步添加元素后,arrayList的内容:"+arrayList); //插入 arrayList.add(1,"A2"); System.out.println("插入A2元素后。arrayList的内容:"+arrayList); //shangchu arrayList.remove(3); System.out.println("s删除定义D元su后,arrayList的内容"+arrayList); //indeof()cha System.out.println("arrayList第一次的元素:"+arrayList.lastIndexOf("BBBB")); //lastinderof() System.out.println("arrayList最后一次的元素:"+arrayList.lastIndexOf("BBBB")); //subList()取指定的集合 java.util.List<String> newlist=arrayList.subList(0, 5); System.out.println("取指定的集合list的集合;"+newlist); System.out.println("========================================="); // lterator集合 //创建对象 Collection collection =new ArrayList(); // collection.add("小明"); collection.add("小红"); collection.add("小开"); collection.add("小里"); collection.add("小陪"); collection.add("小了"); // if(collection.isEmpty()){ System.out.println("集合 Collection为空"); }else{ System.out.println("必定不为空,元素个数"); collection.size(); } //遍历这个集合 okIterator Iterator iterator =collection.iterator(); System.out.println("第一次遍历ok"); while(iterator.hasNext()){ //遍历循化 //获得集合元素 String element=(String)iterator.next(); System.out.println(element+" "); } // System.out.println("第二段遍历ok"); Iterator iterator2 =collection.iterator(); System.out.println("第二次遍历ok"); while(iterator2.hasNext()){ //遍历循化 //获得集合元素 String element=(String)iterator2.next(); System.out.println(element+" "); //从集合中删除上一次nec()方法返回值元素 iterator2.remove(); } System.out.println(); //判断集合是否为空 if(collection.isEmpty()){ System.out.println("集合collection为空值"); } //Collect<参数化类型>collection =new ArrayLIst< 参数化类型>(); Collection<String>collection2 =new ArrayList< String>(); collection.add("小明"); collection.add("小红"); collection.add("小开"); //bianli Iterator<String> iterator3 =collection.iterator(); while(iterator3.hasNext()){ String element=(String)iterator3.next(); System.out.println(element+" "); } System.out.println( ); // System.out.println(""); } }
package com.JavaBasicsDemo4; import java.util.ArrayList; public class Muster10 { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList list =new ArrayList(); // 增加元素 //创建学生对象 增加元素 Student stu1 =new Student("AAAAAA",27); Student stu2 =new Student("BBB",21); Student stu3=new Student("CCCC",10); Student stu4 =new Student("DDDDD",20); Student stu5 =new Student("AAAAAA",27); Student stu6 =new Student("BBB",21); Student stu7=new Student("CCCC",10); Student stu8 =new Student("DDDDD",20); Student stu9 =new Student("AAAAAA",27); Student stu10 =new Student("BBB",21); Student stu11=new Student("CCCC",10); Student stu12 =new Student("DDDDD",20); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); list.add(stu6); list.add(stu7); list.add(stu8); list.add(stu9); list.add(stu10); list.add(stu11); list.add(stu12); //shengchu boolean remove (int index) list.remove(0); list.remove(10); //获取集合yuansu object get(int indx) System.out.println(list.get(0)); System.out.println(list.get(3)); //修改元素指定的值 //public E set (int index ,E element )修改元素的值 list.set(2, new Student ("nam",30)); list.set(4, new Student ("namerteee",34)); //集合个数 int size() int count =list.size(); System.out.println("集合个数:"+count); //打印输出集合 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
package com.JavaBasicsDemo4; import java.util.ArrayList; import java.util.Iterator; public class Muster11 { public static void main(String[] args) { ArrayList<String> list =new ArrayList <String>(); list.add("AAAAAA"); list.add("BAAAAA"); list.add("AACAAA"); list.add("AAADAA"); list.add("AAAAEA"); list.add("AAAAAF"); //bianlidiyizhong for(){} //size() System.out.println("第一种111111111111111111111111forxunhuan:"); for(int i=0;i<list.size();i++){ //get() 读取数据 System.out.println(list.get(i)+""); } System.out.println( ); //bianlidierzhong System.out.println("dire22222222222222222222222zhongforeachxunhuanbianlijihe"); for(String s :list){ System.out.println(s+" "); } System.out.println(); //bianlidishanzhong System.out.println("dishan333333333333333333333zhong"); Iterator<String> it =list.iterator(); System.out.println("diyizhongfangfA"); while(it.hasNext()){ System.out.println(it.next()+" "); } } }
package com.JavaBasicsDemo4; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Muster12 { public static void main(String[] args) { //创建Map集合,实力化HashMap累类 HashMap<String,String> map =new HashMap<String,String>(); map.put("Tom","Javachengxusheji" ); map.put("John", "MYSQLshujukuan"); map.put("JSP", "JSPAaili"); map.put("Kou", "PS"); System.out.println("map集合的内容:"+map); System.out.println("mapjihedaxiao:"+map.size()); //给定key的值map的集合查着对应的value //如果没有反回null System.out.println("在定义中找到John教师的课程"+map.get("John")); System.out.println("在定义中找到sdo教师的课程"+map.get("sdo")); //增加重符的key:vvvv map.put("vvvv","MYSQLshujukuan"); System.out.println("增加重符的key后,map的内容"+map); System.out.println("增加重符的key后,map的大小"+map.size()); //判断是否包含指定到key System.out.println("map集合是否包含Tomjiaoshi:"+map.containsKey("Tom")); //判断是否包含指定的value System.out.println("map的集合中是否包含unix操作系统课程:" +map.containsValue("unix操作系统课程")); System.out.println("===================================="); System.out.println("===================================="); System.out.println("===================================="); System.out.println("===================================="); System.out.println("===================================="); //遍历 notone Set<String> keySet =map.keySet(); Iterator<String> it =keySet.iterator(); while(it.hasNext()){ String key =it.next(); String value =map.get(key); System.out.println(key+":"+value+" "); System.out.println(); } System.out.println("===================================="); System.out.println("===================================="); System.out.println("===================================="); System.out.println("===================================="); System.out.println("===================================="); //遍历第二种方式 Set<Map.Entry<String,String>> entrySet=map.entrySet(); Iterator<Map.Entry<String,String>> it2 =entrySet.iterator(); while(it2.hasNext()){ Map.Entry<String,String> entry =it2.next(); String key =entry.getKey(); String value =entry.getValue(); System.out.println(key+":"+value+" "); } System.out.println(); } }
package com.JavaBasicsDemo4; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.HashMap; import java.util.Scanner; public class Muster13 { public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub HashMap<String, String> map =new HashMap<String,String>(); map.put("360102", "AAAAAAA"); map.put("360321", "BBBBBBB"); map.put("360481", "CCCCCCC"); map.put("360429","自由的梦想"); map.put("350211", "DDDDDDD"); map.put("343987", "EEEEEEE"); map.put("360014", "FFFFFFF"); map.put("360897", "GGGGGGG"); map.put("360015", "HHHHHHH"); map.put("360456", "IIIIIII"); map.put("360128", "JJJJJJJ"); map.put("364258", "KKKKKKK"); //创建scanner Scanner sc=new Scanner(System.in); System.out.println("输入身份证号6位ok"); String idNum=sc.next(); if(map.containsKey(idNum)){ System.out.println("你的归属地:"+map.get(idNum)); }else{ System.out.println("没查到,请重输入ok"); } File file = new File("E:AAAAAAA/北京市.docx"); BufferedReader br =new BufferedReader(new FileReader(file)); String str =" "; HashMap<String, String> mapt =new HashMap<String,String>(); mapt.put("北京市", "110000"); map.put("河北省","130000"); mapt.put("天津市", "120000"); mapt.put("内蒙古自治区","150000"); mapt.put("辽宁省","210000"); mapt.put("吉林省 黑龙江省", "220000230000"); mapt.put("上海市江苏省", "310000320000"); mapt.put("浙江省 安徽省", "330000340000"); mapt.put("福建省江西省","360000350000"); mapt.put("山东省河南省", "370000410000"); mapt.put("湖北省湖南省", "420000430000"); mapt.put("广东省广西壮族自治区", "440000450000"); mapt.put("海南省重庆市", "460000500000"); mapt.put("四川省 贵州省", "510000520000"); mapt.put("云南省西藏自治区", "530000540000"); mapt.put("陕西省 甘肃省", "610000620000"); mapt.put("青海省 宁夏回族自治区","630000640000"); mapt.put("新疆维吾尔自治台湾省", "650000(886)|710000"); mapt.put("香港特别行政区"," (852)|810000"); mapt.put("澳门特别行政区","(853)|820000"); Scanner sct=new Scanner(System.in); System.out.println("输入你的归属身份地"); String f=sct.next(); if(map.containsKey(f)){ System.out.println("你的身份证last6位是?:"+mapt.get(f)); }else{ System.out.println("没查到,请重输入ok"); } } }