The transformation of Core Java (25) List and array, the mutual conversion between set and array __java

Source: Internet
Author: User
the transformation between list and array

List into array

Called the ToArray method of the list, there are two methods with the same name, where object[] ToArray () returns an array of type object, but it is inconvenient to use. The other is public <T> t[] ToArray (t[] a), which returns a generic array of t[].

Array into List

The static method Aslist of the arrays class is invoked, returning a list wrapper that wraps a normal Java array. The object returned is not ArrayList, but a view object that implements the serializable interface to serialize and implements the Randomaccess interface.


Example:

Package Com.xujin;
Import java.util.ArrayList;
Import Java.util.Arrays;

Import java.util.List;
		public class linkedlisttest{public static void Main (STRING...ARG) {Employee Jim = new Employee ("Jim");
		Employee Bob = new Employee ("Bob");
		
		
		Employee gin = new Employee ("Gin"); /************list to array********************************/list<employee> staff = new ArrayList<Employee
		> ();
		Staff.add (Jim);
		Staff.add (Bob);
		Staff.add (gin);
		SYSTEM.OUT.PRINTLN (staff);//[name:jim, Name:bob, Name:gin] employee[] e = new employee[staff.size ()];
		Staff.toarray (e);
		for (Employee em:e) System.out.println (EM);
		* * * name:jim * name:bob * name:gin * * * * */************array to list********************************/
		list<employee> arraytolist = Arrays.aslist (e); System.out.println (Arraytolist.get (2)),//name:gin}} class employee{public Employee (String Name) {this.name =
	Name Public String GetName () {return THIS.NAme
	Public String toString () {return "name:" + name; Private String name = "";//instance domain initialization}


the conversion between set and array

Similar to the above, set into array is called toarray generic method, and array conversion to set is called the Aslist method of the array.

Example:

This example is almost the same as the above, the only difference is

/************set to array********************************/
set<employee> staff = new hashset<employee> ();

And the results are the same.

Package Com.xujin;
Import Java.util.Arrays;
Import Java.util.HashSet;
Import java.util.List;

Import Java.util.Set;
		public class linkedlisttest{public static void Main (STRING...ARG) {Employee Jim = new Employee ("Jim");
		Employee Bob = new Employee ("Bob");
		
		
		Employee gin = new Employee ("Gin");
		/************set to array********************************/set<employee> staff = new hashset<employee> ();
		Staff.add (Jim);
		Staff.add (Bob);
		Staff.add (gin);
		SYSTEM.OUT.PRINTLN (staff);//[name:jim, Name:bob, Name:gin] employee[] e = new employee[staff.size ()];
		Staff.toarray (e);
		for (Employee em:e) System.out.println (EM);
		* * * name:jim * name:bob * name:gin * * * * */************array to set********************************/
		list<employee> arraytolist = Arrays.aslist (e); System.out.println (Arraytolist.get (2)),//name:gin}} class employee{public Employee (String Name) {this.name =
	Name
Public String GetName () {		return this.name;
	Public String toString () {return "name:" + name; Private String name = "";//instance domain initialization}


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.