Java. util summary 2

Source: Internet
Author: User


Java. util summary 2
Qzg589 original (Participation score: 123781, expert score: 6302) published: Version: 1.0 read:1637Times

Small and smaller.
In the previous example, the default conversion provided by the tostring () method is used to display the content of the class set, tostring ()
Methods are inherited from abstractcollection. Although it is sufficient for a short example program, few
This method is used to display the content of the actual class set. Programmers usually provide their own output programs. But below
In the following examples, the default output created by the tostring () method will still be used.
Even if an object is stored in an arraylist object, its capacity increases automatically. You can still call
The ensurecapacity () method increases the arraylist capacity at the site. If you know in advance that
When you store a large number of items in a centralized manner, you may want to do so. At the beginning, increase its capacity at one time,
To avoid subsequent redistribution. Because redistribution takes a long time to avoid unnecessary processing to improve performance.
The features of the ensurecapacity () method are as follows:
Void ensurecapacity (INT cap)
Here, CAP is the new capacity.
Conversely, if you want to reduce the size of the array under the arraylist object so that it can accommodate the current item
You can call the trimtosize () method. This method is described as follows:
Void trimtosize ()
Obtain an array from the array list (arraylist)
When arraylist is used, you sometimes want to obtain an actual array, which contains the list content. Zheng
As explained above, you can call the toarray () method to implement it. Below are a few reasons why you might want
Reasons for converting a set into an array:
? For specific operations, you can get faster processing time.
? In order to pass an array to the method, the method does not need to overload to receive the class set.
? To integrate new programs based on the class set with old programs that do not know the class set.
For whatever reason, as shown in the following example, converting arraylist to an array is cumbersome.
// Convert an arraylist into an array.
ImportJava. util.*;
Class arraylisttoarray {
Public static void main (string ARGs []) {
// Create an array list
Arraylist Al = new arraylist ();
// Add elements to the array list
Al. Add (New INTEGER (1 ));
Al. Add (New INTEGER (2 ));
Al. Add (New INTEGER (3 ));
Al. Add (New INTEGER (4 ));
System. Out. println ("Contents of Al:" + Al );
// Get Array

Object Ia [] = Al. toarray ();
Int sum = 0;
// Sum the Array
For (INT I = 0; I <IA. length; I ++)
Sum + = (integer) IA [I]). intvalue ();
System. Out. println ("sum is:" + sum );
}
}
The program output is as follows:
Contents of Al: [1, 2, 3, 4]
Sum is: 10
Creates an integer class set at the beginning of the program. As explained above, since the original type cannot be stored
It is stored in the class set, so the type Integer object is created and saved. Next, the toarray () method is called.
Obtains an array of objects. The content of this array is set to integer. Then, evaluate these values.
And.
15.3.2 shortlist class
The listlist class extends abstractsequentiallist and executes the list interface. It provides a link list data knot
Structure. It has the following two constructors:
Partition list ()
Collections list (collection C)
The first constructor creates an empty link list. The second constructor creates a link list.
The list is initialized by elements in class set C.
In addition to its inherited methods, the javaslist class also defines some useful methods. These methods are mainly used
Operation and access list. The addfirst () method can be used to add elements to the list header. The addlast () method can be used in
Add an element at the end of the list. They are in the following format:
Void addfirst (Object OBJ)
Void addlast (Object OBJ)
Here, obj is the added item.
Call the getfirst () method to obtain the first element. Call the getlast () method to obtain the last element.
They are in the following format:
Object getfirst ()
Object getlast ()
To delete the first element, you can use the removefirst () method. to delete the last element, you can call
Use the removelast () method. They are in the following format:
Object removefirst ()
Object removelast ()

The following program illustrates several methods supported by listing.
// Demonstrate your list.
ImportJava. util.*;
Class program listdemo {
Public static void main (string ARGs []) {
// Create a linked list
Counter list LL = new counter list ();
// Add elements to the linked list
Ll. Add ("F ");
Ll. Add ("B ");
Ll. Add ("D ");
Ll. Add ("e ");
Ll. Add ("C ");
Ll. addlast ("Z ");
Ll. addfirst ("");
Ll. Add (1, "A2 ");
System. Out. println ("original contents of LL:" + LL );
// Remove elements from the linked list
Ll. Remove ("F ");
Ll. Remove (2 );
System. Out. println ("Contents of LL after deletion :"
+ LL );
// Remove first and last elements
Ll. removefirst ();
Ll. removelast ();
System. Out. println ("ll after deleting first and last :"
+ LL );
// Get and set a value
Object val = ll. Get (2 );
Ll. Set (2, (string) Val + "changed ");
System. Out. println ("ll after change:" + LL );
}
}
The program output is as follows:
Original contents of LL: [A, A2, F, B, D, E, C, Z]
Contents of LL after deletion: [A, A2, D, E, C, Z]
Ll after deleting first and last: [A2, D, E, C]
Ll after change: [A2, d, e changed, C]
Because the listlist interface implements the list interface, add (object) is called to append the project to the end of the list, just like addlast ()
Method. Use the add (INT, object) Form of the add () method to insert a project to a specified position, as shown in the example.

Example of calling add (1, "A2") in the program.
Note how to change the third element in ll by calling the get () and set () methods. To obtain
The current value of the element. The get () method is used to pass the lower value of the element. To add a new subscript
Value. The subscript and the corresponding new value are passed through the Set () method.

 

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.