[关闭]
@nextleaf 2018-08-20T03:53:26.000000Z 字数 18686 阅读 693

2018-08-16 工作日志

Java 异常 工作日志 集合 List


手动抛出异常

  1. throw new Exception("");
  2. //....
  3. getMessage;

自定义异常

TestMyException.java

  1. package com.nl.sx816;
  2. import com.nl.sx807.HeapStackPractice.Student;
  3. /**
  4. * Created with IntelliJ IDEA 2018.
  5. * Description: 使用自定义异常类MyException
  6. *
  7. * @author: 黄昭鸿
  8. * @date: 2018-08-16
  9. * Time: 11:23
  10. */
  11. public class TestMyException {
  12. public static void main(String[] args){
  13. Student student=new Student("小明","hx001",10);
  14. try {
  15. student.showInfo();
  16. student.regist(-12);
  17. } catch (MyException e) {
  18. System.out.println(e.getMessage());
  19. //e.printStackTrace();
  20. }
  21. }
  22. }

Java 集合框架

集合是一个对象,可容纳其他对象的引用。集合接口声明对每一种类型的集合可以执行的操作。
集合框架的类和接口均在java.util包中。
任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换。

主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射。Collection 接口又有 3 种子类型,List、Set 和 Queue,再下面是一些抽象类,最后是具体实现类,常用的有 ArrayList、LinkedList、HashSet、LinkedHashSet、HashMap、LinkedHashMap 等等
所有集合里只能存放引用类型(对象、包装类)

Java 集合框架
此处输入图片的描述
Java集合框架

1. 接口:

接口

2. 实现:

通用实现
接口 哈希表实现 可调整大小的数组实现 树实现 链接列表实现 哈希表+链表实现
Set HashSet   TreeSet   LinkedHashSet
List   ArrayList   LinkedList  
Queue          
Deque   ArrayDeque   LinkedList  
Map HashMap   TreeMap   LinkedHashMap

3. 算法:

  1. Sorting归并排序
  2. Shuffling随机打乱
  3. Routine Data Manipulation
  4. Searching
  5. Composition
  6. Finding Extreme Values

集合接口

官方文档、相关博文: Java 集合深入理解
接口

1. Collection 接口

Collection 是最基本的集合接口,继承于Iterable接口,一个 Collection 代表一组 Object,即 Collection 的元素, Java不提供直接继承自Collection的类,只提供继承于的子接口(如List和set)。Collection 接口存储一组不唯一,无序的对象。

List 接口

List接口是一个有序的 Collection,使用此接口能够精确的控制每个元素插入的位置,能够通过索引(元素在List中位置,类似于数组的下标)来访问List中的元素,第一个元素的索引为 0,而且允许有相同的元素。
List 接口存储一组不唯一,有序(插入顺序)的对象。

Set 接口

Set 具有与 Collection 完全一样的接口,只是行为上不同,Set 不保存重复的元素。
Set 接口存储一组唯一,无序的对象。
SortedSet接口,继承于Set保存有序的集合。

Queue接口

Set和List的区别

  1. Set 接口实例存储的是无序的,不重复的数据。List 接口实例存储的是有序的,可以重复的元素。
  2. Set检索效率低下,删除和插入效率高,插入和删除不会引起元素位置改变 <实现类有HashSet,TreeSet>。
  3. List和数组类似,可以动态增长,根据实际存储的数据的长度自动增长List的长度。查找元素效率高,插入删除效率低,因为会引起其他元素位置改变 <实现类有ArrayList,LinkedList,Vector> 。

2. Map接口

Map 接口存储一组键值对象,提供key(键)到value(值)的映射。
主要子接口有SortedMap接口。

Map.Entry接口

描述在一个Map中的一个元素(键/值对)。是一个Map的内部接口。

SortedMap接口

继承于 Map,使 Key 保持在升序排列。

3. Enumeration接口

这是一个传统的接口和定义的方法,通过它可以枚举(一次获得一个)对象集合中的元素。这个传统接口已被迭代器取代

工具类

Collections类

