3.1 集合
(1) 集合是一种汇集,组织了其余对象的对象。它定义一张破那个特定的方式,能够访问、管理所包含的对象。
(2)集合的分类:1)按其元素是否按直线方式组织进行划分,分为线性集合(linear collection)和非线性集合(nonlinear collection);2)按保存类型划分,分为同构集合和异构集合。
(3)抽象数据类型:一个抽象数据类型(ADT)是由数据和在该数据上所实施的操做构成的集合,一个ADT有名称、值域和一组容许执行的操做。
(4)Java集合类API:Java标准类库中定义了几种不一样类型集合的类,称为Java集合类API。前端
3.2 栈集合
(1)栈(stack)是一种线性集合,其元素添加和删除都在同一端进行。
(2)栈的元素是按照后进先出(LIFO)(即 Last in ,first out)的方法进行处理的,最后进入栈中的元素最早被移出。
(3) 栈的一些操做:java
操做 | 描述 |
---|---|
push | 把项压入堆栈顶部 |
pop | 移除堆栈顶部的对象,并做为此函数的值返回该对象 |
peek | 查看堆栈顶部的对象,但不从堆栈中移除它 |
isempty | 测试堆栈是否为空 |
size | 肯定栈的元素数目 |
search | 返回对象在堆栈中的位置,以 1 为基数 |
3.3 主要的面向对象的概念
(1)继承:继承的过程是在两个类之间创建一种“是”的关系,即子类是一种更具体的父类版本。
(2)由继承实现多态性:当用类名声明一个引用变量时,这个变量能够指向该类的任何一个对象,同时,它也能引用经过继承与它所声明的类型有关的任何类的对象。
(3)泛型(generic type):一个集合所管理的对象的类型要在实例化该集合对象时才肯定。泛型保证了集合中对象类型的兼容性。android
3.4 异常
(1)异常(exception)就是一种对象,它定义了一种非正常或错误的状况。异常有程序或运行时环境抛出,能够按预期的被捕获或被正确处理。
(2)错误(error)与异常类似,不过异常表示一种没法恢复的状况,且没必要去捕获它。
(3)Java有一个预约义的异常和错误集,当程序运行时他们可能会发生。git
public interface Stack<T> { // Adds the specified element to the top of the stack. public void push (T element); // Removes and returns the top element from the stack. public T pop(); // Returns a reference to the top element of this stack without removing it. public T peek(); // Returns true if this stack contains no elements and false otherwise. public boolean isEmpty(); // Returns the number of elements in the stack. public int size(); // Returns a string representation of the stack. public String toString(); }
3.6 ArrayStack类数组
package jsjf; import jsjf.exceptions.*; import java.util.Arrays; public class ArrayStack<T> implements StackADT<T> { private final static int DEFAULT_CAPACITY = 100; private int top; private T[] stack;
public ArrayStack() { this(DEFAULT_CAPACITY); } public ArrayStack(int initialCapacity) { top = 0; stack = (T[])(new Object[initialCapacity]); }
public void push(T element) { if (size() == stack.length) expandCapacity(); stack[top] = element; top++; } private void expandCapacity() { stack = Arrays.copyOf(stack, stack.length * 2); } @Override public T pop() { if (isEmpty()) throw new EmptyCollectionException("stack"); top--; T result = stack[top]; stack[top] = null; return result; } @Override public T peek() { if (isEmpty()) throw new EmptyCollectionException("stack"); return stack[top-1]; } @Override public boolean isEmpty() { if (top==0) return true; else return false; } @Override public int size() { return top; }
1.链式结构是一种数据结构,它使用对象引用变量来建立对象之间的连接。安全
2.使用下面的这个类边能够建立一个链式结构,一个Person对象含有指向两个Person对象的连接,如此类推,这种类型的对象由称为自引用的。数据结构
public class Person { private String name; private String address; private Person next; }
3.全部动态建立的对象都来自于一个名为系统堆(system heap)或自由存储(free store)的内存区。并发
4.管理链表包括如下几个方面:
(1)访问元素
要访问其元素,咱们必须先访问第一个元素,而后顺着下一个指针从一个元素到另外一个元素。同时注意必需要维护指向链表的第一个指针。
(2)插入节点
首先,新添加节点的next引用被设置为指向链表的当前首结点,接着,指向链表前端的引用从新设置为指向这个新添加的节点。
(3)删除节点
要删除链表的首结点,须要重置指向链表前端的引用,使其指向链表当前的次结点。
app
5.无连接的元素
(1)定义一个单独的结点类,将全部元素连接在一块儿
(2)双向链表
6.LinkedList类
//-------------------------------------------------------------------- // Creates an empty stack using the default capacity. //-------------------------------------------------------------------- public LinkedStack() { count = 0; top = null; }
//删除并返回栈顶元素 public void push(T element) { LinearNode<T> eNode = new LinearNode<>(element); //新元素入栈对应新的对象 eNode.setNext(top); //新结点next引用指向栈顶 top = eNode; count++; } //返回当前栈顶所保存元素的引用 public T peek() throws EmptyStackException { return top.getElement(); } public boolean isEmpty() { return count == 0; } public int size() { return count; }
问题1:关于泛型的理解,泛型上学期涉及但未使用,此次看书上泛型代码就看不明白,“T”究竟是在指什么?
问题1解决方案:因而我把书上讲解泛型的部分从新看了一遍,又结合某公众号讲解总结了如下内容。
泛型的本质是为了参数化类型(在不建立新的类型的状况下,经过泛型指定的不一样类型来控制形参具体限制的类型)。也就是说在泛型使用过程当中,操做的数据类型被指定为一个参数,这种参数类型能够用在类、接口和方法中,分别被称为泛型类、泛型接口、泛型方法。
public class Box { private String object; public void set(String object) { this.object = object; } public String get() { return object; } }
这是最多见的作法,这样作的一个坏处是Box里面如今只能装入String类型的元素,若是咱们须要装入Int等其余类型的元素,还必需要另外重写一个Box,代码得不到复用,使用泛型能够很好的解决这个问题。
public class Box<T> { // T stands for "Type" private T t; public void set(T t) { this.t = t; } public T get() { return t; } }
这样咱们的Box类即可以获得复用,咱们能够将T替换成任何咱们想要的类型:
Box<Integer> integerBox = new Box<Integer>(); Box<Double> doubleBox = new Box<Double>(); Box<String> stringBox = new Box<String>();这样一来,T的含义就很明晰了,T它是能够代指咱们所须要的任何类型,用起来也会很方便。
问题2:java.util.Stack的实现有哪些优势,又存在哪些潜在的问题?
public class Stack<E> extends Vector<E> { /** * Creates an empty Stack. */ public Stack() { } /** * Pushes an item onto the top of this stack. This has exactly * the same effect as: * <blockquote><pre> * addElement(item)</pre></blockquote> * * @param item the item to be pushed onto this stack. * @return the <code>item</code> argument. * @see java.util.Vector#addElement */ public E push(E item) { addElement(item); return item; } /** * Removes the object at the top of this stack and returns that * object as the value of this function. * * @return The object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @exception EmptyStackException if this stack is empty. */ public synchronized E pop() { E obj; int len = size(); obj = peek(); removeElementAt(len - 1); return obj; } /** * Looks at the object at the top of this stack without removing it * from the stack. * * @return the object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @exception EmptyStackException if this stack is empty. */ public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return elementAt(len - 1); } /** * Tests if this stack is empty. * * @return <code>true</code> if and only if this stack contains * no items; <code>false</code> otherwise. */ public boolean empty() { return size() == 0; } /** * Returns the 1-based position where an object is on this stack. * If the object <tt>o</tt> occurs as an item in this stack, this * method returns the distance from the top of the stack of the * occurrence nearest the top of the stack; the topmost item on the * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt> * method is used to compare <tt>o</tt> to the * items in this stack. * * @param o the desired object. * @return the 1-based position from the top of the stack where * the object is located; the return value <code>-1</code> * indicates that the object is not on the stack. */ public synchronized int search(Object o) { int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; } /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = 1224463164541339165L; }
1.经过源码发现,Vector类在初始化的时候,会构造一个大小为10是空间
2.Stack中的pop、peek、search为线程安全类型
3.时间复杂度:
索引: O(n)
搜索: O(n)
插入: O(1)
移除: O(1)
经过peek()方法注释The object at the top of this stack (the last item of the Vector object,能够发现数组(Vector)的最后一位即为Stack的栈顶
pop、peek以及search方法自己进行了同步
push方法调用了父类的addElement方法
empty方法调用了父类的size方法
由此咱们能够了解到Stack类,它经过五个操做对类Vector 进行了扩展 ,容许将向量视为堆栈。它提供了一般的push 和 pop 操做,以及取堆栈顶点的peek 方法、测试堆栈是否为空的 empty 方法、在堆栈中查找项并肯定到堆栈顶距离的search 方法。因为Vector是经过数组实现的,这就意味着,Stack也是经过数组实现的。它可使用一个索引来跟踪栈中元素的位置。同时,因为它继承自Vector类,它继承了不少与栈的基本假设相互冲突的操做。
- 关于synchronized解释
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,可以保证在同一时刻最多只有一个线程执行该段代码。
1、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程获得执行。另外一个线程必须等待当前线程执行完这个代码块之后才能执行该代码块。
2、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另外一个线程仍然能够访问该object中的非synchronized(this)同步代码块。
3、尤为关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其余线程对object中全部其它synchronized(this)同步代码块的访问将被阻塞。
4、第三个例子一样适用其它同步代码块。也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就得到了这个object的对象锁。结果,其它线程对该object对象全部同步代码部分的访问都被暂时阻塞。
5、以上规则对其它对象锁一样适用.
问题1:关于Android Studio中文本显示换行问题,如图
问题1解决方案:参考上学期所作app,将layout文件夹下xml文件中text中代码作了这些修改,将android:singleLine="true"
中true改成false,同时添加一行代码android:inputType="textMultiLine"
,修改完以后程序运行如图:
问题2:关于书上intValue方法变红问题:
intValue方法,这是第一次接触这个方法。
intValue()
如Integer类型,就回有intValue()方法意识是说,把Integer类型转化为Int类型。其余相似,都是一个意思
valueOf()
如String就有valueOf()方法,意识是说,要把参数中给的值,转化为String类型,Integer的valueOf()就是把参数给的值,转化为Integer类型。其余相似,都是一个意思。
上学期结束时为7683行,如今为8255行,本周共572行
1.Software systems need only to work to support the work of developers, maintainers, and users.【×】
解析:软件系统不只仅只须要支持开发人员、维护人员和用户的工做。
2.Which Growth function has the highest order?
A .O(n log n)
B .O(n2)
C .O(2n)
D .O(log n)
解析:这道题目是问哪一个增加函数具备最高的阶次,咱们知道一个结论c(常量)<㏒₂n < n < n㏒₂n < n²< n³ < 2ⁿ < 3ⁿ< n!
,因此题目中的四个函数显然是2ⁿ的阶次是最高的,这里出错是由于当时把2ⁿ当作了2×n,误选了B。
博客中值得学习的或问题: 博客中代码问题解决过程记录较详细,可适当添加教材内容总结。
结对学习内容:第三章内容:集合概述——栈,第四章内容:链式结构——栈
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 0/0 | 1/1 | 15/15 | |
第二周 | 572/572 | 1/2 | 16/31 |