Java------list collection common method

Source: Internet
Author: User
Tags addall

The elements in the list are ordered, and the elements can be duplicated because the collection has an index.

Method Summary
 boolean add(E e)
Adds the specified element to the end of the list (optional action).
 void add(int index, E element)
Inserts the specified element (optional action) at the specified position in the list.
 boolean addAll(Collection<? extendsE> c)
Adds all the elements in the specified collection to the end of this list, in order to specify the order in which the collection iterators return these elements (optional action).
 boolean addAll(int index, Collection<? extends E> c)
Inserts all the elements in the specified collection into the specified position in the list (optional action).
 void clear()
Removes all elements from the list (optional action).
 boolean contains(Object o)
Returns trueif the list contains the specified element.
 boolean containsAll(Collection<?> c)
Returns trueif the list contains all the elements of the specified collection.
 boolean equals(Object o)
Compares whether the specified object is equal to the list.
 E get(int index)
Returns the element at the specified position in the list.
 int hashCode()
Returns the hash code value for the list.
 int indexOf(Object o)
Returns the index of the specified element that first appears in this list, or 1 if the list does not contain the element.
 boolean isEmpty()
Returns trueif the list does not contain elements.
 Iterator<E> iterator()
Returns an iterator that iterates over the elements of a list in the appropriate order.
 int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or 1 if the list does not contain this element.
 ListIterator<E> listIterator()
Returns the list iterator for this list element, in the appropriate order.
 ListIterator<E> listIterator(int index)
Returns the list iterator (in the appropriate order) of the elements in the list, starting at the specified position in the list.
 E remove(int index)
Removes the element from the specified position in the list (optional action).
 boolean remove(Object o)
Removes the first occurrence of the specified element (if one exists) from this list (optional action).
 boolean removeAll(Collection<?> c)
Removes all of its elements contained in the specified collection from the list (optional action).
 boolean retainAll(Collection<?> c)
Retains only the elements contained in the specified collection in the list (optional actions).
 E set(int index, E element)
Replaces the element in the specified position in the list with the specified element (optional action).
 int size()
Returns the number of elements in the list.
 List<E> subList(int fromIndex, int toIndex)
Returns a partial view between the specified FromIndex(inclusive) and Toindex(not included) in the list.
 Object[] toArray()
Returns an array that contains all the elements in the list in the appropriate order, from the first element to the last element.
<T> T[]
toArray(T[] a)
Returns an array of all the elements in the list in the appropriate order (from the first element to the last element); The run-time type of the returned array is the run-time type of the specified array.



Import Java.util.arraylist;import java.util.iterator;import java.util.list;import java.util.listiterator;/** * * List in the unique method: All can operate the angle of the method is * * Add (index,element); AddAll (index,collection); By deleting remove (index); Change set * (Index,element); Check get (index); Sublist (from,to); Listiterator (); * * @author Caesar * * List Collection-specific iterator, Listiterator is a sub-interface of iterator in the iteration, you can not manipulate the elements in the collection by the method of the collection Object * can only judge the elements, take out, delete In addition to the operation if you want other operations, such as add, modify, etc., you need to use the period sub-interface Listiterator * This interface can only be obtained through the Listiterator method of the list collection. */public class Listdemo {public static void main (string[] args) {//TODO auto-generated method stub//method_1 (); Method_2 ( );} public static void Method_2 () {//Demo list iterator ArrayList al = new ArrayList (); Al.add (""); Al.add ("03");/* *//in Iteration The process is ready to add or remove elements Iterator it = Al.iterator (); The iterator operates while * (It.hasnext ()) {Object object = It.next (), if (Object.Equals (")") * {//al.add ("05");//concurrent access may create a security risk and cannot The same set of elements perform different operations at the same time it.remove * ();//The 02 in the collection container is also deleted, and the 02 reference is removed from the collection//iterator operation without adding an action. } SOP (It.next ()); } */listiterator li = Al.listiterator (), while (Li.hasnext ()) {Object object = Li.next (), if (Object.Equals ("") ") {Li.add ( "08");}} SOP (AL), SOP (Li.hasnext ()), SOP (Li.hasprevious ()),//Reverse traversal while (li.hasprevious ()) {SOP (li.previous ());}} public static void Method_1 () {ArrayList Al = new ArrayList () Al.add ("n"); Al.add (""), Al.add ("the"); SOP ("Original set is:" + al); /add Element Al.add (1, "the") in the specified position, SOP (AL),//delete element Al.remove (2), SOP (AL), modify element al.set (0, "00000005"), SOP (AL),//Get element sop via corner Mark (" Get 2 = "+ Al.get (2));//Get all elements for (int x = 0; x < Al.size (); X + +) {System.out.println (Al.get (x));} Iterator it = Al.iterator (); while (It.hasnext ()) {SOP (It.next ());} Get the position of the object via IndexOf sop ("index =" + al.indexof ("03")); List sub = al.sublist (1, 3); SOP (sub);} public static void Sop (Object obj) {System.out.println (obj);}}


Java------list collection common method

Related Article

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.