1.3 Map接口中定义的通用方法 增:public V put(K key, V value) ;添加一个键值对Entry,返回null 删:public V remove(Object key);根据键Key去删键值对Entry,返回被删除的键值对的值 查:public V get(Object key);根据键Key获取对应的值Value 改:public V put(K key, V value) ;添加一个重复的键时,该方法变为修改,返回修改前的值 其他: public boolean containKey(Object K);判断Map中是否包含该键 public boolean containKey(Object V);判断Map中是否包含该值
1.4 LinkedList的数据结构以及使用 a.LinkedList的方法(7个collection+1个迭代器+4个List接口中的) 有特有方法: public void addFirst(E e);//添加元素到集合首 public void addLast(E e);//添加元素到集合尾 public E getFirst(E e);//获取集合的首元素 public E getLast(E e);//获取集合的尾元素 public E removeFirst(E e);//删除集合的首元素 public E removeLast(E e);//删除集合的尾元素
public void pop(E e);//删除集合中的首元素,底层就是removeFirst public void push(E e);//添加集合中的首元素,底层就是addFirst 源码:
voidlinkLast(E e) { final Node<E> l = last;//一个临时变量,存储最后一个节点 final Node<E> newNode = newNode<>(l, e, null);//创建一个Node对象 last = newNode;//将新Node对象存储到last if (l == null)//如果没有最后一个元素,说明当前是第一个节点 first = newNode;//将新节点存为第一个节点 else l.next = newNode;//否则不是第一个节点,就赋值到当前的last的next成员 size++;//总数量 + 1 modCount++;// }
•LinkedList的get()方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public E get(int index) { checkElementIndex(index);//检查索引的合法性(必须在0-size之间),如果不合法,此方法抛出异常 return node(index).item; }
Node<E> node(int index) {//此方法接收一个索引,返回一个Node // assert isElementIndex(index); if (index < (size >> 1)) {//判断要查找的index是否小于size / 2,二分法查找 Node<E> x = first;// x = 第一个节点——从前往后找 for (inti=0; i < index; i++)//从0开始,条件:i < index,此循环只控制次数 x = x.next;//每次 x = 当前节点.next; return x;//循环完毕,x就是index索引的节点。 } else { Node<E> x = last;// x = 最后一个节点——从后往前找 for (inti= size - 1; i > index; i--)//从最后位置开始,条件:i > index x = x.prev;//每次 x = 当前节点.prev; return x;//循环完毕,x就是index索引的节点 } }
//...... final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
Node<K, V>[] tab; //临时变量,存储"哈希表"——由此可见,哈希表是一个Node[]数组 Node<K,V> p;//临时变量,用于存储从"哈希表"中获取的Node int n, i;//n存储哈希表长度;i存储哈希表索引 if ((tab = table) == null || (n = tab.length) == 0)//判断当前是否还没有生成哈希表 n = (tab = resize()).length;//resize()方法用于生成一个哈希表,默认长度:16,赋给n if ((p = tab[i = (n - 1) & hash]) == null)//(n-1)&hash等效于hash % n,转换为数组索 tab[i] = newNode(hash, key, value, null);//此位置没有元素,直接存储 else{//否则此位置已经有元素了 Node<K,V> e; K k; if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k))))//判断哈希值和 e = p;//将哈希表中的元素存储为e elseif (p instanceof TreeNode)//判断是否为"树"结构 e = ((TreeNode<K, V>) p).putTreeVal(this, tab, hash, key, value); else {//排除以上两种情况,将其存为新的Node节点 for (intbinCount=0; ; ++binCount) {//遍历链表 if ((e = p.next) == null) {//找到最后一个节点 p.next = newNode(hash, key, value, null);//产生一个新节点,赋值到链 if (binCount >= TREEIFY_THRESHOLD - 1) //判断链表长度是否大于了8 treeifyBin(tab, hash);//树形化 break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))//跟当前 变量的元素比较,如果hashCode相同,equals也相同 break;//结束循环 p = e;//将p设为当前遍历的Node节点 } } if (e != null) { // 如果存在此键 } }
总结:
Collection { Iterator it = cc.iterator(); public boolean add(E e);添加元素,返回值表示是否添加成功。 public boolean remove(E e);删除元素,返回值表示是否删除成功。 无修改方法。 无查询方法。 public boolean contains(Object obj);判断集合中是否包含某个元素。 public void clear();清空集合(把集合中的元素全部删除,不是把集合置为Null) public boolean isEmpty();判断集合是否为空(指集合中没有元素,而非集合是否为Null) public int size();返回集合中元素的个数 public Object[] toArray();将集合转成数组 } List接口: 特点:有序,有索引,元素可重复 特有方法: add(int index,E e); remove(int index,E e); set(int index,E e); get(int index,E e);
ArrayList: 底层是数组结构 特有方法:无 特点:有序,有索引,元素可重复
LinkedList 底层是链表结构 特有方法: public void addFirst(E e);//添加元素到集合首 public void addLast(E e);//添加元素到集合尾 public E getFirst(E e);//获取集合的首元素 public E getLast(E e);//获取集合的尾元素 public E removeFirst(E e);//删除集合的首元素 public E removeLast(E e);//删除集合的尾元素 public void pop(E e);//删除集合中的首元素,底层就是removeFirst public void push(E e);//添加集合中的首元素,底层就是addFirst 特点:有序,有索引,元素可重复
1.集合的介绍&集合和数组的区别 a.什么是集合 集合就是Java用来保存数据的容器。 b.学过的容器 数组,ArrayList 数组定义: 数据类型[] 变量名 = new 数据类型[数组的长度] 集合定义: ArrayList<数据类型> 变量名 = new ArrayList<数据类型>(); c.数组和集合区别在哪里 I.数组的长度固定,集合的长度可变 II.数组中的元素类型可以是基本类型,也可以是引用类型。 集合中的元素类型必须只能是引用类型,如果想保存基本类型,要写该基本类型对应的写包装类。 例如: ArrayList arr = new ArrayList();
2.集合框架的继承体系
List接口特点: 有序,有索引,元素可以重复
Set接口特点: 无序,无索引,元素不可以重复
3.Collection中的通用方法 增:增加 public boolean add(E e);添加元素,返回值表示是否添加成功。 删:删除 public boolean remove(E e);删除元素,返回值表示是否删除成功。 改:修改 无修改方法。 查:查询 无查询方法。 其他方法: public boolean contains(Object obj);判断集合中是否包含某个元素。 public void clear();清空集合(把集合中的元素全部删除,不是把集合置为Null) public boolean isEmpty();判断集合是否为空(指集合中没有元素,而非集合是否为Null) public int size();返回集合中元素的个数 public Object[] toArray();将集合转成数组
比如: 在JDK1.5之前,创建集合:ArrayList arr = new ArrayList();集合中可以保存任意对象 在JDK1.5时,创建集合:ArrayList<具体的引用类型> arr = new new ArrayList<具体的引用类型>(); 这种参数类型可以用在类、方法和接口中,分别被称为泛型类,泛型方法,泛型接口
1 public BigInteger(String num);//创建一个大整数 BigInteger的成员方法 BigInteger不能直接使用+-*/进行计算,而是要通过方法进行计算
1 2 3 4
public BigInteger add(BigInteger value);//求和 public BigInteger subtract(BigInteger value);//求差 public BigInteger multiply(BigInteger value);//求积 public BigInteger divide(BigInteger value);//求商
3.2 DateFormat类的构造方法 DateFormat is an abstract class for date/time
DateFromat是抽象类,使用其子类(SimpleDateFormat) public SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols); 这里的pattern表示我们想要的时间字符串格式/模式
方法名 说明 public SimpleDateFormat(); 构造一个SimpleDateFormat,使用默认模式和日期格式 public SimpleDateFormat(String pattern); 构造一个SimpleDateFormat使用给定的模式和默认的日期格式 Letter Date or Time Component Presentation Examples G Era designator Text AD y Year Year 1996; 96 Y Week year Year 2009; 09 M Month in year (context sensitive) Month July; Jul; 07 L Month in year (standalone form) Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day name in week Text Tuesday; Tue u Day number of week (1 = Monday, …, 7 = Sunday) Number 1 a Am/pm marker Text PM H Hour in day (0-23) Number 0 k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 h Hour in am/pm (1-12) Number 12 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time zone Pacific Standard Time; PST; GMT-08:00 Z Time zone RFC 822 time zone -0800 X Time zone ISO 8601 time zone -08; -0800; -08:00 主要: y – 年 M – 月 d – 日 H – 时 m – 分 s – 秒
3.3 DateFormat类的成员方法
方法名 说明 public String format(Date date); 格式化方法,将日期格式化成日期/时间字符串 public Date parse(String date); 解析方法,从给定字符串的开始解析文本以及生成日期
使用Calendar的静态方法【推荐方式】: Calendar c = Calendar.getInstance();创建一个子类对象,返回
注意:在Calendar类中,月份(0-11),代表我们的(1-12)
4.2 Calendar类中常见的方法
方法名 说明 public int get(int field); 返回给定日历字段的值 public abstract void add(int field,int amount); 根据日历的规则,将指定的时间量添加或减去给定的日历字段 public final void set(int year,int month,int date); 设置当前的日历年月日
六. System 6.1 System类的介绍 The System class contains several useful class fields and methods. It cannot be instantiated. System类中包含几个静态到变量和静态到方法,且该类不能被实例化(无法被创建对象)
方法名 说明 public static void exit(int status); 终止当前运行的Java虚拟机,非0表示异常终止 public static long currentTimeMillis(); 返回当前时间(以毫秒为单位) System源码表示构造被私有化,故而无法创建对象 源码:
1 2 3
/** Don't let anyone instantiate this class */ privateSystem() { }
6.2 System类到常见用法
1 public static void exit(int status); 作用:退出Java虚拟机
1 public static long currentTimeMillis() 作用:获取当前系统毫秒值
1 2 3 4 5 6 7 8 9 10
longstart_new= System.currentTimeMillis();
StringBuilders1=newStringBuilder(); for (inti=0; i < 5000; i++) { s1.append(i); }
1 Parent P = new Sub(); 范例: 1 Animal A = new Dog();(假设Dog已经继承了Animal,并重写了某个方法) 1.4 多态调用方法的特点 a.多态调用方法时,编译阶段看父类 b.多态调用方法时,运行阶段看子类 总结:多态调用方法的特点是编译看父,运行看子
public protected (空的) private 同一类中 √ √ √ √ 同一类中(子类与无关类) √ √ √ X 不同包的子类 √ √ X X 不同包中的无关类 √ X X X 一般来说: ···成员方法和构造方法是public修饰符的 ···成员变量是private修饰的 在极个别的情况下,构造方法也可能是private(单例设计模式)
public class 实现类 implementsinterfaceName{ //实现类必须重写接口中的所有抽象方法 //实现类可以选择性重写默认方法但是@override后不能加default //静态方法通过类名/接口名直接调用的,没有@override说法 }
3.4 接口的多实现 格式:
1 2 3 4 5 6
public class 实现类 implementsinterface1,interface2...{ //实现类需要重写所有接口中的所有抽象方法 //如果有抽象方法是一样的,那么实现类只需要重写一次 //如果接口中有一样的默认方法,那么实现类必须重写一次! //静态方法没有重写的概念,就算多个接口中有一样的静态方法也不冲突,通过各自所在的接口名调用,没有歧义 }
方法名 说明 public String() 创建一个空白字符串对象,不含任何内容 public String(char[] chs) 根据字符组的内容,来创建字符串对象 public String(byte[] bys) 根据字节组的内容,来创建字符串对象 String s = “abc” 直接赋值的方式创建字符串对象,内容就是abc
方法名 说明 public StringBuilder() 创建一个空白可变字符串对象,不含任何内容 public StringBuilder(String str) 根据字符串内容,创建可变字符串对象 public StringBuilder(int Capacity) 构造一个没有字符的字符串构建器,以及由capcacity参数指定的初始容量 public StringBuilder(CharSequence seq) 构造一个字符串构建器,其中包含与指定的CharSequence相同的字符 3.3 StringBuilder的添加和反转方法
方法名 说明 public StringBuilder append(任意类型) 添加数据,并返回数据本身 public StringBuilder reserve() 返回相反的字符序列
方法名 说明 public StringBuilder append(任意类型) 添加数据,并返回数据本身 public StringBuilder reserve() 返回相反的字符序列 public String toString() 通过toString()就可以实现把StringBuilder转换为String