对于List 的相关算法

  1. package com.nl.sx816.CollectionFramework.ArraysUtil;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. /**
  5. * Created with IntelliJ IDEA 2018.
  6. * Description: Arrays类和Collections类
  7. * Arrays类包含用于操作数组的各种方法还包含一个静态工厂,允许将数组视为列表。
  8. *
  9. * Collections类是操作集合的工具类,包含对集合进行操作或返回集合的静态方法。
  10. *
  11. * 算法:
  12. * Collections对于List 的相关算法
  13. * sort ,归并排序(小-->大)
  14. * shuffle ,随机打乱
  15. * reverse ,反转元素顺序
  16. * swap ,交换
  17. * binarySearch ,二分查找
  18. *
  19. * static <E> Collection<E> checkedCollection(Collection<E> c, Class<E> type) 返回指定集合的​​动态类型安全视图。
  20. * static <E> List<E> checkedList(List<E> list, Class<E> type) 返回指定列表的动态类型安全视图。
  21. * static <K,V> Map<K,V> checkedMap(Map<K,V> m, Class<K> keyType, Class<V> valueType) 返回指定映射的动态类型安全视图。
  22. *
  23. * boolean addAll(Collection<? super T> c, T... elements) 将所有指定的元素添加到指定的集合中。
  24. * static <E> Set<E> checkedSet(Set<E> s, Class<E> type)返回指定集的动态类型安全视图。
  25. * static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K,V> m, Class<K> keyType, Class<V> valueType) 返回指定有序映射的动态类型安全视图。
  26. * static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s, Class<E> type) 返回指定有序集的动态类型安全视图。
  27. *
  28. *
  29. * @author: 黄昭鸿
  30. * @date: 2018-08-17
  31. * Time: 10:45
  32. */
  33. public class TestCollections {
  34. public static void main(String[] args){
  35. }
  36. }

Arrays类

  1. package com.nl.sx816.CollectionFramework.ArraysUtil;
  2. import com.nl.sx807.HeapStackPractice.Student;
  3. import java.util.Arrays;
  4. /**
  5. * Created with IntelliJ IDEA 2018.
  6. * Description: Arrays工具类
  7. *
  8. * @author: 黄昭鸿
  9. * @date: 2018-08-20
  10. * Time: 9:43
  11. */
  12. public class TestArrays {
  13. public static void main(String[] args) {
  14. //sort 将指定的数组按升序排序,可指定范围。
  15. //asList,返回由指定数组支持的固定大小的列表
  16. //List<String> stooges = Arrays.asList("Larry","Moe","Curly");
  17. // binarySearch使用二进制搜索算法搜索指定值的数组的范围
  18. //copyOfRange将指定数组的指定范围复制到新数组中
  19. //fill 将值分配给数组的指定范围的每个元素。
  20. //parallelSort(int[] a, int fromIndex, int toIndex) 将指定的数组范围按数字升序排序。
  21. //parallelSort(T[] a, Comparator<? super T> cmp) //根据指定比较器引发的顺序对指定的对象数组进行排序。
  22. //parallelSort(T[] a, int fromIndex, int toIndex) //根据元素的自然顺序(compareTo(T o)),将指定对象数组的指定范围按升序 排序。
  23. //spliterator(T[] array, int startInclusive, int endExclusive) 返回Spliterator覆盖指定数组的指定范围。
  24. int[] arrs = {12, 23, 45, 56, 78, 89, 34, 45, 56, 67, 89, 98};
  25. print(arrs);
  26. //对数组排序
  27. Arrays.sort(arrs);
  28. print(arrs);
  29. Student[] students = {
  30. new Student("张三丰", "1", 105),
  31. new Student("里斯", "2", 46),
  32. new Student("王武", "3", 99),
  33. new Student("赵莉丰", "4", 13)
  34. };
  35. for (Student s : students) {
  36. System.out.println(s);
  37. }
  38. //当需要对某个类的对象进行排序时,该类需要实现Comparable<T>接口 必须重写compareTo方法
  39. Arrays.sort(students);
  40. System.out.println();
  41. for (Student s : students) {
  42. System.out.println(s);
  43. }
  44. }
  45. public static void print(int[] ints) {
  46. for (int i : ints) {
  47. System.out.print(i + " ");
  48. }
  49. System.out.println();
  50. }
  51. }

