赞
踩
集合主要是两组:单列集合、双列集合
Collection接口有两个重要的子接口List、Set,他们的实现子类都是单列集合
Map接口的实现子类是双列集合,存放K-V
public interface Collection<E> extends Iterable<E>
1)Collection实现子类可以存放多个元素,每个元素可以是Object
2)有些Collection的实现类,可以存放重复的元素,有些不可以
3)有些Collection的实现类,可以是有序的(List), 有些也是无序的(Set)
4)Collection接口没有直接的实现子类,是通过他的子接口Set和List来实现的
- public class T {
- public static void main(String[] args) {
- List list = new ArrayList();
- //add添加元素
- list.add("jack");
- list.add(10);
- list.add(true);
- System.out.println("list = " + list);
- //remove 删除指定元素
- //list.remove(0); //删除第一个元素
- list.remove(true);
- //contains查找元素是否存在
- System.out.println(list.contains("jack"));
- //size:获取元素个数
- System.out.println(list.size());
- //isEmpty判断是否为空
- System.out.println(list.isEmpty());
- //clear 清空
- list.clear();
- System.out.println("list = " + list);
-
- //addAll添加多个元素
- ArrayList list2 = new ArrayList();
- list2.add("hlm");
- list2.add("sgyy");
- list.addAll(list2);
- System.out.println("list = " + list);
- //containsAll:判断多个元素是否存在
- System.out.println(list.containsAll(list2));
- //removeAll 删除多个元素
- list.add("lz");
- list.removeAll(list2);
- System.out.println("list = " + list);
- }
-
- }
-
- public class CollectionIterator {
- @SuppressWarnings({"all"})
- public static void main(String[] args) {
- Collection col = new ArrayList();
- col.add(new Book("三国演义", "罗贯中", 10.1));
- col.add(new Book("小李飞刀", "古龙", 5.1));
- col.add(new Book("三红楼梦", "曹雪芹", 34.6));
- // System.out.println("col : " + col);
- // 遍历集合:
- //1、 先得到col对应的迭代器
- Iterator iterator = col.iterator();
- //2、使用while循环遍历
- while(iterator.hasNext()) {
- //返回下一个元素, 类型是Object
- Object obj = iterator.next();
- System.out.println("obj=" + obj);
- }
- //快捷键,快速生成while =》 itit
-
- }
- }
-
- class Book {
- private String name;
- private String author;
- private double price;
-
- public Book(String name, String author, double price) {
- this.name = name;
- this.author = author;
- this.price = price;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getAuthor() {
- return author;
- }
-
- public void setAuthor(String author) {
- this.author = author;
- }
-
- public double getPrice() {
- return price;
- }
-
- public void setPrice(double price) {
- this.price = price;
- }
-
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", price=" + price +
- '}';
- }
- }
基本语法:
- for(元素类型 元素名: 集合名/数组名){
- 访问元素;
- }
- public class ListMethod {
- @SuppressWarnings({"all"})
- public static void main(String[] args) {
- List list = new ArrayList();
- list.add("张三丰");
- list.add("贾宝玉");
- // void add(int index, Object ele):在index位置插入ele元素
- //在index = 1的位置插入一个对象
- list.add(1, "韩顺平");
- System.out.println("list=" + list);
- // boolean addAll(int index, Collection eles):从index位置开始将eles中的所有元素添加进来
- List list2 = new ArrayList();
- list2.add("jack");
- list2.add("tom");
- list.addAll(1, list2);
- System.out.println("list=" + list);
- // Object get(int index):获取指定index位置的元素
- //说过
- // int indexOf(Object obj):返回obj在集合中首次出现的位置
- System.out.println(list.indexOf("tom"));//2
- // int lastIndexOf(Object obj):返回obj在当前集合中末次出现的位置
- list.add("韩顺平");
- System.out.println("list=" + list);
- System.out.println(list.lastIndexOf("韩顺平"));
- // Object remove(int index):移除指定index位置的元素,并返回此元素
- list.remove(0);
- System.out.println("list=" + list);
- // Object set(int index, Object ele):设置指定index位置的元素为ele , 相当于是替换.
- list.set(1, "玛丽");
- System.out.println("list=" + list);
- // List subList(int fromIndex, int toIndex):返回从fromIndex到toIndex位置的子集合
- // 注意返回的子集合 fromIndex <= subList < toIndex
- List returnlist = list.subList(0, 2);
- System.out.println("returnlist=" + returnlist);
-
- }
- }
3、List三种遍历方式(ArrayList、LinkedList,Vector):
2、ArrayList底层操作机制源码分析
transient Object[] elementData;//transient关键字表示,该属性不会被序列化
(1)无参与有参构造器创建和使用ArrayList源码分析:
2、Vector和ArrayList比较:
3、Vector底层扩容:
- public class Vector_ {
- public static void main(String[] args) {
- //无参构造器
- //有参数的构造
- 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);
- }
- */
-
- }
- }
- public class LinkedListCRUD {
- public static void main(String[] args) {
-
- 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.将新的结点,加入到双向链表的最后
- Node(Node<E> prev, E element, Node<E> next) {
- this.item = element;
- this.next = next;
- this.prev = prev;
- }
- 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;
- }
- */
- }
- }
1)如果改查操作多,选择ArrayList
2)如果增删操作多,选择LinkedList
3)一般来说,在程序中,80%-90%都是查询,因此大部分情况下都会选择ArrayList
4)在一个项目中,根据业务灵活选择。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。