[Java class set] _ list interface notes (instance test)

Source: Internet
Author: User

[Java class set] _ list interface notes

Objectives of this chapter:
Understanding the relationship between the list interface and the collection Interface
Common subclasses of the List interface: arraylist and vector
Measure the test taker's knowledge about the differences between arraylist and vector classes.

3. Details

List is a sub-interface of collection. The list interface can store any data, and the content of the list interface can be repeated.
The list interface is much more powerful than the collection interface, because it greatly expands the operations of the collection interface.

Method to expand the list interface:

No. method type description
1 Public void add (INT index, e element) add elements at the specified position
2 Public Boolean addall (INT index, collection <? Extends e?> C) Add a group of elements at a specified position
3 E get (INT index) returns the elements at the specified position
4 Public int indexof (Object O) common search for the location of a specified Element
5 Public int lastindexof (Object O): searches for the position of the specified element from the forward.
6 Public listiterator <E> listiterator () is instantiated by the listiterator interface.
7. Public E remove (in index): delete an element at a specified position.
8 public list <E> sublist (INT fromindex, int toindex)
9 public e set (INT index, e element) replaces the elements at the specified position

A common subclass of the List interface-arraylist

Arraylist is a list subclass that can be instantiated for the list interface directly through object polymorphism.
This class is defined as follows:

Public class arraylist <E> extends actlist <E> implements list <E>, randomaccess, cloneable, serializable

The abstractlist class is defined as follows:

Public abstract class implements actlist <E> extends abstractcollection <E> implements list <E>

If you want to use an interface, you must use the object polymorphism for the instantiation operation, so the list interface itself is the same.
Arraylist is the most common subclass in list.

import java.util.ArrayList;import java.util.List;public class ArrayListDemo01{    public static void main(String args[]){        List<String> allList = null;        allList = new ArrayList<String>();        allList.add("Hello");        allList.add("World");        System.out.println(allList);    }}

Output: [Hello, world]
Add a specified position by using the list interface extension method.

import java.util.ArrayList;import java.util.List;import java.util.Collection;public class ArrayListDemo01{    public static void main(String[] args){        List<String> allList = null;        Collection<String> allCollection = null;        allList = new ArrayList<String>();        allCollection = new ArrayList<String>();        allList.add("Hello");        allList.add(0,"World");        System.out.println(allList);        allCollection.add("LXH");        allCollection.add("www.mldn.cn");        allList.addAll(allCollection);        System.out.println(allList);    }}

Output:
[World, hello]
[World, hello, lxh, www.mldn.cn]

Add a group of data to a specified location

import java.util.ArrayList;import java.util.List;import java.util.Collection;public class ArrayListDemo01{    public static void main(String[] args){        List<String> allList = null;        Collection<String> allCollection = null;        allList = new ArrayList<String>();        allCollection = new ArrayList<String>();        allList.add("Hello");        allList.add(0,"World");        System.out.println(allList);        allCollection.add("LXH");        allCollection.add("www.mldn.cn");        allList.addAll(allCollection);        allList.addAll(0,allCollection);        System.out.println(allList);    }}

Output:
[World, hello]
[Lxh, www.mldn.cn, world, hello, lxh, www.mldn.cn]

Since data can be added, data can be deleted.
There are two types of deletion in list: Object content, Object ID

import java.util.ArrayList;import java.util.List;public class ArrayListDemo02{    public static void main(String args[]){        List<String> allList = null;        allList = new ArrayList<String>();        allList.add("Hello");        allList.add(0,"World");        allList.add("MLDN");        allList.add("www.mldn.cn");        allList.remove(0);        allList.remove("MLDN");        System.out.println(allList);    }}

Output:
[Hello, www.mldn.cn]

The content in the set can be added or deleted, so the most important thing is the output.

The get () method is provided in the list interface, which can be used to complete the output.
Output is completed by loop, and the number of loops is obtained by the size () method.

