容器
来源:互联网 发布:xp桌面修复软件 编辑:程序博客网 时间:2024/06/05 02:42
章节结构
1 容器的概念
2 容器API
3 Collection接口
4 Iterator接口
5 增强的for循环
6 Set接口
7 List接口和Comparable接口
8 Collections类
9 Map接口
10 自动打包/解包
11泛型
1136 一个图 一个类 3个知识点 6个接口
一 容器–盛放对象的程序
Set中数据对象无顺不可重复
List中数据对象有顺序可重复((即equal)重复指数值相等)
- equals仅能用于比较对象值是否相等
- ==比较基本数值的值 比较对象的内存位置 Collection接口
1 Collection接口
import java.util.*;public class Collect { public static void main(String[] args) { Collection c = new ArrayList();// 父类指向子类,仅能用父类已有方法,保证了程序的灵活性:不管换父类哪个子类都可运行下面程序 c.add("hello");//放入hello对象,即放入hello的引用 c.add(new Name("f1","l1"));//添加name类 c.add(new Integer(100));//添加一个integer对象(只能添加对象,不能添加基础数据类型(其分配在栈上,随时会被清空)) System.out.println(c.size());//对象数量 3 System.out.println(c);//[hello, f1 l1, 100] c.remove("hello");//找到与之equals的对象并去除 c.remove(new Integer(100));//自带equals方法已被重写 System.out.println(c.remove(new Name("f1","l1")));//未重写equals方法,比较的是同一对象 System.out.println(c); }}class Name {//自定义方法需重写equals方法则必须写hashCode方法 private String firstName, lastName; public Name(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String toString() { return firstName + " " + lastName; } public boolean equals(Object obj) {//在类当作键值和索引类时使用 if(obj instanceof Name) { Name name = (Name) obj; return (firstName.equals(n.firstName)) &&(lastName.equals(name.lastName)); } return super.equals(obj); } public int hashCode() { return firstName.hashCode(); }}
2 Iterator接口(相当于游标) 含多态(继承 重写 父类引用指向子类对象)
boolean hasNext();//判断游标右边是否有元素 Object next();//返回游标右边的元素并将游标移动到下一个位置 void remove();//删除游标左面的元素,在执行完next之后该操作只能执行一次
import java.util.*;public class Itera { public static void main(String[] args) { Collection c = new HashSet();// 父类指向子类,仅能用父类已有方法,保证了程序的灵活性:不管换父类哪个子类都可运行下面程序 c.add(new Name("fff1","l1")); c.add(new Name("f2","l2")); c.add(new Name("f3dsfsd","l3")); //Interator i = c.iterator(); for(Iterator i=c.iterator();i.hasNext();) { Name name = (Name)i.next(); if(name.getFirstName().length()<3) { i.remove();//调用c.remove(name)会出问题,因为在调用过程中已锁定 } }// while(i.hasNext()) {// Name n = (Name)i.next();// System.out.print(n.getFirstName()+" ");无序// } }}
增强for循环
import java.util.*;public class EnhancedFor {//增强for循环 public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5};//静态初始化 for(int i : arr) {//遍历打印(无法访问下标值,与Iterator相比无法方便删除集合中内容) System.out.println(i); } Collection c = new ArrayList(); c.add(new String("aaa")); c.add(new String("bbb")); c.add(new String("ccc")); for(Object o : c) { System.out.println(o);//aaabbbccc } }}
3 Set接口
import java.util.*;public class Seta { public static void main(String[] args) { Set s1 = new HashSet(); Set s2 = new HashSet(); s1.add("a"); s1.add("b"); s1.add("c"); s2.add("b"); s2.add("c"); s2.add("a"); Set sn = new HashSet(s1); sn.retainAll(s2);//交集 Set su = new HashSet(s1); su.addAll(s2);//并集 System.out.println(sn); System.out.println(su); }}
4 List接口(每个元素都有整数型序号与之对应)
选择数据结构
Array读快改慢
Linked改快读慢
Hash两者之间
import java.util.*;public class Link { public static void main(String[] args) { List l = new LinkedList();//底层为链表 for(int i=0; i<3; i++) { l.add("a"+i);//依次添加a0 a1等 } System.out.println(l); l.add(3,"as");//把第4位改成as,原有值后移 System.out.println(l); l.set(2,"as");//把第3位改为as,其余不动 System.out.println(l); System.out.println((String)l.get(2)+" ");//获取第3位值并转换为String型 System.out.println(l.indexOf("a3"));//获取值为a3的位置 l.remove(1);//移走第2位(第一位下标值为0) System.out.println(l); }}
一个类 collections
import java.util.*;public class Linkxu { public static void main(String[] args) { List l1 = new LinkedList(); List l2 = new LinkedList(); for(int i=0; i<9; i++) { l1.add("a"+i); } System.out.println(l1);//链条 Collections.shuffle(l1);//随机排列 System.out.println(l1); Collections.reverse(l1);//逆序 System.out.println(l1); Collections.sort(l1);//顺序 System.out.println(l1); System.out.println(Collections.binarySearch(l1,"a5"));//折半查找 }}
5 Comparable接口(自己类之间比较大小)
方法:public int compareTo(Object obj);
public int compareTo(Object o) { Name n = (Name)o; int lastCmp = lastName.compareTo(n.lastName); return (lastCmp != 0?lastCmp : firstName.compareTo(n.firstName));
返回0 this == obj
返回正数 this>obj
返回负数 this
import java.util.*;public class Mapp { public static void main(String[] args) { Map m1 = new HashMap(); Map m2 = new HashMap(); //m1.put("o",new Integer(1));//添加键值对 m1.put("one", 1);//被 *自动打包* 相当于m1.put("one",new Integer(1)); m1.put("tw",new Integer(2)); m1.put("th",new Integer(3));//抛出object型 m2.put("A",new Integer(1)); m2.put("B",new Integer(2)); System.out.println(m1.size());//3 System.out.println(m1.containsKey("o"));//是否包含键o System.out.println(m1.containsValue(new Integer(1)));//s (1) if(m1.containsKey("tw")) { int i = ((Integer)m1.get("tw")).intValue();//(Integer)m1.get("tw") //获取键为tw的值并强制转换,求其作为int型的值 //int i = (Integer)m1.get("tw");**自动解包**integer为相对应值 System.out.println(i); } Map m3 = new HashMap(m1); m3.putAll(m2);//把m2扔进m3 System.out.println(m3); }}
泛型(提高程序可读性与稳定性)类似enum
List<String> c = new ArrayList<String>();//只能装String型(可以为自定义类)
java规定可以用泛型的类可以用泛型()
class MyName implements Comparable<MyName> {//规定只可传入MyName型 int age; public int comepreTo(MyName mn) { if(this.age > mn.age)return 1; else if(this.age<mn.age) return -1; else return 0; }}
Map<String, Integer> m1 = new HashMap<String , Integer>(); //根据api确定每个类型后可跟几个泛型
阅读全文
0 0
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- 容器
- JDBC之增删改查
- HDFS IO操作总结
- spring
- JVM 三大性能调优参数-Xms -Xmx -Xss
- hdu 1117 Booklet Printing(模拟 分类 水题)
- 容器
- Coursera机器学习第一周学习笔记(二)——Gradient descent
- 记录gis踩过的坑(一)
- Tomcat学习总结(3)——Tomcat优化详细教程
- 建设一个学习Drupal的网站----------之学习笔记
- markdown语法
- 算法的时间复杂度和空间复杂度
- BZOJ 1036: [ZJOI2008]树的统计Count 树链剖分+线段树
- java 策略模式