ArrayList类
和使用链表实现的LinkedList类
,它们均可以存储由泛型参数E定义的元素,同时也都实现了List接口。add操做:不管是有序列表的add操做仍是无序列表的addToFtont和addAfter操做,都与remove操做相似,须要进行屡次比较和平移操做,因此其时间复杂度为O(n)。addToRear操做与栈的push操做相似,时间复杂度为O(1)。html
在队列中使用环形数组时,能够把dequeue的效率从O(n)变为O(1),由于它不须要平移数组,可是在列表中由于咱们能够在任何位置随意添加删除元素,因此平移数组的操做没法省略,所以是否使用环形数组就没有那么重要了。前端
instanceof
是什么?怎么用?public boolean equals(Object other) { boolean result = false; if (other instanceof Course) { Course otherCourse = (Course) other; if (prefix.equals(otherCourse.getPrefix()) && number == otherCourse.getNumber()) { result = true; } } return result; }
boolean b1 = new String() instanceof String; //b1为true boolean b2 = "Sting" instanceof Object; //b2为true,由于String是Object的子类 boolean b3 = new Object() instanceof String; //b3为false,Object是父类 boolean b4 = 'A' instanceof Character; //编译不经过,‘A’在此处视为基本数据类型char,instanceof操做符只能用做对象的判断 boolean b5 = null instanceof String; //b5为false,这是instanceof特有的规则:若左操做数为null,结果就直接返回false,再也不运算右操做数是什么类。 boolean b6 = (String)null instanceof String; //b6为false,即便类型转换仍是个 null boolean b7 = new Date() instanceof String; //编译不经过,instanceof操做符的左右操做数必须有继承或实现关系,不然编译出错
target
进行比较的元素上面,假如我设定的元素是temp
,它指的是用于遍历链表的指针如今所指的节点的下一个节点,当它指向的是所要找的目标元素时,实际要插入的位置(即current指向的节点)是目标元素的前一个,因此最后会致使,addAfter将所插元素插到指定位置以前而不是以后。将与target
进行比较的元素改成current
便可。NullPointerException
Deque类
,当时它的add和remove都是在一个类中写的,因此tail会发生改变。可是在列表中remove操做与add操做并不在一个类里,因此LinkedList类
中的tail从开始设定为null以后,若是直接引用的话理所固然要抛出异常。//原last方法 public T last() throws EmptyCollectionException { if (isEmpty()){ throw new EmptyCollectionException("LinkedList"); } return tail.getElement(); } //修改过的last方法 public T last() throws EmptyCollectionException { if (isEmpty()){ throw new EmptyCollectionException("LinkedList"); } LinearNode<T> temp = head; T result = temp.getElement(); while (temp.getNext() != tail){ temp = temp.getNext(); } tail = temp; return tail.getElement(); }
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 10/10 | 1/1 | 10/10 | |
第二周 | 246/366 | 2/3 | 20/30 | |
第三周 | 567/903 | 1/4 | 10/40 | |
第四周 | 2346/3249 | 2/6 | 20/60 |