数组转成 List 的方法 asList :

  1. @SafeVarargs
  2. public static <T> List<T> asList(T... array) {
  3. return new ArrayList<T>(array);
  4. }

集合实现类(标准集合类)

序号 类描述
1 AbstractCollection 
实现了大部分的集合接口。
2 AbstractList 
继承于AbstractCollection 并且实现了大部分List接口。
3 AbstractSequentialList 
继承于 AbstractList ,提供了对数据元素的链式访问而不是随机访问。
4 LinkedList

该类实现了List接口,允许有null(空)元素。主要用于创建链表数据结构,该类没有同步方法,如果多个线程同时访问一个List,则必须自己实现访问同步,解决方法就是在创建List时候构造一个同步的List。例如:

Listlist=Collections.synchronizedList(newLinkedList(...));

LinkedList 查找效率低。

5 ArrayList

该类也是实现了List的接口,实现了可变大小的数组,随机访问和遍历元素时,提供更好的性能。该类也是非同步的,在多线程的情况下不要使用。ArrayList 增长当前长度的50%,插入删除效率低。 线程不同步

6 AbstractSet 
继承于AbstractCollection 并且实现了大部分Set接口。
7 HashSet

该类实现了Set接口,不允许出现重复元素,不保证集合中元素的顺序,允许包含值为null的元素,但最多只能一个。

8 LinkedHashSet
具有可预知迭代顺序的 Set 接口的哈希表和链接列表实现。
9 TreeSet

该类实现了Set接口,可以实现排序等功能。

10 AbstractMap 
实现了大部分的Map接口。
11 HashMap
HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。
该类实现了Map接口,根据键的HashCode值存储数据,具有很快的访问速度,最多允许一条记录的键为null,不支持线程同步。
12 TreeMap
继承了AbstractMap,并且使用一颗树。
13 WeakHashMap
继承AbstractMap类,使用弱密钥的哈希表。
14 LinkedHashMap
继承于HashMap,使用元素的自然顺序对元素进行排序.
15 IdentityHashMap
继承AbstractMap类,比较文档时使用引用相等。

上午讲自定义异常类

下午讲了List

ArrayList VS LinkedList

ArrayList

LinkedList

ArrayList与 Linkedlist的区别

Array List釆用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入删除时非常麻烦
Linkedlist采用的将对象存放在独立的空间中而且在每个空间中还保存下一个链接的索引但是缺点就是查找非常麻烦要丛第一个索引开始,优点是插入和删除非常方便

ArrayListDemo.java

  1. package com.nl.sx816.CollectionFramework.ListDemo;
  2. /*包名统一使用小写,点分隔符之间有且仅有一个自然语义的英语单词。包名统一使用单数形式*/
  3. import cn.hutool.core.lang.Console;
  4. import java.util.ArrayList;
  5. /**
  6. * Created with IntelliJ IDEA 2018.
  7. * Description: List——有序可重复、元素可以为 null
  8. * LinkedList,有List共有特点
  9. * 不同步(线程不安全)
  10. * 容量不固定,想放多少放多少(当然有最大阈值,但一般达不到)
  11. * 效率高:
  12. * size(), isEmpty(), get(), set() iterator(), ListIterator() 方法的时间复杂度都是 O(1)
  13. * add() 添加操作的时间复杂度平均为 O(n)
  14. * 其他所有操作的时间复杂度几乎都是 O(n)
  15. * 占用空间更小
  16. * 对比 LinkedList,不用占用额外空间维护链表结构
  17. *
  18. * List——有序可重复、元素可以为 null
  19. *
  20. * @author: 黄昭鸿
  21. * @date: 2018-08-16
  22. * Time: 16:21
  23. */
  24. /**
  25. * 类说明:
  26. * boolean add(E e) 将指定的元素添加到此列表的尾部。
  27. * void add(int index, E element) 将指定的元素插入此列表中的指定位置。
  28. * void clear() 移除此列表中的所有元素。
  29. * Object clone() 返回此 ArrayList 实例的浅表副本。
  30. * boolean contains(Object o) 如果此列表中包含指定的元素,则返回 true。
  31. * E get(int index) 返回此列表中指定位置上的元素。
  32. * int indexOf(Object o) 返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1。
  33. * boolean isEmpty() 如果此列表中没有元素,则返回 true
  34. * int lastIndexOf(Object o) 返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。
  35. * E remove(int index) 移除此列表中指定位置上的元素。
  36. * boolean remove(Object o) 移除此列表中首次出现的指定元素(如果存在)。
  37. * protected void removeRange(int fromIndex, int toIndex) 移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素,向左移动所有后续元素。
  38. * E set(int index, E element) 用指定的元素替代此列表中指定位置上的元素。
  39. * int size() 返回此列表中的元素数。
  40. * Object[] toArray() 按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组。
  41. * <T> T[] toArray(T[] a) 按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。
  42. */
  43. public class ArrayListDemo {
  44. public static void main(String[] args){
  45. //数组初始容量为10(private static final int DEFAULT_CAPACITY = 10;),size <= 容量
  46. //空数组
  47. ArrayList arrayList=new ArrayList();
  48. arrayList.add(1);
  49. arrayList.add(2);
  50. arrayList.add(3);
  51. //指定容量
  52. ArrayList arrayList2=new ArrayList(5);
  53. arrayList2.add("a");
  54. arrayList2.add("b");
  55. arrayList2.add("c");
  56. //直接创建和指定集合一样内容的 ArrayList
  57. ArrayList arrayList3=new ArrayList(arrayList2);
  58. Console.log(arrayList.hashCode());
  59. Console.log(arrayList2.hashCode());
  60. Console.log(arrayList3.hashCode());
  61. arrayList.addAll(arrayList3);
  62. Console.log(arrayList);
  63. Console.log(arrayList2);
  64. Console.log(arrayList.hashCode());
  65. Console.log(arrayList2.hashCode());
  66. Console.log(arrayList3.hashCode());
  67. }
  68. }

