第一章:List集合_List接口介绍

1).特点

        1).有序的;

  2).可以存储重复元素;

  3).可以通过索引访问;

  1.      List<String> list = new ArrayList<>();
  2.  
  3.       list.add("张无忌");
  4.  
  5.      list.add("张三丰");
  6.  
  7.      list.add("章子怡");
  8.  
  9.       list.add("章子怡");//OK的,可以添加
  10.  
  11.      for(String s : list){
  12.  
  13.       System.out.println(s);//有序的
  14.  
  15.      }

 

  2).方法

  继承了Collection接口的所有方法,并且又有很多自己的方法

  1. void add(String item)
  2. 向滚动列表的末尾添加指定的项。
  3. void add(String item, int index)
  4. 向滚动列表中索引指示的位置添加指定的项。
  5. void addActionListener(ActionListener l)
  6. 添加指定的动作侦听器以从此列表接收动作事件。
  7. void addItemListener(ItemListener l)
  8. 添加指定的项侦听器以接收此列表的项事件。
  9. void addNotify()
  10. 创建列表的同位体。
  11. void deselect(int index)
  12. 取消选择指定索引处的项。
  13. AccessibleContext
  14. getAccessibleContext()
  15. 获取与此 List 关联的 AccessibleContext
  16. ActionListener[]
  17. getActionListeners()
  18. 返回已在此列表上注册的所有动作侦听器的数组。
  19. String
  20. getItem(int index)
  21. 获取与指定索引关联的项。
  22. int getItemCount()
  23. 获取列表中的项数。
  24. ItemListener[]
  25. getItemListeners()
  26. 返回已在此列表上注册的所有项侦听器的数组。
  27. String[]
  28. getItems()
  29. 获取列表中的项。
  30. <T extends EventListener>
  31. T[]
  32. getListeners(Class<T> listenerType)
  33. 返回目前已在此 List 上注册为 FooListener 的所有对象的数组。
  34. Dimension
  35. getMinimumSize()
  36. 确定此滚动列表的最小大小。
  37. Dimension
  38. getMinimumSize(int rows)
  39. 获取具有指定行数的列表的最少维数。
  40. Dimension
  41. getPreferredSize()
  42. 获取此滚动列表的首选大小。
  43. Dimension
  44. getPreferredSize(int rows)
  45. 获取具有指定行数的列表的首选维数。
  46. int getRows()
  47. 获取此列表中的可视行数。
  48. int getSelectedIndex()
  49. 获取列表中选中项的索引。
  50. int[] getSelectedIndexes()
  51. 获取列表中选中的索引。
  52. String
  53. getSelectedItem()
  54. 获取此滚动列表中选中的项。
  55. String[]
  56. getSelectedItems()
  57. 获取此滚动列表中选中的项。
  58. Object[]
  59. getSelectedObjects()
  60. 获取对象数组中此滚动列表的选中项。
  61. int getVisibleIndex()
  62. 获取上次由 makeVisible 方法使其可视的项的索引。
  63. boolean isIndexSelected(int index)
  64. 确定是否已选中此滚动列表中的指定项。
  65. boolean isMultipleMode()
  66. 确定此列表是否允许进行多项选择。
  67. void makeVisible(int index)
  68. 使指定索引处的项可视。
  69. protected String
  70. paramString()
  71. 返回表示此滚动列表状态的参数字符串。
  72. protected void processActionEvent(ActionEvent e)
  73. 处理发生在此列表上的动作事件,方法是将这些事件指派给所有已注册的 ActionListener 对象。
  74. protected void processEvent(AWTEvent e)
  75. 此滚动列表的进程事件。
  76. protected void processItemEvent(ItemEvent e)
  77. 处理发生在此列表上的项事件,方法是将这些事件指派给所有已注册的 ItemListener 对象。
  78. void remove(int position)
  79. 从此滚动列表中移除指定位置处的项。
  80. void remove(String item)
  81. 从列表中移除项的第一次出现。
  82. void removeActionListener(ActionListener l)
  83. 移除指定的动作侦听器,以便不再从此列表接收动作事件。
  84. void removeAll()
  85. 从此列表中移除所有项。
  86. void removeItemListener(ItemListener l)
  87. 移除指定的项侦听器,以便不再从此列表接收项事件。
  88. void removeNotify()
  89. 移除此列表的同位体。
  90. void replaceItem(String newValue, int index)
  91. 使用新字符串替换滚动列表中指定索引处的项。
  92. void select(int index)
  93. 选择滚动列表中指定索引处的项。
  94. void setMultipleMode(boolean b)
  95. 设置确定此列表是否允许进行多项选择的标志。

