Detailed code explanation of how to use List
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)
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); }
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); }
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));//获取集合中某一部分的元素,包含头不包含尾 }
Note:
When iterating, you cannot operate the elements in the collection through the methods of the collectionpublic 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"); } } }
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()); } } }
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())); } } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