运行输出:

  1. 30817
  2. 126145
  3. 126145
  4. [1, 2, 3, a, b, c]
  5. [a, b, c]
  6. 918165601
  7. 126145
  8. 126145
  9. Process finished with exit code 0

LinkedListDemo.java

  1. package com.nl.sx816.CollectionFramework.ListDemo;
  2. import cn.hutool.core.lang.Console;
  3. import java.util.Iterator;
  4. import java.util.LinkedList;
  5. import java.util.ListIterator;
  6. /**
  7. * Created with IntelliJ IDEA 2018.
  8. * Description: List——有序可重复、元素可以为 null
  9. * LinkedList,有List共有特点
  10. * 双向链表实现
  11. * 所有指定位置的操作都是从头开始遍历进行的
  12. * 和 ArrayList 一样,不是同步容器(线程不安全)
  13. *
  14. * @author: 黄昭鸿
  15. * @date: 2018-08-16
  16. * Time: 16:17
  17. */
  18. public class LinkedListDemo {
  19. /**
  20. * 默认值长度为0
  21. */
  22. private LinkedList<String> linkedList = new LinkedList<>();
  23. public static void main(String[] args) {
  24. //单个方法的总行数不要超过80行。
  25. LinkedListDemo linkedListDemo = new LinkedListDemo();
  26. //添加到此列表的结尾
  27. System.out.print(linkedListDemo.linkedList.add("天津"));
  28. System.out.print(linkedListDemo.linkedList.add("上海"));
  29. System.out.print(linkedListDemo.linkedList.add("北京"));
  30. System.out.print(linkedListDemo.linkedList.add("贵州"));
  31. System.out.print(linkedListDemo.linkedList.add("广州"));
  32. System.out.print(linkedListDemo.linkedList.add("杭州"));
  33. System.out.print(linkedListDemo.linkedList.add("苏州"));
  34. System.out.print(linkedListDemo.linkedList.add("荆州"));
  35. System.out.print(linkedListDemo.linkedList.add("郑州"));
  36. System.out.print(linkedListDemo.linkedList.add("兰州"));
  37. System.out.print(linkedListDemo.linkedList.add("福州"));
  38. System.out.print(linkedListDemo.linkedList.add("柳州"));
  39. System.out.print(linkedListDemo.linkedList.add(null));
  40. System.out.print(linkedListDemo.linkedList.add("钦州"));
  41. System.out.println(linkedListDemo.linkedList.add("北京"));
  42. System.out.print("add(E e) 方法:");
  43. Console.log(linkedListDemo.linkedList);
  44. //交换俩元素位置
  45. System.out.println("交换俩元素位置:");
  46. swap(linkedListDemo.linkedList, 0, 5);
  47. Console.log(linkedListDemo.linkedList);
  48. //插入此列表的开头addFirst(E e)
  49. System.out.print("addFirst(E e)方法:");
  50. linkedListDemo.linkedList.addFirst("开头");
  51. Console.log(linkedListDemo.linkedList);
  52. //addLast(E e)将指定元素添加到此列表的结尾。
  53. System.out.print("addLast(E e)方法:");
  54. linkedListDemo.linkedList.addLast("结尾");
  55. Console.log(linkedListDemo.linkedList);
  56. //contains(Object o) 如果此列表包含指定元素,则返回 true。
  57. System.out.println("是否包含“天津”:" + linkedListDemo.linkedList.contains("天津"));
  58. //element()获取但不移除此列表的头(第一个元素)。
  59. System.out.println("头:" + linkedListDemo.linkedList.element());
  60. //E get(int index)返回此列表中指定位置处的元素。
  61. System.out.println("指定位置的元素:" + linkedListDemo.linkedList.get(5));
  62. //getFirst()返回此列表的第一个元素。
  63. System.out.println("第一个元素:" + linkedListDemo.linkedList.getFirst());
  64. // E getLast()返回此列表的最后一个元素。
  65. System.out.println("最后一个元素:" + linkedListDemo.linkedList.getLast());
  66. // int indexOf(Object o) 返回此列表中首次出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1。
  67. System.out.println("索引:" + linkedListDemo.indexOf("北京"));
  68. System.out.println("索引:" + linkedListDemo.indexOf("九州"));
  69. // int lastIndexOf(Object o) 返回此列表中最后出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1。
  70. System.out.println("索引:" + linkedListDemo.lastIndexOf("北京"));
  71. //boolean offer(E e)将指定元素添加到此列表的末尾。
  72. System.out.println("元素添加到此列表的末尾(offer)" + linkedListDemo.linkedList.offer("A"));
  73. // boolean offerFirst(E e)在此列表的开头插入指定的元素。
  74. System.out.println("在列表的开头插入元素(offerFirst):" + linkedListDemo.linkedList.offerFirst("B"));
  75. // boolean offerLast(E e)在此列表末尾插入指定的元素
  76. System.out.println("元素添加到此列表的末尾(offerLast):" + linkedListDemo.linkedList.offerLast("C"));
  77. Console.log(linkedListDemo.linkedList);
  78. // E peek() 获取但不移除此列表的头(第一个元素)。
  79. // E peekFirst() 获取但不移除此列表的第一个元素;如果此列表为空,则返回 null。
  80. // E peekLast() 获取但不移除此列表的最后一个元素;如果此列表为空,则返回 null。
  81. // E poll() 获取并移除此列表的头(第一个元素)
  82. // E pollFirst() 获取并移除此列表的第一个元素;如果此列表为空,则返回 null。
  83. // E pollLast() 获取并移除此列表的最后一个元素;如果此列表为空,则返回 null。
  84. // E pop() 从此列表所表示的堆栈处弹出一个元素。
  85. System.out.println("弹出的是“" + linkedListDemo.linkedList.pop() + "”,结果:");
  86. Console.log(linkedListDemo.linkedList);
  87. // void push(E e) 将元素推入此列表所表示的堆栈。
  88. System.out.println("将元素推入此列表所表示的堆栈:");
  89. linkedListDemo.linkedList.push("push");
  90. Console.log(linkedListDemo.linkedList);
  91. // E remove() 获取并移除此列表的头(第一个元素)。
  92. // E remove(int index) 移除此列表中指定位置处的元素。
  93. System.out.println("移除指定位置(6)处的元素“" + linkedListDemo.linkedList.remove(6) + "”后:");
  94. Console.log(linkedListDemo.linkedList);
  95. // boolean remove(Object o) 从此列表中移除首次出现的指定元素(如果存在)。
  96. // E removeFirst() 移除并返回此列表的第一个元素。
  97. // E removeLast() 移除并返回此列表的最后一个元素。
  98. // E set(int index, E element) 将此列表中指定位置的元素替换为指定的元素。
  99. System.out.println("替换“" + linkedListDemo.linkedList.set(0, "呃") + "”后:");
  100. Console.log(linkedListDemo.linkedList);
  101. // Object[] toArray() 返回以适当顺序(从第一个元素到最后一个元素)包含此列表中所有元素的数组。
  102. //<T> T[] toArray(T[] a) 返回以适当顺序(从第一个元素到最后一个元素)包含此列表中所有元素的数组;返回数组的运行时类型为指定数组的类型。
  103. //如果指定数组能容纳列表,则在其中返回该列表。否则,分配具有指定数组的运行时类型和此列表大小的新数组。
  104. //如果指定数组能容纳列表,并有剩余空间(即数组比列表元素多),则紧跟在列表末尾的数组元素会被设置为 null(只有在调用者知道列表不包含任何 null 元素时,才可使用此方法来确定列表的长度。)
  105. //String[] strings=new String[linkedListDemo.linkedList.size()];
  106. //linkedListDemo.linkedList.toArray(strings);
  107. //Console.log(strings);
  108. Console.log((linkedListDemo.linkedList.toArray(new String[0])));
  109. //listIterator迭代器
  110. ListIterator lit = linkedListDemo.linkedList.listIterator();
  111. while (lit.hasNext()) {
  112. System.out.print(lit.next());
  113. }
  114. System.out.println();
  115. //倒序迭代器
  116. Iterator x =linkedListDemo.linkedList.descendingIterator();
  117. while (x.hasNext()) {
  118. System.out.print(x.next());
  119. }
  120. }
  121. public String indexOf(String o) {
  122. int t = linkedList.indexOf(o);
  123. if (t == (-1)) {
  124. return "此列表中不包含“" + o + "”元素";
  125. } else {
  126. return String.valueOf(t);
  127. }
  128. }
  129. public String lastIndexOf(String o) {
  130. int t = linkedList.lastIndexOf(o);
  131. if (t == (-1)) {
  132. return "此列表中不包含“" + o + "”元素";
  133. } else {
  134. return String.valueOf(t);
  135. }
  136. }
  137. //元素交换(工具类Collections中的方法)
  138. public static <E> void swap(LinkedList<E> a, int i, int j) {
  139. E tmp = a.get(i);
  140. a.set(i, a.get(j));
  141. a.set(j, tmp);
  142. }
  143. }