api

  3).常用方法(以下几个方法都是List接口中特有的方法)

      1).增:public void add(int index,E e):将e添加到当前集合的index位置。

       2).删:public E remove(int index):删除index位置上的元素,并将删除的元素返回。

       3).改:public E set​(int index,E element):将element替换index位置上的元素,并将原index位置上的元素返回。

       4).查:public E get​(int index):获取index位置上的元素。

      示例代码:

  

  1. public static void main(String[] args) {
  2. //List集合中增加自己的add方法,add(int index,E e);
  3. List<String> list=new ArrayList<>();
  4.  
  5. list.add("aaaa");
  6. list.add("bbbb");
  7. list.add("cccc");
  8. list.add(1,"dddd");
  9.  
  10. System.out.println(list);
  11. //删除指定索引的元素,并将删除的元素返回
  12. String removeStr=list.remove(2);
  13. System.out.println(removeStr);
  14. System.out.println(list);
  15.  
  16. //修改指定索引位置上的元素set(int index,E e),并将原index位置上的元素返回
  17. String setStr=list.set(2,"ffff");
  18. System.out.println(setStr);
  19. System.out.println(list);
  20.  
  21. //通过索引获取指定索引上的元素
  22. String getStr=list.get(2);
  23. System.out.println("索引为3的元素为:"+getStr);
  24. System.out.println(list);
  25. }

第二章.实现List接口的常用类_ArrayList

  1).list接口常用实现类

  2).ArrayList

    特点:

   查询快----使用索引

  增删慢---需要扩容,移位

    图解:

    方法:

      无特有方法

    案例

  1. public static void main(String[] args) {
  2. //List集合中增加自己的add方法,add(int index,E e);
  3. ArrayList<String> list=new ArrayList<>();
  4.  
  5. list.add("aaaa");
  6. list.add("hhhh");
  7. list.add("cccc");
  8. list.add(1,"dddd");
  9. System.out.println(list);
  10. //删除指定索引的元素,并将删除的元素返回
  11. String removeStr=list.remove(2);
  12. System.out.println(removeStr);
  13. System.out.println(list);
  14. //修改指定索引位置上的元素set(int index,E e),并将原index位置上的元素返回
  15. String setStr=list.set(2,"ffff");
  16. System.out.println(setStr);
  17. System.out.println(list);
  18. //通过索引获取指定索引上的元素
  19. String getStr=list.get(2);
  20. System.out.println("索引为3的元素为:"+getStr);
  21. System.out.println(list);
  22. }

  3).LinkedList

    特点

      使用链表实现

      增删快,查询慢

    图解

    方法

    新增了一些方法,可以模拟栈、队列:

    1).public void push(Object o):压栈    等同于addFirst(E e) ,将指定元素添加到此集合的开头

    2).public
E pop():弹栈--如果没有元素,会抛异常;

      public E poll():弹栈--如果没有元素,会返回null【建议使用】

    案例:

  1. public class Demo {
  2. public static void main(String[] args) {
  3. LinkedList<String> list = new LinkedList<>();
  4. list.push("孙悟空");
  5. list.push("猪八戒");
  6. list.push("沙和尚");
  7. System.out.println(list);
  8.  
  9. while (list.size() > 0) {
  10. System.out.println("弹出一个:" + list.poll());
  11. System.out.println("集合大小:" + list.size());
  12. }
  13. }
  14. }

