
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How does a list work in Java?
public interface List<E> extends Collection<E>
The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements.
Elements can be inserted or accessed by their position in the list, using a zero-based index.
A list may contain duplicate elements.
In addition to the methods defined by Collection, List defines some of its own, which are summarized in the following table.
Several of the list methods will throw an UnsupportedOperationException if the collection cannot be modified, and a ClassCastException is generated when one object is incompatible with another.
Example
The above interface has been implemented in various classes like ArrayList or LinkedList, etc. Following is the example to explain few methods from various class implementation of the above collection methods −
package com.tutorialspoint; import java.util.*; public class CollectionsDemo { public static void main(String[] args) { List<String> a1 = new ArrayList<>(); a1.add("Zara"); a1.add("Mahnaz"); a1.add("Ayan"); System.out.println("ArrayList: " + a1); List<String> l1 = new LinkedList<>(); l1.add("Zara"); l1.add("Mahnaz"); l1.add("Ayan"); l1.remove(1); System.out.println("LinkedList: " + l1); } }
Output
This will produce the following result −
ArrayList: [Zara, Mahnaz, Ayan] LinkedList: [Zara, Ayan]
- Related Articles
- How does a ClassLoader work in Java?\n
- How does * operator work on list in Python?
- How does in operator work on list in Python?
- How does concatenation operator work on list in Python?
- How does repetition operator work on list in Python?
- How does del operator work on list in Python?
- How does the Java “foreach” loop work?
- How does a kaleidoscope work?
- How does a Lock work?
- How does a vaccine work?
- How does a vector work in C++?
- How does a Lens antenna work?
- How does a packet filters work?
- How does a Python interpreter work?
- How does a vector work in C/C++
