zoukankan      html  css  js  c++  java
  • 集合类——集合输出、栈和队列及Collections集合

    1、集合输出

    在之前我们利用了toString()get()方法对集合进行了输出,其实那都不是集合的标准输出,集合输出有四种方式:IteratorListIteratorEnumerationforeach

    (1)Iterator(迭代输出)

    jdk1.5之前,在Collection接口中就有iterator()方法来获取Iterator接口的实例化对象,而在jdk1.5之后该方法被提升到Iterable接口中,但是不管怎么提升,只要Collection有该方法,则ListSet也有此方法。

    Iterator接口中的方法:

    1. 判断是否有下一个元素: public boolean hasNext();

    2. 取得当前元素: public E next();

    3. 删除元素: public default void remove();

    1. /*
    2. * Iterator输出
    3. * */
    4. public class Test{
    5. public static void main(String[] args) {
    6. List<String> list = new ArrayList<>();
    7. list.add("hello");
    8. list.add("world!");
    9. list.add(" hello");
    10. list.add("lemon!");
    11. //实例化Iterator对象
    12. Iterator<String> iterable = list.iterator();
    13. //循环输出
    14. while(iterable.hasNext()) {
    15. System.out.print(" "+iterable.next());
    16. }
    17. }
    18. }

                                 

    下面我们来观察在集合输出的过程中对集合中的元素进行更改的情况:

    1. /*
    2. * 输出过程中对集合内容进行更改
    3. * */
    4. public class Test{
    5. public static void main(String[] args) {
    6. List<String> list = new ArrayList<>();
    7. list.add("hello");
    8. list.add("world!");
    9. list.add(" hello");
    10. list.add("lemon!");
    11. //实例化Iterator对象
    12. Iterator<String> iterable = list.iterator();
    13. //循环输出
    14. while(iterable.hasNext()) {
    15. String string = iterable.next();
    16. if(string.equals("hello")) {
    17. //使用集合提供的remove()方法时,会抛出java.util.ConcurrentModificationException异常
    18. //list.remove(string);
    19. //使用Iterator的remove()方法则不会抛出异常
    20. iterable.remove();
    21. continue;
    22. }
    23. System.out.print(" "+string);
    24. }
    25. }
    26. }

    使用listremove()方法运行结果:


    使用Iteratorremove()方法运行结果:

    所以一般在集合输出时,我们不对其元素进行修改。

    (2)ListIterator(双向迭代接口)

    只有List接口有,而Set接口没有。

    1. /*
    2. * ListIterator双向迭代输出
    3. * */
    4. public class Test{
    5. public static void main(String[] args) {
    6. List<String> list = new ArrayList<>();
    7. list.add("hello");
    8. list.add("world!");
    9. list.add("hello");
    10. list.add("lemon!");
    11. //实例化Iterator对象
    12. ListIterator<String> listIterator = list.listIterator();
    13. // //向前循环输出
    14. // while(listIterator.hasPrevious()) {
    15. // System.out.print(" "+listIterator.previous());
    16. // }
    17. //向后循环输出
    18. while(listIterator.hasNext()) {
    19. System.out.print(" "+listIterator.next());
    20. }
    21. System.out.println();
    22. //向前循环输出
    23. while(listIterator.hasPrevious()) {
    24. System.out.print(" "+listIterator.previous());
    25. }
    26. }
    27. }

    先向后输出在向前输出运行结果:


    在向后输出之前先进行向前输出运行结果:

    所以要使用双向迭代输出,不仅要是List的子类,而且必须先实现向后输出才可执行向前输出,要不然不能出现向前执行的结果,无法实现双向。

    (3)Enumeration(枚举输出)

    只有Vector类才有

    Enumeration的接口定义

     判断是否有下一个元素:public  boolean  hasMoreElements();

     取得元素:public  E  nextElement();

    但是要想取得这个接口的实例化对象,是不能依靠CollectionListSet等接口的。只能够依靠Vector子类,因为Enumeration最早的设计就是为Vector服务的,在Vector类中提供有一个取得Enumeration接口对象的方法:

    取得Enumeration接口对象:public Enumeration elements()

    1. /*
    2. * Enumeration枚举输出
    3. * */
    4. public class Test{
    5. public static void main(String[] args) {
    6. Vector< String> vector = new Vector<>();
    7. vector.add("hello");
    8. vector.add("lemon!");
    9. vector.add("hello");
    10. vector.add("world!");
    11. //取得对象
    12. Enumeration<String> enumeration = vector.elements();
    13. //循环输出
    14. while(enumeration.hasMoreElements()) {
    15. System.out.print(" "+enumeration.nextElement());
    16. }
    17. }
    18. }

    (4)foreach

    JDK1.5开始foreach可以输出数组,实际上除了数组之外也可以输出集合.

    1. /*
    2. * foreach输出
    3. * */
    4. public class Test{
    5. public static void main(String[] args) {
    6. Vector< String> vector = new Vector<>();
    7. vector.add("hello");
    8. vector.add("lemon!");
    9. vector.add("hello");
    10. vector.add("world!");
    11. for (String string : vector) {
    12. System.out.print(" "+string);
    13. }
    14. }
    15. }
                               

    2、栈和队列

    (1)Stack栈(类)

    栈是一种后进先出的数据结构,常见的浏览器的退出、文件的撤销等都属于栈的功能。

    Java中提供有Stack类,这个类是Vector的子类。

    public class Stack<E> extends Vector<E>

    Stack在使用时不是使用Vector的方法而是使用其自定义的方法,且在使用时不需要向上转型,因为要操作的方法不是有List定义的而是Stack自定义的。

    常用方法:

    入栈:public E push(E item)

    出栈:public synchronized E pop()

    返回栈顶元素:public synchronized E peek()


    1. /*
    2. * Stack栈
    3. * */
    4. public class Test{
    5. public static void main(String[] args) {
    6. Stack<String> stack = new Stack<>();
    7. //入栈
    8. stack.push("hello");
    9. stack.push("world");
    10. stack.push("hello");
    11. stack.push("lemon");
    12. System.out.println("打印栈中元素:"+stack);
    13. //取栈顶元素
    14. System.out.println("栈顶元素:"+stack.peek());
    15. //出栈
    16. System.out.println("出栈元素为:");
    17. System.out.println(stack.pop());
    18. System.out.println(stack.pop());
    19. System.out.println(stack.pop());
    20. System.out.println(stack.pop());
    21. //对空栈进行出栈操作
    22. System.out.println(stack.pop());
    23. }
    24. }

                       

    (2)Queue队列(接口)

    Stack栈是后进先出,而与之对应的Queue队列则是先进先出。

    java.util包下的Queue接口实现队列操作,而Queue提供有一个子类LinkedList.

    public interface Queue<E> extends Collection<E>

            

    常用方法:

    入队列:public boolean add(E e)

    出队列:public E poll()

    取队首元素:public E peek()


    1. /*
    2. * Queue队列
    3. * */
    4. public class Test{
    5. public static void main(String[] args) {
    6. Queue<String> queue = new LinkedList<>();
    7. //入队列
    8. queue.add("hello");
    9. queue.add("world");
    10. queue.add("hello");
    11. queue.add("lemon");
    12. //取队首元素
    13. System.out.println("队首元素:"+queue.peek());
    14. //出队列
    15. System.out.println("出队元素:");
    16. System.out.print(queue.poll());
    17. System.out.print(" "+queue.poll());
    18. System.out.print(" "+queue.poll());
    19. System.out.print(" "+queue.poll());
    20. }
    21. }

                                              

    3、Collections工具类

    Collections是一个集合操作的工具类,包括集合的反转、排序等操作。

    1. /*
    2. *Collections工具类
    3. * */
    4. public class Test{
    5. public static void main(String[] args) {
    6. List<String> list = new ArrayList<>();
    7. //相当于调用三次add()方法
    8. Collections.addAll(list,"hello","world","lemon");
    9. System.out.println("反转之前的集合:"+list);
    10. //对集合进行反转
    11. Collections.reverse(list);
    12. System.out.println("反转之后的集合:"+list);
    13. List<Integer> list1 = new ArrayList<>();
    14. Collections.addAll(list1, 4,9,6,3,1,8);
    15. System.out.println("排序前集合:"+list1);
    16. //对list1集合进行排序
    17. Collections.sort(list1);
    18. System.out.println("排序后集合:"+list1);
    19. }
    20. }

                     





  • 相关阅读:
    SQLSERVER中的sp_reset_connection存储过程的作用
    SQLSERVER数据库经常置疑的原因
    sqlserver2005数据库邮件
    SQLSERVER书签查找的通俗理解
    msdb数据库里的表究竟存储什么信息
    造成阻塞和死锁的3大原因:
    SQLSERVER中的锁资源类型RID KEY PAG EXT TAB DB FIL
    总结一下要影响SQLSERVER锁的申请和释放行为要考虑的因素
    Linux下getsockopt/setsockopt 函数说明
    HTTP协议详解(转)
  • 原文地址:https://www.cnblogs.com/edda/p/12601166.html
Copyright © 2011-2022 走看看