ArrayList and List relationships and code examples-Java

Source: Internet
Author: User
Tags iterable

Relationship

List is Java Interface, ArrayList is Java Class, and they all belong to the Java.util package.

The Java List is an ordered set (ordered collection), also known as a sequence (Sequence);

Java ArrayList is a variable-sized array implementation of the Java List Interface.

Both of these are in the JAVA collection framework location, as shown in:

Take a look at the Official document description:

Java.util

Interface list<e>

    • Type Parameters:

E-the type of elements in this list

All superinterfaces:

Collection<e>, iterable<e>

All known implementing Classes:

Abstractlist, Abstractsequentiallist, ArrayList, AttributeList, Copyonwritearraylist, LinkedList, RoleList, Roleunresolvedlist, Stack, Vector

public interface list<e>

Extends collection<e>

An ordered collection (also known as a sequence).

Java.util

Class arraylist<e>

    • Java.lang.Object
      • Java.util.abstractcollection<e>
        • Java.util.abstractlist<e>
          • Java.util.arraylist<e>

All implemented Interfaces:

Serializable, Cloneable, Iterable<e>, Collection<e>, List<e>, randomaccess

Direct known subclasses:

AttributeList, Rolelist, roleunresolvedlist

public class Arraylist<e>

Extends abstractlist<e>

Implements List<e>, Randomaccess, Cloneable, Serializable

Resizable-array implementation of the List interface.

Example

1. Create a new ArrayList object

New Arraylist<string> ();

2. Adding elements to them

Brands.add ("Jack Amber");

3. Complete Example

//Beerexpert.java, excerpted from Dawn Griffiths & David Griffiths, "Head first Android development",Importjava.util.ArrayList;Importjava.util.List; Public classBeerexpert {List<String>getbrands (String color) {List<String> brands =NewArraylist<string>(); if(Color.equals ("Amber") {Brands.add ("Jack Amber."); Brands.add ("Red Moose"); } Else{Brands.add ("Jail Pale Ale"); Brands.add ("Gout Stout"); }                returnbrands; }}

References

[1] Oracle Corporation. Class arraylist<e>-Java? Platform Standard Ed. 8 [OL]. Https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

[2] Oracle Corporation. Interface list<e>-Java? Platform Standard Ed. 8 [OL]. Https://docs.oracle.com/javase/8/docs/api/java/util/List.html

[3] Stack Overflow Users. Type List vs type ArrayList in Java [OL]. Https://stackoverflow.com/questions/2279030/type-list-vs-type-arraylist-in-java

ArrayList and List relationships and code examples-Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.