运行输出:

  1. truetruetruetruetruetruetruetruetruetruetruetruetruetruetrue
  2. add(E e) 方法:[天津, 上海, 北京, 贵州, 广州, 杭州, 苏州, 荆州, 郑州, 兰州, 福州, 柳州, null, 钦州, 北京]
  3. 交换俩元素位置:
  4. [杭州, 上海, 北京, 贵州, 广州, 天津, 苏州, 荆州, 郑州, 兰州, 福州, 柳州, null, 钦州, 北京]
  5. addFirst(E e)方法:[开头, 杭州, 上海, 北京, 贵州, 广州, 天津, 苏州, 荆州, 郑州, 兰州, 福州, 柳州, null, 钦州, 北京]
  6. addLast(E e)方法:[开头, 杭州, 上海, 北京, 贵州, 广州, 天津, 苏州, 荆州, 郑州, 兰州, 福州, 柳州, null, 钦州, 北京, 结尾]
  7. 是否包含“天津”:true
  8. 头:开头
  9. 指定位置的元素:广州
  10. 第一个元素:开头
  11. 最后一个元素:结尾
  12. 索引:3
  13. 索引:此列表中不包含“九州”元素
  14. 索引:15
  15. 元素添加到此列表的末尾(offertrue
  16. 在列表的开头插入元素(offerFirst):true
  17. 元素添加到此列表的末尾(offerLast):true
  18. [B, 开头, 杭州, 上海, 北京, 贵州, 广州, 天津, 苏州, 荆州, 郑州, 兰州, 福州, 柳州, null, 钦州, 北京, 结尾, A, C]
  19. 弹出的是“B”,结果:
  20. [开头, 杭州, 上海, 北京, 贵州, 广州, 天津, 苏州, 荆州, 郑州, 兰州, 福州, 柳州, null, 钦州, 北京, 结尾, A, C]
  21. 将元素推入此列表所表示的堆栈:
  22. [push, 开头, 杭州, 上海, 北京, 贵州, 广州, 天津, 苏州, 荆州, 郑州, 兰州, 福州, 柳州, null, 钦州, 北京, 结尾, A, C]
  23. 移除指定位置(6)处的元素“广州”后:
  24. [push, 开头, 杭州, 上海, 北京, 贵州, 天津, 苏州, 荆州, 郑州, 兰州, 福州, 柳州, null, 钦州, 北京, 结尾, A, C]
  25. 替换“push”后:
  26. [呃, 开头, 杭州, 上海, 北京, 贵州, 天津, 苏州, 荆州, 郑州, 兰州, 福州, 柳州, null, 钦州, 北京, 结尾, A, C]
  27. [呃, 开头, 杭州, 上海, 北京, 贵州, 天津, 苏州, 荆州, 郑州, 兰州, 福州, 柳州, null, 钦州, 北京, 结尾, A, C]
  28. 呃开头杭州上海北京贵州天津苏州荆州郑州兰州福州柳州null钦州北京结尾AC
  29. CA结尾北京钦州null柳州福州兰州郑州荆州苏州天津贵州北京上海杭州开头呃
  30. Process finished with exit code 0

附:

自定义异常类(检查性)
MyExceptionChecked.java

  1. package com.nl.sx816;
  2. import java.security.PrivilegedActionException;
  3. /**
  4. * Created with IntelliJ IDEA 2018.
  5. * Description: Java异常——自定义异常类——checked exception
  6. *
  7. * 如果要自定义异常类,则扩展Exception类即可,因此这样的自定义异常都属于检查异常(checked exception)。
  8. * 如果要自定义非检查异常,则扩展自RuntimeException。
  9. *
  10. * 按照国际惯例,自定义的异常应该总是包含如下的构造函数:
  11. *
  12. * 一个无参构造函数
  13. * 一个带有String参数的构造函数,并传递给父类的构造函数。
  14. * 一个带有String参数和Throwable参数,并都传递给父类构造函数
  15. * 一个带有Throwable 参数的构造函数,并传递给父类的构造函数。
  16. *
  17. * 通常还会给出serialVersionUID
  18. * @author: 黄昭鸿
  19. * @date: 2018-08-16
  20. * Time: 11:25
  21. */
  22. public class MyExceptionChecked extends Exception {
  23. private static final long serialVersionUID = -4407200639975626465L;
  24. /**
  25. * Constructs a new exception with {@code null} as its detail message.
  26. * The cause is not initialized, and may subsequently be initialized by a
  27. * call to {@link #initCause}.
  28. */
  29. public MyExceptionChecked() { }
  30. /**
  31. * Constructs a new exception with the specified detail message. The
  32. * cause is not initialized, and may subsequently be initialized by
  33. * a call to {@link #initCause}.
  34. *
  35. * @param message the detail message. The detail message is saved for
  36. * later retrieval by the {@link #getMessage()} method.
  37. */
  38. public MyExceptionChecked(String message) {
  39. super(message);
  40. }
  41. /**
  42. * Constructs a new exception with the specified detail message and
  43. * cause. <p>Note that the detail message associated with
  44. * {@code cause} is <i>not</i> automatically incorporated in
  45. * this exception's detail message.
  46. *
  47. * @param message the detail message (which is saved for later retrieval
  48. * by the {@link #getMessage()} method).
  49. * @param cause the cause (which is saved for later retrieval by the
  50. * {@link #getCause()} method). (A {@code null} value is
  51. * permitted, and indicates that the cause is nonexistent or
  52. * unknown.)
  53. * @since 1.4
  54. */
  55. public MyExceptionChecked(String message, Throwable cause) {
  56. super(message, cause);
  57. }
  58. /**
  59. * Constructs a new exception with the specified cause and a detail
  60. * message of {@code (cause==null ? null : cause.toString())} (which
  61. * typically contains the class and detail message of {@code cause}).
  62. * This constructor is useful for exceptions that are little more than
  63. * wrappers for other throwables (for example, {@link
  64. * PrivilegedActionException}).
  65. *
  66. * @param cause the cause (which is saved for later retrieval by the
  67. * {@link #getCause()} method). (A {@code null} value is
  68. * permitted, and indicates that the cause is nonexistent or
  69. * unknown.)
  70. * @since 1.4
  71. */
  72. public MyExceptionChecked(Throwable cause) {
  73. super(cause);
  74. }
  75. /**
  76. * Constructs a new exception with the specified detail message,
  77. * cause, suppression enabled or disabled, and writable stack
  78. * trace enabled or disabled.
  79. *
  80. * @param message the detail message.
  81. * @param cause the cause. (A {@code null} value is permitted,
  82. * and indicates that the cause is nonexistent or unknown.)
  83. * @param enableSuppression whether or not suppression is enabled
  84. * or disabled
  85. * @param writableStackTrace whether or not the stack trace should
  86. * be writable
  87. * @since 1.7
  88. */
  89. public MyExceptionChecked(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
  90. super(message, cause, enableSuppression, writableStackTrace);
  91. }
  92. }

自定义异常类(非检查性异常)
MyException.java(RuntimeException)

  1. package com.nl.sx816;
  2. /**
  3. * Created with IntelliJ IDEA 2018.
  4. * Description: Java异常——自定义异常类——RuntimeException
  5. *
  6. * 如果要自定义异常类,则扩展Exception类即可,因此这样的自定义异常都属于检查异常(checked exception)。
  7. * 如果要自定义非检查异常,则扩展自RuntimeException。
  8. *
  9. * 按照国际惯例,自定义的异常应该总是包含如下的构造函数:
  10. *
  11. * 一个无参构造函数
  12. * 一个带有String参数的构造函数,并传递给父类的构造函数。
  13. * 一个带有String参数和Throwable参数,并都传递给父类构造函数
  14. * 一个带有Throwable 参数的构造函数,并传递给父类的构造函数。
  15. *
  16. * 通常还会给出serialVersionUID
  17. *
  18. * Author: 黄昭鸿
  19. * Date: 2018-08-16
  20. * Time: 10:56
  21. */
  22. public class MyException extends RuntimeException{
  23. private static final long serialVersionUID = 2032238587799006983L;
  24. /**
  25. * Constructs a new runtime exception with {@code null} as its
  26. * detail message. The cause is not initialized, and may subsequently be
  27. * initialized by a call to {@link #initCause}.
  28. */
  29. public MyException() {
  30. super();
  31. }
  32. /**
  33. * Constructs a new runtime exception with the specified detail message.
  34. * The cause is not initialized, and may subsequently be initialized by a
  35. * call to {@link #initCause}.
  36. *
  37. * @param message the detail message. The detail message is saved for
  38. * later retrieval by the {@link #getMessage()} method.
  39. */
  40. public MyException(String message) {
  41. super(message);
  42. }
  43. /**
  44. * Constructs a new runtime exception with the specified detail message and
  45. * cause. <p>Note that the detail message associated with
  46. * {@code cause} is <i>not</i> automatically incorporated in
  47. * this runtime exception's detail message.
  48. *
  49. * @param message the detail message (which is saved for later retrieval
  50. * by the {@link #getMessage()} method).
  51. * @param cause the cause (which is saved for later retrieval by the
  52. * {@link #getCause()} method). (A {@code null} value is
  53. * permitted, and indicates that the cause is nonexistent or
  54. * unknown.)
  55. * @since 1.4
  56. */
  57. public MyException(String message, Throwable cause) {
  58. super(message, cause);
  59. }
  60. /**
  61. * Constructs a new runtime exception with the specified cause and a
  62. * detail message of {@code (cause==null ? null : cause.toString())}
  63. * (which typically contains the class and detail message of
  64. * {@code cause}). This constructor is useful for runtime exceptions
  65. * that are little more than wrappers for other throwables.
  66. *
  67. * @param cause the cause (which is saved for later retrieval by the
  68. * {@link #getCause()} method). (A {@code null} value is
  69. * permitted, and indicates that the cause is nonexistent or
  70. * unknown.)
  71. * @since 1.4
  72. */
  73. public MyException(Throwable cause) {
  74. super(cause);
  75. }
  76. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注