赞
踩
目录
- //注意,注意,注意,Idea 默认情况下,Debug 显示的数据是简化后的,如果希望看到完整的数据
- //需要做设置.
- //使用无参构造器创建ArrayList对象
- //ArrayList list = new ArrayList();
- ArrayList list = new ArrayList();
- //使用for给list集合添加 1-10数据
- for (int i = 1; i <= 10; i++) {
- list.add(i);
- }
- //使用for给list集合添加 11-15数据
- for (int i = 11; i <= 15; i++) {
- list.add(i);
- }
- list.add(100);
- list.add(200);
- list.add(null);
因为Vector类的操作方法带有synchronized,每一步都要保持线程同步,所以运行速度和ArrayList相比较慢
- //无参构造器
- //有参数的构造
- Vector vector = new Vector(8);
- for (int i = 0; i < 10; i++) {
- vector.add(i);
- }
- vector.add(100);
- System.out.println("vector=" + vector);
- //1. new Vector() 底层
- /*
- public Vector() {
- this(10);
- }
- 补充:如果是 Vector vector = new Vector(8);
- 走的方法:
- public Vector(int initialCapacity) {
- this(initialCapacity, 0);
- }
- 2. vector.add(i)
- 2.1 //下面这个方法就添加数据到vector集合
- public synchronized boolean add(E e) {
- modCount++;
- ensureCapacityHelper(elementCount + 1);
- elementData[elementCount++] = e;
- return true;
- }
- 2.2 //确定是否需要扩容 条件 : minCapacity - elementData.length>0
- private void ensureCapacityHelper(int minCapacity) {
- // overflow-conscious code
- if (minCapacity - elementData.length > 0)
- grow(minCapacity);
- }
- 2.3 //如果 需要的数组大小 不够用,就扩容 , 扩容的算法
- //newCapacity = oldCapacity + ((capacityIncrement > 0) ?
- // capacityIncrement : oldCapacity);
- //就是扩容两倍.
- private void grow(int minCapacity) {
- // overflow-conscious code
- int oldCapacity = elementData.length;
- int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
- capacityIncrement : oldCapacity);
- if (newCapacity - minCapacity < 0)
- newCapacity = minCapacity;
- if (newCapacity - MAX_ARRAY_SIZE > 0)
- newCapacity = hugeCapacity(minCapacity);
- elementData = Arrays.copyOf(elementData, newCapacity);
- }
- */
代码案例
- //定义一个Node 类,Node 对象 表示双向链表的一个结点
- class Node {
- public Object item; //真正存放数据
- public Node next; //指向后一个结点
- public Node pre; //指向前一个结点
- public Node(Object name) {
- this.item = name;
- }
- public String toString() {
- return "Node name=" + item;
- }
- }
-
-
- //模拟一个简单的双向链表
-
- Node jack = new Node("jack");
- Node tom = new Node("tom");
- Node hsp = new Node("老韩");
-
- //连接三个结点,形成双向链表
- //jack -> tom -> hsp
- jack.next = tom;
- tom.next = hsp;
- //hsp -> tom -> jack
- hsp.pre = tom;
- tom.pre = jack;
-
- Node first = jack;//让first引用指向jack,就是双向链表的头结点
- Node last = hsp; //让last引用指向hsp,就是双向链表的尾结点
-
-
- //演示,从头到尾进行遍历
- System.out.println("===从头到尾进行遍历===");
- while (true) {
- if(first == null) {
- break;
- }
- //输出first 信息
- System.out.println(first);
- first = first.next;
- }
-
- //演示,从尾到头的遍历
- System.out.println("====从尾到头的遍历====");
- while (true) {
- if(last == null) {
- break;
- }
- //输出last 信息
- System.out.println(last);
- last = last.pre;
- }
-
- //演示链表的添加对象/数据,是多么的方便
- //要求,是在 tom --------- 老韩直接,插入一个对象 smith
-
- //1. 先创建一个 Node 结点,name 就是 smith
- Node smith = new Node("smith");
- //下面就把 smith 加入到双向链表了
- smith.next = hsp;
- smith.pre = tom;
- hsp.pre = smith;
- tom.next = smith;
-
- //让first 再次指向jack
- first = jack;//让first引用指向jack,就是双向链表的头结点
-
- System.out.println("===从头到尾进行遍历===");
- while (true) {
- if(first == null) {
- break;
- }
- //输出first 信息
- System.out.println(first);
- first = first.next;
- }
-
- last = hsp; //让last 重新指向最后一个结点
- //演示,从尾到头的遍历
- System.out.println("====从尾到头的遍历====");
- while (true) {
- if(last == null) {
- break;
- }
- //输出last 信息
- System.out.println(last);
- last = last.pre;
- }
- LinkedList linkedList = new LinkedList();
- linkedList.add(1);
- linkedList.add(2);
- linkedList.add(3);
- System.out.println("linkedList=" + linkedList);
-
- //演示一个删除结点的
- linkedList.remove(); // 这里默认删除的是第一个结点
- //linkedList.remove(2);
-
- System.out.println("linkedList=" + linkedList);
-
- //修改某个结点对象
- linkedList.set(1, 999);
- System.out.println("linkedList=" + linkedList);
-
- //得到某个结点对象
- //get(1) 是得到双向链表的第二个对象
- Object o = linkedList.get(1);
- System.out.println(o);//999
-
- //因为LinkedList 是 实现了List接口, 遍历方式
- System.out.println("===LinkeList遍历迭代器====");
- Iterator iterator = linkedList.iterator();
- while (iterator.hasNext()) {
- Object next = iterator.next();
- System.out.println("next=" + next);
-
- }
-
- System.out.println("===LinkeList遍历增强for====");
- for (Object o1 : linkedList) {
- System.out.println("o1=" + o1);
- }
- System.out.println("===LinkeList遍历普通for====");
- for (int i = 0; i < linkedList.size(); i++) {
- System.out.println(linkedList.get(i));
- }
-
-
- //老韩源码阅读.
- /* 1. LinkedList linkedList = new LinkedList();
- public LinkedList() {}
- 2. 这时 linkeList 的属性 first = null last = null
- 3. 执行 添加
- public boolean add(E e) {
- linkLast(e);
- return true;
- }
- 4.将新的结点,加入到双向链表的最后
- void linkLast(E e) {
- final Node<E> l = last;
- final Node<E> newNode = new Node<>(l, e, null);
- last = newNode;
- if (l == null)
- first = newNode;
- else
- l.next = newNode;
- size++;
- modCount++;
- }
-
- */
-
- /*
- 老韩读源码 linkedList.remove(); // 这里默认删除的是第一个结点
- 1. 执行 removeFirst
- public E remove() {
- return removeFirst();
- }
- 2. 执行
- public E removeFirst() {
- final Node<E> f = first;
- if (f == null)
- throw new NoSuchElementException();
- return unlinkFirst(f);
- }
- 3. 执行 unlinkFirst, 将 f 指向的双向链表的第一个结点拿掉
- private E unlinkFirst(Node<E> f) {
- // assert f == first && f != null;
- final E element = f.item;
- final Node<E> next = f.next;
- f.item = null;
- f.next = null; // help GC
- first = next;
- if (next == null)
- last = null;
- else
- next.prev = null;
- size--;
- modCount++;
- return element;
- }
- */
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。