ArrayList usage in Java

Source: Internet
Author: User

ArrayList usage in Java

2011-07-20 15:02:03|  Category: computer majors | Tags:java arraylist usage | Report | Font size Subscription

In the course of Java Learning, we use ArrayList to find the learning materials on the Internet. Excerpt from: http://www.cnblogs.com/skylaugh/archive/2006/09/15/505346.html

The System.Collections.ArrayList class is a special array. By adding and removing elements, you can dynamically change the length of the array.

A Advantages

1. Support Automatic size change function
2. Flexibility to insert elements
3. Ability to remove elements flexibly

Two Limitations

It's a little bit faster than a normal array.

Three adding elements

1. Publicvirtualintadd (Objectvalue);

Add an object to the end of the ArrayList

ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Content is ABCDE

2. Publicvirtualvoidinsert (Intindex,objectvalue);

Inserts an element at the specified index of the ArrayList

ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.insert (0, "AA");

The result is AAABCDE

3. Publicvirtualvoidinsertrange (Intindex,icollectionc);

Inserts an element from the collection at the specified index of ArrayList

ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
ArrayList List2 = Newarraylist ();
List2. ADD ("tt");
List2. ADD ("TTT");
Alist.insertrange (2,LIST2);

The result is ABTTTTTCDE

Four Delete

1. Publicvirtualvoidremove (objectobj);

Remove the first occurrence of a specific object from ArrayList, note that the first

ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.remove ("a");

The result is BCDE

2. Publicvirtualvoidremoveat (Intindex);

Removes the element at the specified index of the ArrayList

Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.removeat (0);

The result is BCDE

3. Publicvirtualvoidremoverange (Intindex,intcount);

Removes a range of elements from the ArrayList. Index, which represents the number that starts at the index

Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.removerange (1,3);

Results for AE

4. Publicvirtualvoidclear ();

Removes all elements from the ArrayList.

Five Sort

A) publicvirtualvoidsort ();

Sorts the elements in a ArrayList or part of it.

Arraylistalist=newarraylist ();
Alist.add ("E");
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Dropdownlist1.datasource=alist;//dropdownlistdropdownlist1;
Dropdownlist1.databind ();

The result is EABCD

Arraylistalist=newarraylist ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.sort ();//Sort
Dropdownlist1.datasource=alist;//dropdownlistdropdownlist1;
Dropdownlist1.databind ();

The result is ABCDE

b) Publicvirtualvoidreverse ();

Reverses the order of the elements in the ArrayList or part of it.

Arraylistalist=newarraylist ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.reverse ();//Invert
Dropdownlist1.datasource=alist;//dropdownlistdropdownlist1;
Dropdownlist1.databind ();
The result is EDCBA

Six Find

A) Publicvirtualintindexof (object);
b) Publicvirtualintindexof (object,int);
c) Publicvirtualintindexof (Object,int,int);

Returns the zero-based index of the first occurrence of a value in ArrayList or part of it. No return-1 found.

ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Intnindex=alist.indexof ("a");//1
Nindex=alist.indexof ("P");//Not Found,-1
d) publicvirtualintlastindexof (object);
e) Publicvirtualintlastindexof (object,int);
f) Publicvirtualintlastindexof (Object,int,int);

Returns the zero-based index of the last occurrence of a value in the ArrayList or part of it.

ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("a");//With 0
Alist.add ("D");
Alist.add ("E");
Intnindex=alist.lastindexof ("a");//value is 2 instead of 0

g) Publicvirtualboolcontains (Objectitem);

Determines whether an element is in ArrayList. Contains return true, otherwise returns false

Seven Other

1. Publicvirtualintcapacity{get;set;}

Gets or sets the number of elements that ArrayList can contain.

2. Publicvirtualintcount{get;}

Gets the number of elements actually contained in the ArrayList.
Capacity is the number of elements that ArrayList can store. Count is the number of elements actually contained in ArrayList. Capacity is always greater than or equal to count. If count exceeds capacity when the element is added, the capacity of the list is doubled by automatically reallocating the internal array.
If the value of capacity is explicitly set, the internal array also needs to be reassigned to accommodate the specified capacity. If capacity is explicitly set to 0, the common language runtime sets it to the default capacity. The default capacity is 16.
When clear is called, Count is 0, and at this point capacity is the default capacity of 16 instead of 0

3. Publicvirtualvoidtrimtosize ();

Sets the capacity to the actual number of elements in ArrayList.
If you do not add new elements to the list, this method can be used to minimize the memory overhead of the list.
To completely clear all the elements in the list, call the Clear method before you call TrimToSize. Truncating the empty ArrayList will set the ArrayList capacity to the default capacity, not zero.

ArrayList alist = new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");//count=5,capacity=16,
Alist.trimtosize ();//count=capacity=5;

ArrayList usage in Java

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.