如何建网站免费,建设银行企业网站,微信h5页面制作小程序,网页设计作业视频网站目录
1.Collection集合接口
2.List 接口 (常用子类 ArrayList ,LinkedList,Vector)
3.Set 集合 接口(常用子类 HashSet LinkedHashSet,TreeSet)
4.集合输出(iterator , Enumeration) 1.Collection集合接口
Collection是集合中最大父接口#xff0c;在接口中定义了核心的…目录
1.Collection集合接口
2.List 接口 (常用子类 ArrayList ,LinkedList,Vector)
3.Set 集合 接口(常用子类 HashSet LinkedHashSet,TreeSet)
4.集合输出(iterator , Enumeration) 1.Collection集合接口
Collection是集合中最大父接口在接口中定义了核心的一些操作。
Collection接口定义的核心方法
方法名描述方法修饰符参数返回值类型add(E element)将指定元素添加到集合中publicelement: 要添加的元素booleanremove(Object o)从集合中移除指定元素publico: 要移除的元素booleanget(int index)获取集合中指定位置的元素publicindex: 元素的索引Eclear()清空集合中的所有元素public无无size()返回集合中的元素数量public无inttoArray()将集合转换为数组public无Object[]iterator()返回集合的迭代器public无IteratorEcontains(Object o)检查集合是否包含指定元素publico: 要检查的元素boolean 2.List 接口 (常用子类 ArrayList ,LinkedList,Vector)
List实现了Collection接口但是自己也扩充了一些方法
List扩充的方法
方法名描述get(int index)获取列表中指定位置的元素set(int index, E element)用指定元素替换列表中指定位置的元素ListIterator()返回列表的列表迭代器foreach(Consumer? super E action)对列表中的每个元素执行指定操作
1.ArrayList 线性表
作为一个线性标其存储方式是数组模式以下是一个ArrayList的案例代码
实现ArrayList存放字符串类型的火车
package Example1801;import java.util.ArrayList;
import java.util.List;public class javaDemo {public static void main(String[] args) {ListString list new ArrayListString();list.add(火车头);list.add(车厢1);list.add(车厢2);list.add(车厢3);
// 获取基础信息System.out.println(集合是否为空list.isEmpty() 集合内容的个数list.size());
// 调用方法判断是否存在字符串System.out.println(list.contains(火车头));
// 获取下标为0的内容System.out.println(下标为0的内容是list.get(0));
// 移除数据list.remove(车厢3);
// 非标准输出,了解即可标准输出接口是Iteratorlist.forEach(str-{System.out.println(str);});
// 清空集合内的数据list.clear();System.out.println(list.isEmpty());}
}2.LinkedList 链表
作为链表其实现的方法与火车类似定义一个空间车厢再接在火车最后一列车厢上。由于LinkedList都是遵循了
案例代码
package Example1804;import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;public class javaDemo {public static void main(String[] args) {// 创建链表并插入数据ListString linkedList new LinkedListString();linkedList.add(火车头);linkedList.add(火车车厢1);linkedList.add(火车车厢2);linkedList.add(火车车厢3);linkedList.add(火车车尾);System.out.println(linkedList);}}数组和链表的最大区别 1.链表相比于数组不需要频繁的创造新的空间但是两者的查找效率get并不相同数组的时间复杂度为O(1)而链表的时间复杂度是O(n) 3.Vector
Vector作为原始的程序类由于其应用的广泛性所以将其保存下来了
案例代码
package Example1805;import java.util.List;
import java.util.Vector;public class javaDemo {public static void main(String[] args) {
// 泛型包装类ListInteger all new VectorInteger();for (int i 1;i10;i){all.add(i);}if (!all.isEmpty()){System.out.println(all);}}
}ArrayList与Vector的区别 ArrayList与Vector首先两者虽然外部调用方法看起来一样但是二者的源码内部机制并不一样Vector的源码中操作方法中加入了synochronized同步的操作这就意味着多线程访问二者时候Vector就会比较安全但是相应的Vector的效率就会比较低 3.Set 集合 接口(常用子类 HashSet LinkedHashSet,TreeSet) Set与List最大的不同就是作为一个集合有互斥性即不能放同样的数据或者是同样的数据会被自动覆盖掉比如做一个统计统计某个学校学生最喜欢的科目100个学生但是不可能有100个科目所以如果有学生是喜欢同样的科目就会自动覆盖。
1.Set的子类HashSet(散列集合)
注意该集合的特点是无序性以下案例代码就可以看得出其中特性
案例代码:
package Example1806;import java.util.HashSet;
import java.util.Set;public class javaDemo {public static void main(String[] args) {SetString all new HashSetString();all.add(火车头);all.add(车厢1);
// 重复数据all.add(车尾);all.add(车尾);System.out.println(all);}
}如果想要让HashSet要按照我们顺序输入实现顺序的话就需要用到另一个兄弟类LinkedHashSet以保证其输入的有序性
2.LinkedHashSet
案例代码:
package Example1807;import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Set;public class javaDemo {public static void main(String[] args) {SetString all new LinkedHashSetString();all.add(火车头);for (int i 1;i10;i){all.add(火车车厢i);}all.add(火车车尾);if (!all.isEmpty()){System.out.println(all);}all.clear();}
}3.有序集合 TreeSet
有序集合可能会跟刚才的LinkedHashSet联想在一起实际上并不一样其中所谓的有序指的是元素的有序性比如集合中传入一个班级一次考试的成绩有很大可能并不是按照考试的成绩顺序输入那么想要实现集合内部自动排好成绩的高低。此时就需要用到TreeSet有序集合
案例代码1
package Example1808;import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;public class javaDemo {public static void main(String[] args) {System.out.println(欢迎使用成绩等级系统);SetInteger all new TreeSetInteger();Scanner scanner new Scanner(System.in);System.out.println(请输入需要添加的成绩(输入0时候退出系统));while (true){if (scanner.hasNextInt()){int temp scanner.nextInt();
// 当输入的成绩位0时候退出系统if (temp 0){break;}all.add(temp);System.out.println(加入成绩成功);}else {System.out.println(您输入的成绩有问题,请重新输入);}}System.out.println(all);}
}该代码实现了输出一个班同学所有人考试得到的成绩 然后从低到高输出大家取到过的成绩
4.重复元素消除
问题引出看如下代码
package Example1809;import java.util.HashSet;
import java.util.Set;class Member{private String name;private int age;private int id;Member(String name,int age,int id){this.age age;this.name name;this.id id;}Overridepublic String toString() {return Member{ name name \ , age age , id id };}
}
public class javaDemo {public static void main(String[] args) {SetMember all new HashSetMember();
// 重复对象all.add(new Member(赵五,20,001));all.add(new Member(赵五,20,001));System.out.println(all);}
}问按理来说add的两个对象其实都是同一个人如果定义集合Set就应该剔除重复对象为什么没有剔除呢 虽然添加了两个具有相同数据的Member对象但它们在内存中的地址不同因此HashSet无法识别它们是相同的对象也就无法将其视为重复元素进行删除。 要使HashSet正确识别重复的Member对象需要在Member类中重写hashCode()和equals()方法以便根据实际的业务逻辑来判断对象是否重复。例如可以根据name、age和id属性来判断两个Member对象是否相同。重写这两个方法后HashSet就能正确地剔除重复对象。 重复元素消除实现案例代码;
package Example1809;import java.util.HashSet;
import java.util.Objects;
import java.util.Set;class Member{private String name;private int age;private int id;Member(String name,int age,int id){this.age age;this.name name;this.id id;}Overridepublic boolean equals(Object o) {
// 如果存放的内存地址一样。那么自然就是一样的if (this o){return true;};
// 检查类型是否相同if (o instanceof Member){Member other (Member) o;
// 判断所有值是否相同return Objects.equals(this.name,other.name)this.age other.agethis.id other.id;}return false;}Overridepublic int hashCode() {
// 根据name,age,id计算哈希值return Objects.hash(name, age, id);}Overridepublic String toString() {return Member{ name name \ , age age , id id };}
}
public class javaDemo {public static void main(String[] args) {SetMember all new HashSetMember();
// 重复对象all.add(new Member(赵五,20,001));all.add(new Member(赵五,20,001));System.out.println(all);}
}可以看到同样的对象消失了这样就算实现了重复元素的消除 4.集合输出(迭代器Iterator , Enumeration )
1.迭代器iterator集合的标准输出
在前面的Collection中定义的方法
iterator()返回集合的迭代器public无IteratorE
这一段说明实现集合都可以通过迭代器iterator输出其创建的实例方法流程是
Iterator iterator 某个集合.iterator
Iterator接口的常用方法
方法名描述方法修饰符参数返回类型hasNext()检查迭代器是否还有下一个元素。如果有则返回true否则返回false。public无booleannext()返回迭代器的下一个元素并将迭代器的位置指向下一个元素。public无Eremove()从迭代器所在的集合中移除迭代器返回的最后一个元素可选操作。public无void
案例代码;
package Example1803;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;class Person{private String name;private int age;Person(String name,int age){this.age age;this.name name;}public int getAge() {return age;}public String getName() {return name;}
}
public class javaDemo {public static void main(String[] args) {ListPerson personList new ArrayListPerson();personList.add(new Person(黄小龙,87));personList.add(new Person(陈非凡,20));personList.add(new Person(陈俊,20));
// 直接输出会输出类的地址System.out.println(personList);
// 通过迭代器遍历输出对象的名称和ageIteratorPerson iterator personList.iterator();while (iterator.hasNext()){
// 需要有对象接收不能直接输出iterator.next.getName()iterator.next.getAge
// 这样会出现错误因为iterator接收数据后立刻就往下走Person person iterator.next();System.out.println(person.getName()的年龄是person.getAge());}}
}熟悉集合类型与Iterator的实现
package Example1810;import java.util.*;public class javaDemo {public static void main(String[] args) {
// 链表ListString list1 new LinkedListString();list1.add(和平号高铁车头);list1.add(和平号高铁车厢);list1.add(和平号高铁车尾);IteratorString iterator1 list1.iterator();while (iterator1.hasNext()){System.out.println(iterator1.next());}
// 散列集合SetString set1 new HashSetString();set1.add(绿皮火车车头);set1.add(绿皮火车车厢);set1.add(绿皮火车车尾);IteratorString iterator2 set1.iterator();while (iterator2.hasNext()){System.out.println(iterator2.next());}
// 有序集合SetInteger set2 new TreeSetInteger();set2.add(1);set2.add(2);set2.add(3);IteratorInteger iterator3 set2.iterator();while (iterator3.hasNext()){System.out.println(iterator3.next());}}
}2.Enumeration
Enumeration该接口与Vector都是原始接口都是在jdk很早前就定义了该接口主要是用于对Vector的数据输出在JDK1.5后对其进使用了泛型重新修改
该接口有两个比较常用的方法
方法名描述hasMoreElements()检查枚举对象是否有更多的元素可供迭代。如果有则返回true否则返回false。nextElement()返回枚举对象的下一个元素并将枚举对象的位置指向下一个元素。如果没有更多元素则会抛出NoSuchElementException异常
案例代码
package Example1811;import java.util.Enumeration;
import java.util.Vector;public class javaDemo {public static void main(String[] args) {VectorString vector new Vector();vector.add(测试1);vector.add(测试2);vector.add(测试3);
// 通过vector的elements实例化EnumerationString enumeration vector.elements();while (enumeration.hasMoreElements()){System.out.println(enumeration.nextElement());}}
} 文章转载自: http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.ktskc.cn.gov.cn.ktskc.cn http://www.morning.rsfp.cn.gov.cn.rsfp.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.gpryk.cn.gov.cn.gpryk.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.kqbzy.cn.gov.cn.kqbzy.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.gtqws.cn.gov.cn.gtqws.cn http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.webpapua.com.gov.cn.webpapua.com http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.mbpzw.cn.gov.cn.mbpzw.cn http://www.morning.dyxlm.cn.gov.cn.dyxlm.cn http://www.morning.ymjrg.cn.gov.cn.ymjrg.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.mslhq.cn.gov.cn.mslhq.cn http://www.morning.xrlwr.cn.gov.cn.xrlwr.cn http://www.morning.jpmcb.cn.gov.cn.jpmcb.cn http://www.morning.swkzk.cn.gov.cn.swkzk.cn http://www.morning.dkqyg.cn.gov.cn.dkqyg.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.zhoer.com.gov.cn.zhoer.com http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.iterlog.com.gov.cn.iterlog.com http://www.morning.qtwd.cn.gov.cn.qtwd.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.yrnyz.cn.gov.cn.yrnyz.cn http://www.morning.mkpqr.cn.gov.cn.mkpqr.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.cwcdr.cn.gov.cn.cwcdr.cn http://www.morning.krrjb.cn.gov.cn.krrjb.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.cjsrg.cn.gov.cn.cjsrg.cn http://www.morning.kjlia.com.gov.cn.kjlia.com http://www.morning.xq3nk42mvv.cn.gov.cn.xq3nk42mvv.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.fhykt.cn.gov.cn.fhykt.cn http://www.morning.nsyzm.cn.gov.cn.nsyzm.cn http://www.morning.kqkmx.cn.gov.cn.kqkmx.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.rjynd.cn.gov.cn.rjynd.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.hgtr.cn.gov.cn.hgtr.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.ypxyl.cn.gov.cn.ypxyl.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.dansj.com.gov.cn.dansj.com