search
HomeJavajavaTutorialDetailed code explanation of how to use List
Detailed code explanation of how to use ListMay 12, 2017 am 09:29 AM
javalist

The principle of variable arrays in Java is to continuously create new arrays and add the original array to the new array. The usage of Java List is explained in detail below. Friends who need it can refer to it

The principle of variable arrays in Java is to continuously create new arrays and add the original array to the new array. The usage of Java List is explained in detail below.

  •  List: The elements are in order (you can take them out how you save them, the order will not be messed up), and the elements can be repeated (there is a 3 on the subscript 1 and a subscript 2 There can also be a 3) because the collection system has an index

  • ## ArrayList: The underlying data structure uses an array structure (the array length is variable and can be extended by 50% ) (Characteristics are that queries are fast, but additions and deletions are slow) Threads are not synchronized

  •  LinkedList: The underlying data structure is a linked list structure (Characteristics are that queries are slow, additions and deletions are fast)

  •  Vector: The bottom layer is an array data structure thread synchronization (the array length is variable and 100% extended) (no matter query or addition or deletion is very slow, replaced by ArrayList)

List: a unique method. All methods that can operate the corner mark are unique to the system.

boolean add(int index, E element)
boolean addAll(index,Collection)
public static void List_add(){
   ArrayList a1 = new ArrayList();
   a1.add("java");
   a1.add("php");//List集合中的元素可以重复
  a1.add(".net");
   System.out.println("原集合:"+a1);
   a1.add(1, "Flash");
   a1.add(0, "ps");  
   System.out.println(a1);
  ArrayList a2 = new ArrayList();
  a2.add("javascript");
  a2.add("3dMax");
  a2.add("IBM");
  a1.addAll(0, a2);
  System.out.println(a1);
}

Delete the element at the specified position

boolean remove(int index)
public static void List_remove(){
   ArrayList a1 = new ArrayList();
   a1.add("javascript");
   a1.add("php");
   a1.add("flash");
   System.out.println("原集合:"+a1);
   a1.remove(0);
   System.out.println(a1);
}

Modify the element with the specified index set(int index, E element) Returns the modified element

public static void List_set() {
   ArrayList a1 = new ArrayList();
   a1.add("javascript");
   a1.add("php");
   a1.add(".net");
   System.out.println("原集合:"+a1);
   a1.set(1, "falsh");
   System.out.println(a1);
}

Get

get(int index) Returns the element at the specified position in the list

subList(int fromIndex, int toIndex) Returns some elements between the specified fromIndex (inclusive) and toIndex (exclusive) in the list.

public static void List_get() {
   ArrayList a1 = new ArrayList();
   a1.add("java");
   a1.add("php");
   a1.add("flash");
System.out.println(a1.get(0));//获取指定角标的元素,有了该方法就可以遍历该集合中的所有元素
   System.out.println(a1.subList(1, 3));//获取集合中某一部分的元素,包含头不包含尾
}

Iterator unique to List collection: ListIterator (is a sub-interface of Iterator

)

Note:

When iterating, you cannot operate the elements in the collection through the methods of the collection

Object because a ConcurrentModificationException (concurrency exception) will occur. Therefore, when using an iterator, you can only use the iterator method. Because the Iterator method is limited, the element can only be judged, taken out, and deleted. If you want other operations such as adding, modifying, etc., you need to use its sub-interface, ListIterator. This interface can only be used through the listIterator of the List collection. Method to get

public class ListIteratorDemo {
   public static void main(String[] args) {
     ArrayList a1 = new ArrayList();
     a1.add("java01");
     a1.add("java02");
     a1.add("java03");
     a1.add("java04");
     System.out.println("原集合是:"+a1);
   /*在迭代过程中准备添加或者删除元素
   Iterator it = al.iterator();
   while (it.hasNext()){
     Object obj = it.next();
     if (obj.equals("java02"))
     //al.add("java008");//会出现并发异常,因为迭代器正在操作集合,不能再用集合的方法操作集合了
     it.remove();//将java02的引用从集合中删除了
     System.out.println("obj:"+obj);
    }
    */  
  //只有List的listIterator有增,删,改,查这些功能,因为只有List有索引
    ListIterator li = a1.listIterator();
     while (li.hasNext()){
     if(li.next().equals("java02"))
     //li.add("java009");
     li.set("java006");
    }  
  }
}

Vector: Enumeration is a unique way to extract Vector, which is very similar to iterator (in fact, enumeration and iteration are the same) and has been replaced by iterator

 public class VectorDemo {
   public static void main(String[] args) {
     Vector v = new Vector();
     v.add("java01");
     v.add("java02");
     v.add("java03");
     v.add("java04");
     for(Enumeration en = v.elements();en.hasMoreElements();){
      System.out.println(en.nextElement());
    }
  }
}

LinkedList:

Special methods:

addFirst(); Add elements at the head

addLast(); Add elements at the tail

getFirst(); getLast() ;

Get elements but do not delete elements. If there are no elements in the collection, NoSuchElementException will occur

removeFirst(); removeLast();

Get the element but delete the element. If there are no elements in the collection, NoSuchElementException will occur

An alternative method appeared in JDK1.6

offerFirst(); offerLast();
peekFirst(); peekLast(); 获取元素,但是元素不被删除。如果集合中没有元素,会返回null
pollFirst(); pollLast(); 获取元素,但是元素被删除。如果集合中没有元素,会返回null
 public class LinkedListDemo {
   public static void main(String[] args) {
     LinkedList link = new LinkedList();
     link.add("java01");
     link.add("java02");
     link.add("java03");
     link.add("java04");
     while(!link.isEmpty()){
      System.out.println((link.removeLast()));
    }
  }
}

[Related recommendations]

1.

Java Free Video Tutorial

2.

YMP Online Manual

3.

JAVA Elementary Introduction Video Tutorial

The above is the detailed content of Detailed code explanation of how to use List. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.