java_List集合及其实现类
第一章:List集合_List接口介绍
1).特点
1).有序的;
2).可以存储重复元素;
3).可以通过索引访问;
- List<String> list = new ArrayList<>();
- list.add("张无忌");
- list.add("张三丰");
- list.add("章子怡");
- list.add("章子怡");//OK的,可以添加
- for(String s : list){
- System.out.println(s);//有序的
- }
2).方法
继承了Collection接口的所有方法,并且又有很多自己的方法
- void add(String item)
- 向滚动列表的末尾添加指定的项。
- void add(String item, int index)
- 向滚动列表中索引指示的位置添加指定的项。
- void addActionListener(ActionListener l)
- 添加指定的动作侦听器以从此列表接收动作事件。
- void addItemListener(ItemListener l)
- 添加指定的项侦听器以接收此列表的项事件。
- void addNotify()
- 创建列表的同位体。
- void deselect(int index)
- 取消选择指定索引处的项。
- AccessibleContext
- getAccessibleContext()
- 获取与此 List 关联的 AccessibleContext。
- ActionListener[]
- getActionListeners()
- 返回已在此列表上注册的所有动作侦听器的数组。
- String
- getItem(int index)
- 获取与指定索引关联的项。
- int getItemCount()
- 获取列表中的项数。
- ItemListener[]
- getItemListeners()
- 返回已在此列表上注册的所有项侦听器的数组。
- String[]
- getItems()
- 获取列表中的项。
- <T extends EventListener>
- T[]
- getListeners(Class<T> listenerType)
- 返回目前已在此 List 上注册为 FooListener 的所有对象的数组。
- Dimension
- getMinimumSize()
- 确定此滚动列表的最小大小。
- Dimension
- getMinimumSize(int rows)
- 获取具有指定行数的列表的最少维数。
- Dimension
- getPreferredSize()
- 获取此滚动列表的首选大小。
- Dimension
- getPreferredSize(int rows)
- 获取具有指定行数的列表的首选维数。
- int getRows()
- 获取此列表中的可视行数。
- int getSelectedIndex()
- 获取列表中选中项的索引。
- int[] getSelectedIndexes()
- 获取列表中选中的索引。
- String
- getSelectedItem()
- 获取此滚动列表中选中的项。
- String[]
- getSelectedItems()
- 获取此滚动列表中选中的项。
- Object[]
- getSelectedObjects()
- 获取对象数组中此滚动列表的选中项。
- int getVisibleIndex()
- 获取上次由 makeVisible 方法使其可视的项的索引。
- boolean isIndexSelected(int index)
- 确定是否已选中此滚动列表中的指定项。
- boolean isMultipleMode()
- 确定此列表是否允许进行多项选择。
- void makeVisible(int index)
- 使指定索引处的项可视。
- protected String
- paramString()
- 返回表示此滚动列表状态的参数字符串。
- protected void processActionEvent(ActionEvent e)
- 处理发生在此列表上的动作事件,方法是将这些事件指派给所有已注册的 ActionListener 对象。
- protected void processEvent(AWTEvent e)
- 此滚动列表的进程事件。
- protected void processItemEvent(ItemEvent e)
- 处理发生在此列表上的项事件,方法是将这些事件指派给所有已注册的 ItemListener 对象。
- void remove(int position)
- 从此滚动列表中移除指定位置处的项。
- void remove(String item)
- 从列表中移除项的第一次出现。
- void removeActionListener(ActionListener l)
- 移除指定的动作侦听器,以便不再从此列表接收动作事件。
- void removeAll()
- 从此列表中移除所有项。
- void removeItemListener(ItemListener l)
- 移除指定的项侦听器,以便不再从此列表接收项事件。
- void removeNotify()
- 移除此列表的同位体。
- void replaceItem(String newValue, int index)
- 使用新字符串替换滚动列表中指定索引处的项。
- void select(int index)
- 选择滚动列表中指定索引处的项。
- void setMultipleMode(boolean b)
- 设置确定此列表是否允许进行多项选择的标志。
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位置上的元素。
示例代码:
- public static void main(String[] args) {
- //List集合中增加自己的add方法,add(int index,E e);
- List<String> list=new ArrayList<>();
- list.add("aaaa");
- list.add("bbbb");
- list.add("cccc");
- list.add(1,"dddd");
- System.out.println(list);
- //删除指定索引的元素,并将删除的元素返回
- String removeStr=list.remove(2);
- System.out.println(removeStr);
- System.out.println(list);
- //修改指定索引位置上的元素set(int index,E e),并将原index位置上的元素返回
- String setStr=list.set(2,"ffff");
- System.out.println(setStr);
- System.out.println(list);
- //通过索引获取指定索引上的元素
- String getStr=list.get(2);
- System.out.println("索引为3的元素为:"+getStr);
- System.out.println(list);
- }
第二章.实现List接口的常用类_ArrayList
1).list接口常用实现类
2).ArrayList
特点:
查询快----使用索引
增删慢---需要扩容,移位
图解:
方法:
无特有方法
案例
- public static void main(String[] args) {
- //List集合中增加自己的add方法,add(int index,E e);
- ArrayList<String> list=new ArrayList<>();
- list.add("aaaa");
- list.add("hhhh");
- list.add("cccc");
- list.add(1,"dddd");
- System.out.println(list);
- //删除指定索引的元素,并将删除的元素返回
- String removeStr=list.remove(2);
- System.out.println(removeStr);
- System.out.println(list);
- //修改指定索引位置上的元素set(int index,E e),并将原index位置上的元素返回
- String setStr=list.set(2,"ffff");
- System.out.println(setStr);
- System.out.println(list);
- //通过索引获取指定索引上的元素
- String getStr=list.get(2);
- System.out.println("索引为3的元素为:"+getStr);
- System.out.println(list);
- }
3).LinkedList
特点
使用链表实现
增删快,查询慢
图解
方法
新增了一些方法,可以模拟栈、队列:
1).public void push(Object o):压栈 等同于addFirst
(E e)
,将指定元素添加到此集合的开头
2).public
E pop():弹栈--如果没有元素,会抛异常;
public E poll():弹栈--如果没有元素,会返回null【建议使用】
案例:
- public class Demo {
- public static void main(String[] args) {
- LinkedList<String> list = new LinkedList<>();
- list.push("孙悟空");
- list.push("猪八戒");
- list.push("沙和尚");
- System.out.println(list);
- while (list.size() > 0) {
- System.out.println("弹出一个:" + list.poll());
- System.out.println("集合大小:" + list.size());
- }
- }
- }
java_List集合及其实现类的更多相关文章
- 操作集合的工具类:Collections
Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类提供了大量方法对集合进行排序.查询和修改等操作,还提供了将集合对象置为不可变.对集合对象实现同步控制等方法 ...
- JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)
package com.itcast.test20140113; import java.util.ArrayList; import java.util.Arrays; import java.ut ...
- 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析
1. HashSet与HashMap的联系与区别? 区别:前者是单列后者是双列,就是hashmap有键有值,hashset只有键: 联系:HashSet的底层就是HashMap,可以参考HashSe ...
- 操作集合的工具类Collections
1 操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操 ...
- Java集合概述、Set集合(HashSet类、LinkedHashSet类、TreeSet类、EnumSet类)
Java集合概述.Set集合(HashSet类.LinkedHashSet类.TreeSet类.EnumSet类) 1.Java集合概述1)数组可以保存多个对象,但数组长度不可变,一旦在初始化数组时指 ...
- JAVA中的集合容器操作类
目录 JAVA中的集合容器操作类 List集合 ArrayList的操作方法说明 LinkedList Stack Set Map Queue 总结 JAVA中的集合容器操作类 Java容器类库总共分 ...
- 转:C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、Sort)
C#常用的集合类型(ArrayList类.Stack类.Queue类.Hashtable类.Sort) .ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在Array ...
- 集合框架的类和接口均在java.util包中。 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换。
集合框架的类和接口均在java.util包中. 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换.
- Java中各种集合(字符串类)的线程安全性!!!
Java中各种集合(字符串类)的线程安全性!!! 一.概念: 线程安全:就是当多线程访问时,采用了加锁的机制:即当一个线程访问该类的某个数据时,会对这个数据进行保护,其他线程不能对其访问,直到该线程读 ...
随机推荐
- Python练手例子(9)
49.使用lambda来创建匿名函数. #python3.7 MAXIMUM = lambda x,y : (x > y) * x + (x < y) * y MINIMUM = lamb ...
- Python-字典与json的转换
#json是字符串,只不过长得像字典 import json user_info='''{"niuhy":1234,"shanbl":44566}''' #js ...
- Android OpenGL ES 开发(四): OpenGL ES 绘制形状
在上文中,我们使用OpenGL定义了能够被绘制出来的形状了,现在我们想绘制出来它们.使用OpenGLES 2.0来绘制形状会比你想象的需要更多的代码.因为OpenGL的API提供了大量的对渲染管线的控 ...
- 开源播放器 ijkplayer (六) :Android 下使用 ijkplayer 异常处理思路
一. java.lang.IllegalStateException: mpjni: setOptionLong: null mp 根据已查到的资料看,目前是ijk内部的问题,只能通过try& ...
- [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 ...
- [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 ...
- [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 ...
- python之zipfile
1 简述 zip文件是一个常用的归档和与压缩标准. zipfile模块提供了创建.读取.写入.添加及列出zip文件的工具. zipfile里有2个非常常用的class,分别是Zipfile和ZipIn ...
- python高级-迭代器(18)
一.什么是迭代器 迭代是访问集合元素的⼀种⽅式. 迭代器是⼀个可以记住遍历的位置的对象. 迭代器对象从集合的第⼀个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退 二.可迭代对象 直 ...
- 分布式_zookeeper
分布式协调服务-zookeeper 分布式环境的特点 1.分布性 2.并发性 程序运行过程中,并发性操作是很常见的.比如同一个分布式系统中的多个节点,同时访问一个共享资源.数据库.分布式存储 3.无序 ...