java_List集合及其实现类的更多相关文章

  1. 操作集合的工具类:Collections

    Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类提供了大量方法对集合进行排序.查询和修改等操作,还提供了将集合对象置为不可变.对集合对象实现同步控制等方法 ...

  2. JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)

    package com.itcast.test20140113; import java.util.ArrayList; import java.util.Arrays; import java.ut ...

  3. 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析

    1.  HashSet与HashMap的联系与区别? 区别:前者是单列后者是双列,就是hashmap有键有值,hashset只有键: 联系:HashSet的底层就是HashMap,可以参考HashSe ...

  4. 操作集合的工具类Collections

    1       操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操 ...

  5. Java集合概述、Set集合(HashSet类、LinkedHashSet类、TreeSet类、EnumSet类)

    Java集合概述.Set集合(HashSet类.LinkedHashSet类.TreeSet类.EnumSet类) 1.Java集合概述1)数组可以保存多个对象,但数组长度不可变,一旦在初始化数组时指 ...

  6. JAVA中的集合容器操作类

    目录 JAVA中的集合容器操作类 List集合 ArrayList的操作方法说明 LinkedList Stack Set Map Queue 总结 JAVA中的集合容器操作类 Java容器类库总共分 ...

  7. 转:C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、Sort)

    C#常用的集合类型(ArrayList类.Stack类.Queue类.Hashtable类.Sort) .ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在Array ...

  8. 集合框架的类和接口均在java.util包中。 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换。

    集合框架的类和接口均在java.util包中. 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换.

  9. Java中各种集合(字符串类)的线程安全性!!!

    Java中各种集合(字符串类)的线程安全性!!! 一.概念: 线程安全:就是当多线程访问时,采用了加锁的机制:即当一个线程访问该类的某个数据时,会对这个数据进行保护,其他线程不能对其访问,直到该线程读 ...

随机推荐

  1. Python练手例子(9)

    49.使用lambda来创建匿名函数. #python3.7 MAXIMUM = lambda x,y : (x > y) * x + (x < y) * y MINIMUM = lamb ...

  2. Python-字典与json的转换

    #json是字符串,只不过长得像字典 import json user_info='''{"niuhy":1234,"shanbl":44566}''' #js ...

  3. Android OpenGL ES 开发(四): OpenGL ES 绘制形状

    在上文中,我们使用OpenGL定义了能够被绘制出来的形状了,现在我们想绘制出来它们.使用OpenGLES 2.0来绘制形状会比你想象的需要更多的代码.因为OpenGL的API提供了大量的对渲染管线的控 ...

  4. 开源播放器 ijkplayer (六) :Android 下使用 ijkplayer 异常处理思路

    一. java.lang.IllegalStateException: mpjni: setOptionLong: null mp 根据已查到的资料看,目前是ijk内部的问题,只能通过try& ...

  5. [Swift]LeetCode60. 第k个排列 | Permutation Sequence

    The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...

  6. [Swift]LeetCode129. 求根到叶子节点数字之和 | Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  7. [Swift]LeetCode842. 将数组拆分成斐波那契序列 | Split Array into Fibonacci Sequence

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

  8. python之zipfile

    1 简述 zip文件是一个常用的归档和与压缩标准. zipfile模块提供了创建.读取.写入.添加及列出zip文件的工具. zipfile里有2个非常常用的class,分别是Zipfile和ZipIn ...

  9. python高级-迭代器(18)

    一.什么是迭代器 迭代是访问集合元素的⼀种⽅式. 迭代器是⼀个可以记住遍历的位置的对象. 迭代器对象从集合的第⼀个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退 二.可迭代对象 直 ...

  10. 分布式_zookeeper

    分布式协调服务-zookeeper 分布式环境的特点 1.分布性 2.并发性 程序运行过程中,并发性操作是很常见的.比如同一个分布式系统中的多个节点,同时访问一个共享资源.数据库.分布式存储 3.无序 ...