Import Java. util. arraylist; import Java. util. list; public class arraylistdemo03 {public static void main (string [] ARGs) {list <string> alllist = NULL; alllist = new arraylist <string> (); alllist. add ("hello"); alllist. add ("world"); alllist. add (0, "hello"); alllist. add ("mldn"); system. out. println ("output from front to back"); For (INT I = 0; I <alllist. size (); I ++) {system. out. print (alllist. get (I);} system. out. println ("\ n from back to forward Output"); For (INT I = alllist. size ()-1; I> = 0; I --) {system. out. print (alllist. get (I ));}}}

Output:
Output from front to back
Hellohelloworldmldn
Forward and backward output
Mldnworldhellohello

This output method is exclusive to the list interface, while other interfaces do not, especially the operations in the collection that do not retrieve content based on the index.

Import Java. util. arraylist; import Java. util. list; public class arraylistdemo04 {public static void main (string ARGs []) {list <string> alllist = NULL; alllist = new arraylist <string> (); alllist. add ("hello"); alllist. add ("world! "); Alllist. add ("mldn"); string STR [] = alllist. toarray (New String [] {}); // specify the system type. out. println ("pre-specified data type"); For (INT I = 0; I <alllist. size (); I ++) {system. out. print (alllist. get (I);} system. out. println ("\ n returned object array type"); object OBJ [] = alllist. toarray (); For (INT I = 0; I <alllist. size (); I ++) {system. out. print (string) alllist. get (I); // downward Transformation Operation }}}

Output:

Pre-specified data type
Helloworld! Mldn
Returned object array type
Helloworld! Mldn

Since basic output functions have been completed, the following operations are available in the collection:
Judge whether the set is null: Boolean isempty ()
Part of the truncated collection: List <E> sublist (INT fromindex, int toindex), list interface Extension
Query whether the specified object exists: int indexof (Object O). If the specified object exists, the return position is returned; otherwise, the return position is-1.
Search for existence: Boolean contains (Object O)

Import Java. util. arraylist; import Java. util. list; public class arraylistdemo05 {public static void main (string ARGs []) {list <string> alllist = NULL; alllist = new arraylist <string> (); system. out. println ("before the set operation, determine whether the set is empty? "+ Alllist. isempty (); alllist. add ("hello"); alllist. add ("world"); alllist. add ("mldn"); alllist. add ("www.mldn.cn"); system. out. println (alllist. contains ("hello ")? "\" Hello \ "string exists! ":" \ "Hello \" the string does not exist! "); List <string> allsub = alllist. sublist (2, 3); // string truncation system. out. println ("set truncation:"); For (INT I = 0; I <allsub. size (); I ++) {system. out. println (allsub. get (I) + ",");} system. out. println ("Location of the mldn string:" + alllist. indexof ("mldn"); system. out. println ("is the set empty after the set operation? "+ Alllist. isempty ());}}

Output:
Before the set operation, check whether the set is empty? True
"Hello" string exists!
Set truncation:
Mldn,
Location of the mldn string: 2
Is the set empty after the set operation? False

The preceding operations are completed using the arraylist subclass.

Saved subclass Vector

Since most people are used to the Vector class, the Java designer has made the Vector class implement a list interface, which saves it. However, because it is a list subclass, there is no big difference in the use of arraylist for vector classes.

Vector Definition Format:
Public class vector <E> extends abstratlist <E> implements list <E>, randdomaccess, cloneable, serializable

However, although the Vector class is an old class, it is the same for the program because it implements the list interface.

import java.util.Vector;import java.util.List;public class VectorListDemo01{    public static void main(String args[]){        List<String> allList = null;        allList = new ArrayList<String>();        allList.add("Hello");        allList.add("World");        for(int i=0;i<allSub.size();i++){            System.out.println(allSub.get(i)+"、");                }    }}

Output:

Hello,
World,

Repeated methods are also provided in this class:
Public void addelement (e obj) This method is consistent with the add () method.

import java.util.Vector;import java.util.List;public class VectorListDemo02{    public static void main(String args[]){        Vector<String> allList = null;        allList = new Vector<String>();        allList.addElement("Hello");        allList.addElement("World");        for(int i=0;i<allList.size();i++){            System.out.println(allList.get(i)+"、");                }    }}

Subtype differences: differences between arraylist and vectorlist

No. comparison points: arraylist vectorlist
1 released after jdk1.2, which belongs to the new operation class jdk1.0 and belongs to the old operation class
2. The performance adopts asynchronous processing, and the performance is higher adopts synchronous processing, with low performance
3. thread security is a non-thread security operation and a thread security operation.
4. Only iterator and foreach outputs can be output using iterator, foreach, and enumeration.

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.