Common methods of ArrayList available in the Java API

Source: Internet
Author: User
Tags addall object object

Sort out some of the common methods of ArrayList according to Java1.6 's API.

Three constructors

1.public ArrayList (int initialcapacity);

Constructs an empty list with the specified initial capacity

2.PUBILC ArrayList ();

Constructs an empty list with an initial capacity of 10

3.public ArrayList (collection<> c)

Constructs a list of elements that contain the specified collection.

If collection is null, it throws NullPointerException

Other common methods

4.trimToSize

public void TrimToSize ();

Adjusts the capacity of this ArrayList to the current size of the list. Applications can use this action to minimize the storage of ArrayList instances.

5.size

public int size ();

Returns the number of elements in this list.

6.isEmpty

public boolean isEmpty ();

Returns true if there are no elements in this list

7.contains

Public Boolean contains (Object O);

Returns true if the specified element is contained in this list

8.indexOf

public int indexOf (Object o)

Returns the index of the specified element that first appeared in this list, or 1 if the list does not contain elements.

9.lastIndexOf

public int lastIndexOf (Object o)

Returns the index of the specified element that occurred last in this list, or 1 if the list does not contain an index.

10.toArray

Public object[] ToArray ();

Returns an array containing all the elements in this list, in the appropriate order (from the first to the last element).

Because this list does not maintain any references to the returned array, it will be "safe". (In other words, this method must be assigned a new array). Therefore, the caller is free to modify the returned array.

This approach serves as a bridge between the array-based API and the collection-based API.

11.get

Public E get (int index);

Returns the element at the specified position in this list

12.set

Public E Set (int index, e element);

Replaces the element at the specified position in this list with the specified element. The return value is the element that was previously at that specified position

13.add

Public boolean Add (E element);

Adds the specified element to the tail of this list. Add successful return True

14.add

public void Add (int index, E Element)

Inserts the specified element into the specified position in this list. Moves the element that is currently at that position (if any) and all subsequent elements (indexed by 1) to the right.

15.remove

Public E Remove (int index)

Removes the element from the specified position in this list, returning the element removed from the list

16.remove

public boolean remove (Object o)

Removes the specified element (if any) that first appears in this list. If the list does not contain this element, the list does not change.

17.clear

public void Clear ()

Removes all the elements from this list. When this call returns, the list will be empty

18.addAll

public boolean AddAll (Collection c)

Adds all elements in the collection to the end of this list, according to the order of elements returned by the iterator that specifies collection

19.addAll

public boolean addall (int index, Collection c)

Inserts all elements from the specified collection into this list, starting at the specified location.

20.removeRange

protected void RemoveRange (int fromIndex, int endIndex);

Removes all elements in the list between FromIndex(inclusive) and Toindex(not included). Moves all subsequent elements to the left (decreasing their index). This call shortens the list of (Toindex-fromindex) elements. (If toindex==fromindex, this operation is not valid.) )

Package Collection;import Java.util.arraylist;import Java.util.list;import Java.util.random;public class arraylisttest {public static random random = new random ();p ublic static char[] Chararray = {};p ublic static void Main (Stri ng [] args) {list<string> list = new arraylist<string> ();//Add 10 random strings to list for (int i = 0;i < i++) {List.ad D (arraylisttest.genrandomstring (5));} Insert a fixed string list.add ("abc") into the list,//1. To determine whether ArrayList is an empty Boolean isEmpty = List.isEmpty (); System.out.println ("ArrayList is empty:" + isEmpty); Returns the number of elements in the list int size = List.size (); System.out.println ("The number of elements in the ArrayList is:" + size);//3. Determines whether the ArrayList contains the specified element, Boolean isexist = List.contains ("abc"); System.out.println ("ArrayList contains ABC:" + isexist); Returns the position of the first occurrence of the element in ArrayList, if not, returns -1int index = List.indexof ("ABC "); System.out.println ("The first occurrence of ABC in ArrayList is" + index ");//5 returns the position of the last occurrence of the element in the ArrayList, if not, returns -1int LastIndex = List.lastindexof ("abc"); System.out.println ("ArrayList last occurrence of ABC is" + LastIndex); Returns an array containing all the elements in this list (in order), equivalent to array AThe Pi and collection API bridge, returns an array of object object[] Objectarray = List.toarray (); System.out.println ("generated array is"); for (Object Obj:objectarray) {System.out.println (obj);} 7. Returns the element at the specified position in the list and throws indexoutofboundsexceptionstring s = list.get (0) If the return is exceeded; System.out.println ("The first element in the list is:" + s);//8. Replaces the element at the specified position in this list with the specified element, the return value is the element that was previously located on that bit, string old = List.set (0, "abc"); System.out.println ("first element" + old + "was replaced with ABC");//9. Inserts an element into the tail of the list System.out.println ("Insert succeeded:" + list.add ("def");//10. Inserts an element into the specified position, the return type is Voidlist.add (0, "Ghi"),//11. Removes the element System.out.println ("element" + list.remove (0) + "removed") from the specified position;//12. Removes the first occurrence of the specified element in this list (if present) System.out.println ("The element is removed:" + list.remove ("abc"));//13. Follow the order of elements returned by the specified collection iterator, and the All elements in collection are added to the tail of this list list<string> list2 = new arraylist<string> (); List2.add ( Arraylisttest.genrandomstring (5)); List2.add (Arraylisttest.genrandomstring (5)); List.addall (LIST2); SYSTEM.OUT.PRINTLN ("New list is:"); for (String str:list) {System.out.println (str);} 14. Remove all the elements from the list list.clear (); System.out.println ("The length of the list after removing all elements is:"+ list.size ());} Generates a random string of the specified length public static string genrandomstring (int length) {Chararray = " Abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz ". ToCharArray (); char[] RandomChar = new char [length] ; for (int i = 0; i < randomchar.length; i++) {Randomchar[i] = Chararray[random.nextint (61)];} return new String (Randomchar);}}


Common methods of ArrayList available in the Java API

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.