search
HomeJavajavaTutorialIntroduction to methods of List collection and its implementation class in Java (with code)

This article brings you an introduction to the methods of List collection and its implementation class in Java (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

List collection_List interface introduction

Features

1). Ordered;

2). Can store duplicates Element;

3). Can be accessed through index;

List<String> list = new ArrayList<>();
       list.add("张无忌");
       list.add("张三丰");
       list.add("章子怡");
       list.add("章子怡");//OK的,可以添加
         for(String s : list){
           System.out.println(s);//有序的
      }

Method

Inherits all methods of Collection interface, and has many of its own methods

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

Commonly used methods (the following methods are unique to the List interface)

1). Add: public void add(int index,E e): Add e to the current collection index position.

2). Delete: public E remove(int index): Delete the element at the index position and return the deleted element.

3). Change: public E set(int index,E element): Replace element with the element at the index position and return the element at the original index position.

4). Check: public E get(int index): Get the element at the index position.

Sample code:

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);
}

Commonly used classes that implement List interface_ArrayList

1). Commonly used classes that implement List interface

2).ArrayList

Features:

Fast query----using index

Slow addition and deletion---Need to expand, move Bit

Illustration:

Method:

No unique method

Case

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

Features

Use linked list to achieve

Fast addition and deletion, slow query

Illustration

Methods

Added some new methods to simulate stacks and queues:

1).public void push(Object o): Pushing the stack is equivalent to addFirst(E e), adding the specified element to the beginning of this collection
2).public E pop(): Pop the stack - if there is no element, an exception will be thrown;

public E poll(): Pop the stack - if there is no element, null will be returned [recommended]

Case:

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());
        }
    }
}

The above is the detailed content of Introduction to methods of List collection and its implementation class in Java (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software