Arraylist source code interpretation and application examples

Source: Internet
Author: User
An arraylist source code explanation

When a new arraylist is created using the non-argument constructor, an array of the object type with a length of 10 is created at the underlying layer of arraylist. In subsequent addition, if the number of elements added exceeds the current number of arrays, the underlying arraylist will use Java. util. the static method of the array class copyof () copies the current array to a new array. The length is 1.5 times the original array plus 1, and then the data is added to the new array. The detailed explanation is as follows:

① Arraylist list = new arraylist (); implementation process

First, call the construction method without parameters, as shown below:

Public arraylist (){

This (10 );

}

Then the construction method without parameters calls the construction method with parameters and passes in 10 (the purpose is to specify the size of the initialization Array)

Public arraylist (INT initialcapacity ){

Super ();

If (initialcapacity <0)

Throw new illegalargumentexception ("illegal capacity:" +

Initialcapacity );

This. elementdata = new object [initialcapacity];

}

② Implementation process of list. Add ()

First, call the add method.

Public Boolean add (E ){

Ensurecapacityinternal (size + 1); // increments modcount !!

Elementdata [size ++] = E;

Return true;

}

Call the ensurecapacityinternal method in the add method (confirm internal capacity)

Private void ensurecapacityinternal (INT mincapacity ){

Modcount ++;

// Overflow-conscious code

If (mincapacity-elementdata. length> 0)

Grow (mincapacity );

}

Call grow in the ensurecapacityinternal Method

Private void grow (INT mincapacity ){

// Overflow-conscious code

Int oldcapacity = elementdata. length;

Int newcapacity = oldcapacity + (oldcapacity> 1 );

If (newcapacity-mincapacity <0)

Newcapacity = mincapacity;

If (newcapacity-max_array_size> 0)

Newcapacity = hugecapacity (mincapacity );

// Mincapacity is usually close to size, so this is a win:

Elementdata = arrays. copyof (elementdata, newcapacity );

}

2. arraylist application example

In the online bookstore web application (JDBC + struts2 architecture), the function is to list all books. Arraylist is used to complete this function. The procedure is as follows:

1. Use SQL statements to find all books from the books table

2. Place the books to the objects of the books.

3. Loop the book object into the arraylist

4. Add arraylist to the built-in request object.

5. traverse the arraylist in the request on the JSP page to retrieve the data.

Notes about three arraylist

1. Possible type conversion errors

Arraylist accepts an object, so it can accept any object, but pay attention to type conversion when obtaining the object.

Otherwise, an exception occurs in the conversion of the Java. Lang. classcastexception type. For example:

List list = new arraylist ();

List. Add ("hello ");

List. Add (New INTEGER (2 ));

String STR = (string) list. Get (0 );

String STR = (string) list. Get (0); // an error will be reported during running.

2. Native data types cannot be stored

Arraylist accepts objects of the object type. Native data types such as int are not allowed. To put int type, the packaging type must be used. Example:

Function: adds integers in the list.

Int result = 0;

List list = new arraylist ();

List. Add (New INTEGER (1 ));

List. Add (New INTEGER (2 ));

List. Add (New INTEGER (3 ));

For (INT I = 0; I ++; I <list. Size () // list has a size () method that returns the number of objects in the list.

{

Result + = (integer) list. Get (I). intvalue ();

}

3. toarray Method

You can convert an arraylist object to an array of the object type.

Code: object [] In = List. toarray ();

Note: you cannot directly convert an array of the object type to an array of the integer type.

4. tostring method of arraylist

The tostring method of the contained object is called. The example is as follows:

Arraylist list = new arraylist ();

List. Add (new point (2, 3 ));

List. Add (new point (2, 3 ));

List. Add (new point (2, 3 ));

For (INT I = 0; I <list. Size (); I ++)

{

System. Out. println (list. Get (I ));

}

System. Out. println (list );

Private Class Point

{

Int X;

Int y;

Public point (int x, int y)

{

This. x = X;

Thix. Y = y;

}

Public String tostring ()

{

System. Out. println ("This. x =" + x + ", this. Y =" + y );

}

}

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.